I'm trying to teach myself classes. I decided to create a class to represent a date. It's more for me to learn than to actually be useful. When I have the variables month, day, and year under private setting, the program will not compile (I am using Dev C++). When I move these three variables under public, the program compiles and works exactly as I want it to. I believe I have accessors and mutators set up properly, but if not please let me know. Why do these not work under a private setting?

Here is the date.h (with variables under private setting) file, and the date.cpp file. If you need to see the driver file let me know.

//date.h
using namespace std;

class date
{
      private:
      
      int month, day, year;  /*Variables under private setting, where
                             they don't work.*/
              
      public:
             
      /*  When I move the variables (int month, day, year) here under the 
      public setting the program works.  Why is this so? */   
      
      //Default constructor.
      date();
      
      //Parametized constructor.
      date(int newmonth, int newday, int newyear);
      
      //Accessor methods.
      double getmonth() const;
      double getday() const;
      double getyear() const;
      
      //Mutator methods.
      void changemonth(int newmonth);
      void changeday(int newday);
      void changeyear(int newyear);
      
      //Additional functions.
      //Print date in the form (#month/#day/#year).
      void numform(date d1);
      
      //Print date in the form (Month #day, year).
      void wordform(date d1);
      
};

/**************************************************************************************/

//date.cpp
#include "date.h"
#include<string>
#include<iostream>

using namespace std;

//Default constructor.
date::date()
{
            month=1;
            day=1;
            year=2000;
}
            
//Parametized constructor.
date::date(int newmonth, int newday, int newyear)
{
            month=newmonth;
            day=newday;
            year=newyear;
}

//Accessor methods.
double date::getmonth() const
{
       return month;
}
double date::getday() const
{
       return day;
}
double date::getyear() const
{
       return year;
}

//Mutator methods.
void date::changemonth(int newmonth)
{
     month=newmonth;
}
void date::changeday(int newday)
{
     day=newday;
}
void date::changeyear(int newyear)
{
     year=newyear;
}

//Additional functions.
//Numerical form.
void date::numform(date d1)
{
     cout<<month<<"/"<<day<<"/"<<year<<"\n";
}
//Written form.
void date::wordform(date d1)
{
     string monthword;
     switch(d1.month)
     {
                  case 1:
                       monthword="January";
                  break;
                  case 2:
                       monthword="February";
                  break;
                  case 3:
                       monthword="March";
                  break;
                  case 4:
                       monthword="April";
                  break;
                  case 5:
                       monthword="May";
                  break;
                  case 6:
                       monthword="June";
                  break;
                  case 7:
                       monthword="July";
                  break;
                  case 8:
                       monthword="August";
                  break;
                  case 9:
                       monthword="September";
                  break;
                  case 10:
                       monthword="October";
                  break;
                  case 11:
                       monthword="November";
                  break;
                  case 12:
                       monthword="December";
                  break;
     }
     
     cout<<monthword<<" "<<d1.day<<", "<<d1.year<<"\n";
}

Edited 6 Years Ago by Nick Evan: Added code-tags

Post your error code please. That will tell us whats wrong instead of going through code that isn't formatted.

Edited 6 Years Ago by LevyDee: n/a

Did you try to access your month, day, year variable directly from outside the class? Whats the error that you get?

Please note that your month, day, year variable are declared as int but your accessor methods are returning double. (this does not flag an error but it is not the right way)

//Accessor methods.
double getmonth() const;
double getday() const;
double getyear() const;

You're trying to access a private variable from outside the class. void date::wordform(date d1) Although you're inside the date class, you're not inside the instance of d1. d1.month will fail. You are trying to access the private variable of d1. d1.getmonth will succeed as you are accessing the public method, which returns the private instance of d1.month .

Post your error code please.

I agree. The code is more or less fine. The using namespace std statement at the top of the date.h header could cause a problem, but that should have nothing to do with accessing public vs. private members.

You're trying to access a private variable from outside the class. void date::wordform(date d1) Although you're inside the date class, you're not inside the instance of d1.

All member functions of a class have access to private members, even members of different objects of the class. There's nothing technically wrong with date::wordform, though the design is confused (it should either be a member function with no parameters or a non-member function with one date parameter).

Comments
Good answer and thanks for correcting me ^^

Here is the driver I wrote just to test out the various parts to the class.

//datedriver.cpp

#include "date.h"
#include<iostream>
#include<string>

using namespace std;

int main()
{
    date d1(8, 3, 1993);
    
    d1.numform(d1);  //Print number format.
    d1.wordform(d1);  //Print word format.
    
    //Change date through accesors.
    d1.changemonth(3);  
    d1.changeday(23);
    d1.changeyear(1991);
    
    //Print new date in both forms.
    d1.numform(d1);
    d1.wordform(d1);
    
    system("pause");
    return 0;
}

Narue, I took the

using namespace std;

out of date.h and everything worked fine. I was unaware that this could cause problems. Thanks.

I agree. The code is more or less fine. The using namespace std statement at the top of the date.h header could cause a problem, but that should have nothing to do with accessing public vs. private members.


All member functions of a class have access to private members, even members of different objects of the class. There's nothing technically wrong with date::wordform, though the design is confused (it should either be a member function with no parameters or a non-member function with one date parameter).

*facedesk* That's such an idiotic mistake I made =/

Many apologies. I think I must be tired, I've been making mistakes in my code all day here at work. The warm weather isn't helping ^^

Comments
retarded!
This question has already been answered. Start a new discussion instead.