Define and implement the overloaded constructors that support the following test
ID: 1812579 • Letter: D
Question
Define and implement the overloaded constructors that support the following test function for a Date class. The data members for the Date class are:
%u2022 year - integer number
%u2022 month - integer number
%u2022 day - integer number
int main()
{ Date d1(); //d1 will take all default values
Date d2(2011, 8, 2); //d2 will take all supplied values
Date d3(2011); //d3 will take supplied year, month and day will take default values
Date d4(2011, 9); //d4 will take supplied year and month, day will take default value
Date d5 = d2; //d5 will take the same value as d2
//the rest of the code
}
Explanation / Answer
#include <iostream>
using namespace std;
class Date
{
public:
int day;
int month;
int year;
Date();
Date(int year1, int month1, int day1);
Date(int year1);
Date(int year1, int month1);
};
Date::Date(void){
day = 1; // I have taken the following Default Values//
month =1;
year = 2001;
cout<<"Day:"<<day<<endl;
cout<<"Month:"<<month<<endl;
cout<<"Year:"<<year<<endl;
}
Date::Date(int year1, int month1, int day1){
day = day1;
month =month1;
year = year1;
cout<<"Day:"<<day<<endl;
cout<<"Month:"<<month<<endl;
cout<<"Year:"<<year<<endl;
}
Date::Date(int year1){
day = 1;
month =1;
year = year1;
cout<<"Day:"<<day<<endl;
cout<<"Month:"<<month<<endl;
cout<<"Year:"<<year<<endl;
}
Date::Date(int year1,int month1){
day = 1;
month =month1;
year = year1;
cout<<"Day:"<<day<<endl;
cout<<"Month:"<<month<<endl;
cout<<"Year:"<<year<<endl;
}
int main()
{
Date d1(); //d1 will take all default values
Date d2(2011, 8, 2); //d2 will take all supplied values
Date d3(2011); //d3 will take supplied year, month and day will take default values
Date d4(2011, 9); //d4 will take supplied year and month, day will take default value
Date d5 = d2; //d5 will take the same value as d2
cout<<"Date d5"<<endl;
cout<<"Day:"<<d5.day<<endl;
cout<<"Month:"<<d5.month<<endl;
cout<<"Year:"<<d5.year<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.