I want my out put to be like this cout

I want my out put to be like this

cout << event1 << endl;

cout << event2 << endl;

#===============================

#include <iostream>

#include <string>

using namespace std;

class Date

{  

   // overload stream insertion operators

   friend ostream& operator<< (ostream&, const Date&);

   public:

       Date(int d , int m , int y);

   private:

           int day ,month, year;       

};

//friend function class Date

ostream& operator << (ostream& os, const Date& x)

{

   os << \" Date: \" << x.month << \"/\" << x.day << \"/\" << x.year; //output date in month/day/year format

   return os;

}

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

   :day (d), month(m), year(y)

{}

class Event

{

   // overload stream insertion operators

   friend ostream& operator<< (ostream&, const Event& );

   public:

       Event(string e,int d, int m, int y);

       Event(string e,Date da);

   private:

       Date eventDate;

       string event;

};

//friend function class Event

ostream& operator <<(ostream &os, const Event& y)

{

   os << \"Event: \" << y.event << \" ,\";

   return os;

}

Event :: Event(string e,int d, int m, int y)

   :event(e), eventDate (d,m,y) //Constructor use member initializer list to pass initializer

                                      //values to constructors of member object event and eventDate

{} //Copy constructor that compiler provide implicitly

Event :: Event(string e,Date da)

   :event(e), eventDate(da)

{}

int main ()

{

   string eventName;

   int day, month, year;

   cout <<\"Enter Event name: \";

   getline(cin,eventName);

   cout << \"Enter Month as a number: \";

   cin >> month;

   cout << \"Enter Day: \";

   cin >> day;

   cout << \"Enter Year: \";

   cin >> year;

  

   //instantiate objects using different constructor

   Date date(day,month,year) ;

   Event event1(eventName,day,month,year);

   Event event2(eventName,date);

   cout << event1 << date << endl;

   cout << event2 << date << endl;

  

   return 0;

}

Solution

Your program is corect. If you want output to be like this

cout << event1 << endl;

cout << event2 << endl;

then remove date from cout and run this program--->

main.cpp

#include <iostream>

#include <string>

using namespace std;

class Date

{  

    // overload stream insertion operators

    friend ostream& operator<< (ostream&, const Date&);

    public:

        Date(int d , int m , int y);

    private:

            int day ,month, year;       

};

//friend function class Date

ostream& operator << (ostream& os, const Date& x)

{

    os << \" Date: \" << x.month << \"/\" << x.day << \"/\" << x.year; //output date in month/day/year format

    return os;

}

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

    :day (d), month(m), year(y)

{}

class Event

{

    // overload stream insertion operators

    friend ostream& operator<< (ostream&, const Event& );

    public:

        Event(string e,int d, int m, int y);

        Event(string e,Date da);

    private:

        Date eventDate;

        string event;

};

//friend function class Event

ostream& operator <<(ostream &os, const Event& y)

{

    os << \"Event: \" << y.event;

    return os;

}

Event :: Event(string e,int d, int m, int y)

    :event(e), eventDate (d,m,y)    //Constructor use member initializer list to pass initializer

                                      //values to constructors of member object event and eventDate

{}                                   //Copy constructor that compiler provide implicitly

Event :: Event(string e,Date da)   

    :event(e), eventDate(da)

{}

int main ()

{

    string eventName;     

    int day, month, year;

    cout <<\"Enter Event name: \";

    getline(cin,eventName);

    cout << \"Enter Month as a number: \";

    cin >> month;

    cout << \"Enter Day: \";

    cin >> day;

    cout << \"Enter Year: \";

    cin >> year;

  

    //instantiate objects using different constructor

    Date date(day,month,year) ;

    Event event1(eventName,day,month,year);

    Event event2(eventName,date);

    cout << event1 << endl;

    cout << event2 << endl;
    //cout << event1 << date << endl;

    //cout << event2 << date << endl;
    

    return 0;

}

Output:

I want my out put to be like this cout << event1 << endl; cout << event2 << endl; #=============================== #include <iostream
I want my out put to be like this cout << event1 << endl; cout << event2 << endl; #=============================== #include <iostream
I want my out put to be like this cout << event1 << endl; cout << event2 << endl; #=============================== #include <iostream
I want my out put to be like this cout << event1 << endl; cout << event2 << endl; #=============================== #include <iostream
I want my out put to be like this cout << event1 << endl; cout << event2 << endl; #=============================== #include <iostream

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site