CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Posts
    24

    Help with class and in and out opperators

    The code I have written is here:

    Code:
    #ifndef DATE_H
    #define DATE_H
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class date
      {
        private:
          enum m_e_month                    //member enumeration named month
          {
            January=1,
            Febuary=2,
            March=3,
            April=4,
            May=5,
            June=6,
            July=7,
            August=8,
            September=9,
            October=10,
            November=11,
            December=12
          };
          m_e_month m_m_e_month_month;                    //its a member variable m_ of type (m_e_month) named month
          unsigned short int m_sh_I_year;
          unsigned short int m_sh_I_day;
          void makeyear()
            {
              cout<<"enter the year:";
              int a;
              cin>>a;
    
            }
          void makemonth()
            {
              bool redo=1;
              cout<<"Enter the month's name it must be capitalied.";
              while(redo)
                {
                  string tmonth="0";
                  cin>>tmonth;                    //It set to 0 so is assumes nothings going to happen. If it weren't here thaen and infinite loop would start.
                  redo=0;                   //I have to use if and elses because switchs don't work with emums.
                  if(tmonth=="January")
                      {m_m_e_month_month=January;}
                  else if(tmonth=="Febuary")
                      {m_m_e_month_month=Febuary;}
                  else if(tmonth=="March")
                      {m_m_e_month_month=March;}
                  else if(tmonth=="April")
                      {m_m_e_month_month=April;}
                  else if(tmonth=="May")
                      {m_m_e_month_month=May;}
                  else if(tmonth=="June")
                      {m_m_e_month_month=June;}
                  else if(tmonth=="July")
                      {m_m_e_month_month=July;}
                  else if(tmonth=="August")
                      {m_m_e_month_month=August;}
                  else if(tmonth=="September")
                      {m_m_e_month_month=September;}
                  else if(tmonth=="October")
                      {m_m_e_month_month=October;}
                  else if(tmonth=="November")
                      {m_m_e_month_month=November;}
                  else if(tmonth=="December")
                      {m_m_e_month_month=December;}
                  else
                      {redo=1;cout<<"invalad please try again:";}
                }
            }
          void makeday()
            {
              int numofdays;
              int placeholder;
              placeholder=m_m_e_month_month;                    //again I can't use a switch statement due to repetitivness and it is not compatible
              if ((placeholder==1)||(placeholder==3)||(placeholder==5)||(placeholder==7)||(placeholder==8)||(placeholder==10)||(placeholder==12))
                {
                  numofdays=31;
                }
              else if ((placeholder==2))                    //this is just for febuary.
                {
                  numofdays=28;
                  if(m_sh_I_year%4==0)                    //febuary has leap years
                    {
                      numofdays=29;
                    }
                }
              else
                {
                  numofdays=30;                   //this is for all the other months
                }
              m_sh_I_day=0;                    //this is so the so while loop doesn't show the invalad thingy.
              do
                {
                  if (m_sh_I_day>numofdays)
                    {
                      cout<<"invalad. try again.";
                    }
                  cout<<"enter the day(number)";
                  cin>>m_sh_I_day;
                }while(m_sh_I_day>numofdays);
            }
        public:
          date(int a=0, m_e_month b=January, int c=-1)
            {
              m_sh_I_year=a;
              m_m_e_month_month=b;
              m_sh_I_day=c;
            }
          friend ostream& operator<<(ostream &out, date &adate);
          friend istream& operator>>(istream &in, date &adate);
      };
    
    ostream& operator<<(ostream &out, const date &adate)
      {
        out<<adate.m_m_e_month_month<<"/"<<adate.m_sh_I_day<<"/"<<adate.m_sh_I_year;
        return out;
      }
    
    istream& operator>>(istream &in, date &adate)
      {
        adate.makeyear();
        adate.makemonth();
        adate.makeday();
      }
    
    #endif
    and the bugs are:
    Code:
    In function `std::ostream& operator<<(std::ostream&, const date&)':
    `date::m_e_month date::m_m_e_month_month' is private
    within this context
    `short unsigned int date::m_sh_I_day' is private
    within this context
    `short unsigned int date::m_sh_I_year' is private
    within this context
    I'm trying to keep the variables private so I can't edit them and the class will be easier to use. I think I have the friend declaration right. Does any one know how to keep the code usable yet still keep makemonth() , makeday() , and makeyear() , private?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Help with class and in and out opperators

    One option is to define Day, Month, and Year classes, e.g.,
    Code:
    class Day
    {
    public:
        explicit Day(unsigned short day);
        // ...
    private:
        unsigned short day_;
    };
    
    class Month
    {
    public:
        enum Name
        {
            January = 1,
            February,
            March,
            April,
            May,
            June,
            July,
            August,
            September,
            October,
            November,
            December
        };
    
        explicit Month(Name month);
        // ...
    private:
        Name month_;
    };
    Scott Meyers has an example in Effective C++ that has the month values as static member functions instead, in view that enums are not very type safe:
    Code:
    class Month
    {
    public:
        static Month Jan() { return Month(1); }
        // ...
    private:
        explicit Month(int month); // prevent creation of new Month values
        // ...
    };
    Anyway, with this design you could have a Date class like this:
    Code:
    class Date
    {
    public:
        Date(const Day& day, const Month& month, const Year& year);
        // ...
    private:
        Day day_;
        Month month_;
        Year year_;
    };
    Now, the makemonth(), makeday() and makeyear() functions should be non-member non-friend functions. They would request for user input, process it, and return Month, Day and Year objects respectively. You can then use these objects to construct a Date object.

    The other member functions of the Date object might be a getter function for each of day, month and year. If you do not want Date objects to be modifiable after creation, do not provide any correspondong setter functions.

    The overloaded operator<< would then just use the getter functions to print the date, and presumably you would overload operator<< for each of Day, Month and Year.

    It might not be such a good idea to overload operator>> to read with interactive input. Instead, you might decide on a date format, and then have operator>> read using that format. If so, you could ditch the makemonth(), makeday() and makeyear() functions and go for a makeYear() function that requests for the date in the specified format, and then uses the overloaded operator>> to read it in.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: Help with class and in and out opperators

    E-man96,

    Code:
          friend ostream& operator<<(ostream &out, const date &adate);
    This will solve the problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured