26.4k views
3 votes
1.1. Create an application that overload the operator << and >> in order to manipulate standard input and output. The program must follow these specifications: - You will need to create an application named "yearOverload.cpp", a class named "DateFormat". Create the heading file and the implementation file. - The program must allow the user to a date with this format mm/dd/yyyy.

User Jmaurier
by
6.8k points

1 Answer

3 votes

Answer:

The cpp program is given thus:

Step-by-step explanation:

Date.h:

#ifndef DATE_H

#define DATE_H

using namespace std;

class Date

{

public:

string mm,dd,yyyy;

public:

Date();

string getMonth();

string getDay();

string getYear();

void setValues(char*);

friend istream &operator>>(istream &is, Date &D)

{

char str[20];

is>>str;

D.setValues(str);

return is;

}

friend ostream &operator<<( ostream &output, Date &D ) {

output << "Full Format ==> " << D.getMonth() << " " << D.getDay()<< ", " <<D.getYear()<<endl;

return output;

}

};

#endif

yearOverload.cpp:

#include <iostream>

#include<string>

#include<cstring>

#include <sstream>

#include "Date.h"

using namespace std;

Date::Date()

{

}

string Date::getMonth()

{

stringstream s;

s << mm;

return s.str();

}

string Date:: getDay()

{

stringstream s;

s << dd;

return s.str();

}

string Date:: getYear()

{

stringstream s;

s << yyyy;

return s.str();

}

void Date:: setValues(char* str)

{

string months[13]= {" ","January","February","March","April","May","June","July","August","September","October","November","December"};

string s1;

s1 = strtok(str, "/");

dd = strtok(NULL,"/");/*set day*/

yyyy = strtok(NULL,"/");/*set year*/

cout<<"Here the results :"<<endl;

cout<<"Month : "<<s1<<endl;

cout<<"Day : "<<dd<<endl;

cout<<"Year : "<<yyyy<<endl<<endl;

int month;

std::istringstream iss (s1);

iss >> month;

if(month>0&&month<13)

mm = months[month];/*set month*/

else

cout<<"Error invalid date";

}

main.cpp:

#include<iostream>

#include"Date.h"

#include"yearOverload.cpp"

using namespace std;

int main()

{

Date d;

cout<<"Enter a date with this format mm/dd/yyyy ==> ";

cin>>d;

cout<<d;

return 0;

}

User Quirin
by
7.3k points