Design a class Calendar to calculate number of years, months and days. The member variables are nds, years, months and days. The member functions are d() which accepts number of days and display() to display total number of years, months and days.
#include <iostream>
using namespace std;
class Calendar
{
private:
int nds; // number of days
int months;
int year;
int days;
public:
//default constructor
Calendar(){
nds = 0; // number of days
months= 0;
year = 0;
days = 0;
}
// The member functions: accepts number of days
void d ();
// The member functions: display number of years, months and days
void Display ();
};
void Calendar::d ()
{
cout << "Please Input the number of days : ";
cin >> nds;
year = nds / 365; // number of years
months = (nds%365)/30; // number of months
days = (nds%365)%30; // number of days
}
void Calendar::Display ()
{
cout << "Years: " << year << "\nMonths: " << months << "\nDays: " <<
days<<endl<<endl;
}
int main ()
{
// Create an object myCalendar of Class Calendar
Calendar myCalendar;
myCalendar.Display (); // display default number of years, months and days
myCalendar.d (); // accepts number of days
myCalendar.Display (); // display number of years, months and days
return 0;
}
Comments
Leave a comment