1 Write a C program that has a class called MyDate with the

1. Write a C++ program that has a class called MyDate with the following declaration:
2. In your main function, write the following.

class MyDate

{

private:

     int month;

     int day;

     int year;

public:

};


If you try to compile and run this, you should have a problem.
3. Let’s add a constructor with no argument to the MyDate class and then set month and day to 1 and year to 2016.
Add the MyDate() constructor under the public keyword in the declaration of the MyDate class.
Then write definition of this constructor code after the declaration of the class.

class MyDate

{

private:

     int month;

     int day;

     int year;

public:

     myDate();

};

MyDate::MyDate()

{

     month = 1;

     day = 1;

     year = 2016;


4. Then modify main function to look like this:

int main()

{

     MyDate d1;

     cout << d1.month << \"/\" << d1.day

                                  << \"/\" << d1.year;

return 0;


Then run it, and you should still have the same problem as before.
5. Now let’s modify our MyDate() class to have method to return each of its data members.
So add the following three methods: getMonth(), getDay() and getYear()

class MyDate

{

private:

     int month;

     int day;

     int year;

public:

myDate();

     int getMonth();

     int getDay();

     int getYear();

};

6. The define all these methods one by one. Here is the definition of the getMonth() method.

int myDate::getmonth()

{

     return month;

}


Complete definition for getDay() and getYear()
7. Now modify main() function to look like this and compile and run.

int main()

{

     MyDate d1;

     cout << getMonth << \"/\" << getDay

                                  << \"/\" << getYear;

     return 0;

}


Compile and run.
Still errors.
Remember that in Object oriented programming, everything is based on an object. Hence, method must be called only via an object.
8. Now modify main() function to look like this and compile and run.

int main()

{

     MyDate d1;

     cout << d1.getMonth() << \"/\" d1.getDay()

                                   << \"/\" d1.getYear();

     return 0;

}


Run and you should have output for values set by empty constructor:
1/1/2016


9. Now three setter methods for setMonth(), setDay() and setYear() as shown below.

class MyDate

{

private:

     int month;

     int day;

     int year;

public:

     myDate();

     void setMonth(int);

     void setDay(int);

     void setYear(int);

     int getMonth();

     int getDay();

     int getYear();

};
Here is code for setMonth()

void MyDate::setMonth(int m)

{

month = m;

}


Comlete code for setDay() and setYear()
10. Now modify main() function to look like this and compile and run.

int main()

{

     MyDate d1;

     MyDate d1;

     d2.setmonth(11);

     d2.setDay(21);

     d2.setYear(2016);

     cout << \"Day d1 : \" << d1.getMonth() << \"/\" << d1.getDay()

                                 << \"/\" d1.getYear();

     cout << \"\ \ Day d2 : \" << d2.getMonth() << \"/\" << d2.getDay()

                                 << \"/\" d2.getYear();

     return 0;

You should have this output:

Day d1 : 1/12016

Day d2 : 11/21/2016


11. Now modify the definition of getMonth() method as shown below:

int MyDate::getMonth()

{

     month = 13;

     return month;

}


Then compile and run to have this output:

Day d1 : 13/1/2016

Day d2 : 13/21/2016


This is NOT good since the month is now set to 13 every time the getMonth() method is called.
12. Now let’s add the const keyword to all the getter methods as shown below:

class MyDate

{

private:

     int month;

     int day;

     int year;

public:

     myDate();

     void setMonth(int);

     void setDay(int);

     void setYear(int);

     int getMonth() const;

     int getDay() const;

     int getYear() const;

};

13. Also add the const keyword in the header line of each method such as:

int MyDate::getMonth() const

{

     month = 13;

     return month;


Do same for the getDay() and getYear()
14. Now if you try to run program noq, the assignment of 13 to month in these methods will be a syntax error due to the const keyword.
Remove this assignment of 13 to month, then go on.
15. Now add another constructor that accept 3 integer values; for month, day and year.
So under the existing constructor, add this new constructor such that it will accept 3 integer values as shown below:

public:

MyDate();

MyDate(int, int, int);


16. Then write definition of this constructor.

MyDate::MyDate(int m, int d, int y)

{

     month = m;

     day = d;

     year = y;

}


17. Now modify main() function to explicitly use this constructor just added when creating the object d1 as shown below:

int main()

{

MyDate d1 (10,24,2015);

MyDate d2;

d2.setMonth(11);

d2.setDay(21);

d2.setYear(2016);

     cout << \"Day d1 : \" << d1.getMonth() << \"/\" << d1.getDay()

                                   << \"/\" << d1.getYear();

     cout << \"\ \ Day d2 : \" << d2.getMonth() << \"/\" << d2.getDay()

                                   << \"/\" << d2.getYear();

     return 0;

Complie and run and you should have this output:

Day d1 : 10/24/2015

Day d2 : 11/21/2016


Note that values for d1 are the values stated in the constructor.
18. Now add 2 functions printDateUS() and printDateENG() So your class will be like this now:

class MyDate

{

private:

     int month;

     int day;

     int year;

public:

     myDate();

     void setMonth(int);

     void setDay(int);

     void setYear(int);

     int getMonth() const;

     int getDay() const;

     int getYear() const;

     void printDateUS() const;

     void printDateENG() const;

};

19. Now write definition for printDateUS() to print values in a Date object in the format mm/dd/yy and for the function printDateENG to print date in format dd/mm/yy
20 Modify main() function to make use of these 2 functions as shown below:

int main()

{

MyDate d1 (10,24,2015);

MyDate d2;

d2.setMonth(11);

d2.setDay(21);

d2.setYear(2016);

d1.printDateENG();

d2.printDateUS();

return 0;


21. Compile and run and you should have this output:

24/10/2015

11/21/2016


22. Add a method/function called setDate() that accepts three integers, and they (the parameters) are for the month, day, year in that order.

void setDate(int, int int);


23. Define method setDate() as explained above.
24. Modify main() to be like what shown below and then compile and run:

int main()

{

MyDate d1 (10,24,2015);

MyDate d2;

d2.setDate(11,21,2016);

d1.printDateENG();

d2.printDateUS();

return 0;

}

You should have this output:

24/10/2015

1/1/2016


25. Now, was the definition of your setDate() method something like this?

void myDate::setDate(int m, int d, int y)

{

month = m;

day = d;

year = y;

}

While it works, there is repetition of work done here. These assignments are exactly what was done in our constructor defined above.
26. Now, let’s modify definition of setDate() to make use of existing constructor.
Modify setDate() to be like this. Note the call to the constructor.

void::setDate(int m, int d, int y)

{

MyDate(m,d,y);

}

Solution

#include<bits/stdc++.h>
using namespace std;

class myDate
{
private:
int month;
int day;
int year;
public:
myDate();
myDate(int , int , int );
int getDay() const;
int getYear() const;
  

int getMonth()const;
void setMonth(int);
void setDay(int);
void setYear(int);
void setDate(int , int , int );
void printDateUS() const;
void printDateENG() const;
};

void myDate::setDate(int m, int d, int y)
{
//myDate(m,d,y);
}
void myDate::printDateUS()const
{
   cout<<month<<\"/\"<<day<<\"/\"<<year<<endl;
}

void myDate::printDateENG()const
{
   cout<<day<<\"/\"<<month<<\"/\"<<year<<endl;
}
myDate::myDate()
{
month = 1;
day = 1;
year = 2016;
}
myDate::myDate(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
int myDate::getMonth()const
{
return month;
}
int myDate::getDay()const
{
return day;
}
int myDate::getYear()const
{
return year;
}

void myDate::setDay(int d)
{
day = d;
}

void myDate::setMonth(int m)
{
month = m;
}

void myDate::setYear(int y)
{
year = y;
}

int main()
{
myDate d1;
myDate d2;
d2.setMonth(11);
d2.setDay(21);
d2.setYear(2016);
cout << \"Day d1 : \"<< d1.getMonth() << \"/\" << d1.getDay()<< \"/\"<<d1.getYear();
cout << \"\ \ Day d2 : \" << d2.getMonth() << \"/\" << d2.getDay()<< \"/\"<<d2.getYear();
return 0;

}

====================================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ g++ MyDate.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Day d1 : 1/1/2016

Day d2 : 11/21/2016

1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int
1. Write a C++ program that has a class called MyDate with the following declaration: 2. In your main function, write the following. class MyDate { private: int

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site