month class program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nkhosinathie
    New Member
    • May 2007
    • 91

    month class program

    hello guys,i've started with classes and i've been given this program below to program and also i will post the source code if you need it.this program reads as folllows.

    create a class called DATE that includs three pieces of information as data member a month(type int),a day(type int) and year(type int).your class should have a constructor with three parameters that uses the parameters to initialise the three data members,for the purpose of this exexcise,assume that the values provided for the year and day are correct,but ensure that the month value is in the range (1-12); if it is not,set the month to 1.provide a member function DISPLAYDATE that display the month,day and year separated by forward slashes(/).write a test program that demonstrate CLASSDATE's capabilities.

    i'm new to this classes so please forgive me for including my full source code below. my problem firstly my class doesn't use the function display and it doesn't display the year entered.


    Code:
    #include<iostream>
    
    using namespace std;
    
    
    class Date
    {
    
    public:
    
    	Date(int MONTH,int DAY,int YEAR)
    
    	{
    
    	set(MONTH,DAY,YEAR);
    
    	}
    		
    		
    void set(int MONTH,int DAY,int YEAR)
    
    {
    
     month=MONTH;
     day=DAY;
     year=YEAR;
    
    }
    int getmonth(int MONTH)
    { 
    return MONTH;		
    }
    int getday(int day)
    {
    return day;
    }
    int getyear(int year)
    {
    return year;
    }
    void display(int MONTH,int DAY,int YEAR)
    
    {
    	cout<<"welcome today\n"<<getmonth(MONTH)<<getday(DAY)<<getyear(YEAR);
    	cout<<endl;
    }
    
    private:
    	int month;
    	int day;
    	int year;
    };
    
    int main()
    {
        Date mymonth(00,00,0);
    
    	char slash='/';
    
    	int month,day,year;
    
        cout<<"please enter the date today separated by the forward slash key(e.g 12/08/2004)\n";
    
    	cin>>month>>slash>>day>>year;
    	
    	cout<<"The date today is\n"<<mymonth.getmonth(month)
    		<<slash
    		<<mymonth.getday(day)
    		<<slash
    		<<mymonth.getyear(year)
    	    <<endl;
    	
    	
    
    return 0;
    }
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by Nkhosinathie
    hello guys,i've started with classes and i've been given this program below to program and also i will post the source code if you need it.this program reads as folllows.

    create a class called DATE that includs three pieces of information as data member a month(type int),a day(type int) and year(type int).your class should have a constructor with three parameters that uses the parameters to initialise the three data members,for the purpose of this exexcise,assume that the values provided for the year and day are correct,but ensure that the month value is in the range (1-12); if it is not,set the month to 1.provide a member function DISPLAYDATE that display the month,day and year separated by forward slashes(/).write a test program that demonstrate CLASSDATE's capabilities.

    i'm new to this classes so please forgive me for including my full source code below. my problem firstly my class doesn't use the function display and it doesn't display the year entered.


    Code:
    #include<iostream>
    
    using namespace std;
    
    
    class Date
    {
    
    public:
    
    	Date(int MONTH,int DAY,int YEAR)
    
    	{
    
    	set(MONTH,DAY,YEAR);
    
    	}
    		
    		
    void set(int MONTH,int DAY,int YEAR)
    
    {
    
     month=MONTH;
     day=DAY;
     year=YEAR;
    
    }
    int getmonth(int MONTH)
    { 
    return MONTH;		
    }
    int getday(int day)
    {
    return day;
    }
    int getyear(int year)
    {
    return year;
    }
    void display(int MONTH,int DAY,int YEAR)
    
    {
    	cout<<"welcome today\n"<<getmonth(MONTH)<<getday(DAY)<<getyear(YEAR);
    	cout<<endl;
    }
    
    private:
    	int month;
    	int day;
    	int year;
    };
    
    int main()
    {
        Date mymonth(00,00,0);
    
    	char slash='/';
    
    	int month,day,year;
    
        cout<<"please enter the date today separated by the forward slash key(e.g 12/08/2004)\n";
    
    	cin>>month>>slash>>day>>year;
    	
    	cout<<"The date today is\n"<<mymonth.getmonth(month)
    		<<slash
    		<<mymonth.getday(day)
    		<<slash
    		<<mymonth.getyear(year)
    	    <<endl;
    	
    	
    
    return 0;
    }
    When you run your main method what is the output?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      There are several problems with your code, first being:

      In your 'getter' methods (getday, getmonth, and getyear), you are passing the function an int argument. The function then returns that argument without doing anything. This is pointless! The user already has the value used as the argument, why would he/she/it need it returned!? Your getter functions should have no parameters and simply return the class member.

      If you fix this, your display function will not work, as you are passing arguments to these functions. You can re-think this, though - there's no need to call those functions when display is a member function! It can directly access month, day, and year, so why not directly access them?

      Finally, your main() method is faulty. Instead of trying to be fancy and accept input in the form of mm/dd/yyyy, just get each part separately. You have bee instructed to demonstrate your DATE class, not your input manipulation in main().

      Comment

      • Nkhosinathie
        New Member
        • May 2007
        • 91

        #4
        Originally posted by RedSon
        When you run your main method what is the output?
        my problem is my program has to test this classdate so i'm not sure firstly how my member functions has to have any parameters or not

        Comment

        • Nkhosinathie
          New Member
          • May 2007
          • 91

          #5
          Originally posted by Ganon11
          There are several problems with your code, first being:

          In your 'getter' methods (getday, getmonth, and getyear), you are passing the function an int argument. The function then returns that argument without doing anything. This is pointless! The user already has the value used as the argument, why would he/she/it need it returned!? Your getter functions should have no parameters and simply return the class member.

          If you fix this, your display function will not work, as you are passing arguments to these functions. You can re-think this, though - there's no need to call those functions when display is a member function! It can directly access month, day, and year, so why not directly access them?

          Finally, your main() method is faulty. Instead of trying to be fancy and accept input in the form of mm/dd/yyyy, just get each part separately. You have bee instructed to demonstrate your DATE class, not your input manipulation in main().
          i'm sorry to ask you this, should i put the month,day and year in my main function?

          Comment

          • Nkhosinathie
            New Member
            • May 2007
            • 91

            #6
            i decided to change my source code especially the class date,but my problem is when i compile it,it says date my book can't access my private members.

            this is the code.


            Code:
            code
            #include<iostre am>
            using namespace std;

            [CODE=cpp] class Date
            {

            Date ()
            {
            (0,0,0);
            }
            Date (int m)
            {
            (m, 0,0);
            }
            Date(int m, int d, int y)
            {
            setDateClass( m ,d ,y );
            }

            void setDateClass(in t m, int d, int y)
            {
            setmonth(m);
            setday(d);
            setyear(y);
            }
            void setmonth( int m)
            {
            month =((m >=0 && m <=12)? m : 0);
            }
            void setday( int d)
            {
            day = ((d >=0 && d <=31)? d : 0);
            }
            void setyear(int y)
            {
            year = ((y >= 0 && y <=2007)? y : 0);
            }
            int getmonth()
            {
            return month;
            }
            int getday()
            {
            return day;
            }
            int getyear()
            {
            return year;
            }

            private:

            int month;
            int day;
            int year;
            int months[12];
            };

            int main()
            {

            int month;
            int day;
            int year;


            Date mybook;

            cout<<"today's date is\n"<<mybook.g etmonth()
            <<"/"
            <<mybook.getdat e()
            <<"/"
            <<mybook.getyea r()
            <<endl;


            return 0;
            }[/CODE]
            Last edited by Ganon11; Oct 11 '07, 01:42 PM. Reason: Please use the [CODE] tags provided.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              You haven't put the public: modifier in front of your constructor and methods. In C++, by default, all members of a class are considered private. To override this, you need to put the public: keyword in front of the public functions, just like you put private: in front of your private data.

              Comment

              Working...