asked 222k views
1 vote
Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sunday for Sunday. The program should be able to perform the following operations on an object of type dayType: Step a: Set the day. Step b: Print the day. Step c: Return the day. Step d: Return the next day. Step e: Return the previous day. Step f: Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday. Step g: Add the appropriate constructors. Write the definitions of the functions to implement the operations for the class dayType.

1 Answer

2 votes

#include<iostream>

#include<string>

usingnamespace std;

void menu(); //declartion of function to show the menu

classdayType//creating the class

{

string Wdays[7];

int i;

string day;

string prDay;

string NxtDay;

string AddDays;

public:

dayType(string);//constructor with one parameter

string setday(); //declartion of function to set the day

string preday(); // declartion of function to find privious day

void Nextday(); // declartion of function to find Next day

string add(int n); // declartion of function to find after addingthenumber of days given by the user

void print(); // declartion of function to print the result

};

int main()

{

int n;

string d;

menu(); //displaying menu

cout <<"Enter the day :";

getline(cin, d); //Takes the day from the user

dayType Da(d); //creating an object of a class

Da.setday();

Da.preday();

Da.Nextday();

cout <<"Enter the No. of days to add :";

while (!(cin >> n) ||n<0) { //validation checking

cin.clear();

cin.ignore(999, '\\');

cout <<"Invalid data type! \\Please enter No. of daysto add again :";

}

Da.add(n);

Da.print();

system("pause");

return 0;

}

dayType::dayType(stringi) :day(i){ //Defintion of a constructor with one parameter

Wdays[0] = "Mon";

Wdays[1] = "Tues";

Wdays[2] = "Wednes";

Wdays[3] = "Thurs";

Wdays[4] = "Fri";

Wdays[5] = "Satur";

Wdays[6] = "Sun";

}

stringdayType::setday() //Function to set the day

{

if (day == Wdays[0])

{

i = 0;

}

elseif (day == Wdays[1])

{

i = 1;

}

elseif (day == Wdays[2])

{

i = 2;

}

elseif (day == Wdays[3])

{

i = 3;

}

elseif (day == Wdays[4])

{

i = 4;

}

elseif (day == Wdays[5])

{

i = 5;

}

elseif (day == Wdays[6])

i = 6;

else

{

day = "Invalid Input";

i = 7;

}

return day;

}

stringdayType::preday() //Function to find the preivous day

{

if (i == 0)

prDay = Wdays[6];

elseif (i == 7)

prDay = "Invalid Input";

else

prDay = Wdays[i - 1];

return prDay;

}

voiddayType::Nextday() //Function to find Next day

{

if (i == 6)

NxtDay = Wdays[0];

elseif (i == 7)

prDay = "Invalid Input";

else

NxtDay = Wdays[i + 1];

}

stringdayType::add(intn) // function to find after adding the number of days given by the user

{

n = n + i;

while (n>= 7)

{

n = n - 7;

}

if (i == 7)

Wdays[n] = "Invalid Input ";

return AddDays = Wdays[n];

}

voiddayType::print()

{

cout << endl <<"\tDay="<< day <<"day"<< endl;

cout <<"\tPrevious day :"<< prDay <<"day"<< endl;

cout <<"\tNext day :"<< NxtDay <<"day"<< endl;

cout <<"\tAfter Adding Days :"<< AddDays <<"day"<< endl;

}

void menu() //Function to display menu

{

cout <<"******************MENU********************"<< endl;

cout <<"\tEnter 'Sun' for 'Sunday'"<< endl;

cout <<"\tEnter 'Mon' for 'Monday'"<< endl;

cout <<"\tEnter 'Tues' for 'Tuesday'"<< endl;

cout <<"\tEnter 'Wednes' for 'Wednesday'"<< endl;

cout <<"\tEnter 'Thurs' for 'Thursday'"<< endl;

cout <<"\tEnter 'Fri' for 'Friday'"<< endl;

cout <<"\tEnter 'Satur' for 'Saturday'"<< endl;

answered
User Ggomeze
by
8.5k points

No related questions found