classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greek
    New Member
    • Nov 2006
    • 17

    classes

    Hi...ive having some prob dealing wiv classes question though ive understood the concept.. if u cud help me wiv the following question:

    The class Date has the following data members:
     Month
     Day
     Year
    and member functions:
     A default constructor that initialises all data members to zero.
     A constructor to create and initialise a Date object with the values passed as parameters
     Assigndate to assign values to the data members from the keyboard in the format:
    Day: 9
    Month: 10
    Year: 2002
     Displayshort to display a date in short form e.g. “09/10/2002”
     DisplayVerbose to display a date in form “October 9, 2002”
     NextDay to change the date to next day e.g. “October 10, 2002”

    (a) Write the class interface for the class Date.

    (b) Write the full function implementation for the member functions of the class Date.

    (c) Write a main program that declares two objects D1 and D2 of type Date. D1 must be initialised to the value “23 October 2002” and D2 must get its value from the keyboard. The program must then find the earlier of D1 and D2 and display the same in both formats. The program must also set the date D2 to next day and display the same in both formats.



    ive started by doin the 3rd part c directly but dunno how to continue and if wat ive done is correct..this is wat ive manage to do uptil nw:


    Code:
    #include<iostream.h>
    #include<conio.h>
    
      class Date{
        char month[10];
        int month;
        int day;
        int year;
    
      public:
        Date();
        Date(char, int, int);
        void Assigndate();
        void Displayshort();
        void DisplayVerbose();
        void NextDay();
        }
    
    Date::Date()
    { month=0;
      day=0;
      year=0;
    }
    
    void Assigndate()
    { cin>>day;
      cout<<"Day: "<<day<<endl;
      cin>>month;
      cout<<"Month: "<<month<<endl;
      cin>>year;
      cout<<"Year: "<<year<<endl;
    }
    
    void Displayshort()
    { cout<<day<<"/"<<month<<"/"<<year<<endl;
    }
    
    void NextDay()
    { D1.date(char, int, int)
     d++;
     cout<<D1;
    }
    
    
    void main(){
    Date D1,D2;
    
    cout<<"Enter the date"<<endl;
    cin>>date;


    ive not completed it..hw do i display the date in different formats..plz correct it n how do i continue
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    For the different formats...

    When you get the month variable either from the constructor or from the Assigndates function, you can use a set of if...else statements to determine the name of the month. If the month entered is 1, then the name is January, and you can store this in your char array. If the month entered is 2, then the month is February, and so on.

    When you get to the DisplayVerbose function, you can display the month name, followed by a space, followed by the day, followed by a comma and a space, then the year.

    Comment

    Working...