How to overload constructors

As always thanks for trying to help!

I have to create a class called "Date"
which will work with a given "driver program"

here is the given program which is not supposed to be changed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//Use this file in unmodified form with the following exceptions:
//If you used class names other than "Date" and "FutureDate", you may modify those
//  lines to reflect your class names
//If you are using an operating system that does not support the system("pause")
//  function, you may remove it or replace it with a cin statement.

//Please note: while testing, you will probably want to comment out portions of this
//  code to make sure your class is working.  However, the submitted classes should
//  run using the entire file (with possible modifications above).

#include <cstdlib>
#include <iostream>
#include "Date.h"
#include "FutureDate.h"
using namespace std;

int main()
{
    int menuChoice; //used for menu
    int tempMonth;
    int tempDay;
    int tempYear;
    cout<<"Creating Date object with default constructor..."<<endl;
    Date date1;
    cout<<"Stream insertion operator..."<<endl;
    cout<<date1<<endl;
    cout<<"\nStream extraction operator..."<<endl;
    cin>>date1;
    cout<<"Stream insertion operator..."<<endl;
    cout<<date1<<endl;
    
    //Ask user to enter data for next 2 objects
    cout<<"\n\nYou will be asked to enter a date to be used for the next 2 object."<<endl;
    cout<<"Try entering months and days outside the valid range."<<endl;
    cout<<"This program does not try to validate leap years."<<endl;
    cout<<"\n  Enter a month: ";
    cin>>tempMonth;
    cout<<"  Enter a day: ";
    cin>>tempDay;
    cout<<"  Enter a year:";
    cin>>tempYear;
    
    cout<<"\nCreating a Date object with the overloaded constructor..."<<endl;
    Date date2(tempMonth, tempDay, tempYear);
    cout<<"Stream insertion operator..."<<endl;
    cout<<date2<<endl;
    
    cout<<"\nCreating a FutureDate object using the overloaded constructor..."<<endl;    
    FutureDate aFutureDate(tempMonth, tempDay, tempYear);
    cout<<"Stream insertion operator..."<<endl;
    cout<<aFutureDate<<endl;
    
    cout<<"\n\nYou will now use the in/decrement operators on the FutureDate"<<endl;
    cout<<"Press (1) to use the increment operator"<<endl;
    cout<<"Press (2) to use the decrement operator"<<endl;
    cout<<"Press (0) to exit"<<endl;
    cin>>menuChoice;
    while(menuChoice)
    {
        switch(menuChoice)
        {
        case 1:
            aFutureDate++;
            cout<<aFutureDate<<endl;
            break;
        case 2: 
            aFutureDate--;
            cout<<aFutureDate<<endl;
        }
        cout<<"Press (1) to use the increment operator"<<endl;
        cout<<"Press (2) to use the decrement operator"<<endl;
        cout<<"Press (0) to exit"<<endl;
        cin>>menuChoice;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



here is the header I'm working on:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Date
{
private:
	int month;
	int day;
	int year;
public:
	Date();
	Date()
	{
		month = 12;
		day = 25;
		year = 2005;
	};
	
	Date(int m, int d, int y)
	{
		month = m;
		day = d;
		year = y;
	};
};


My issue is that every time the driver program uses objects of Date in cin or cout. I get:

Error no operator matches these operands. I googled and found that I have to overload the constructor:

but I can't figure out how
could somebody please give an example for this case?

Thanks ahead
Overloading a method, like a constructor, is just having more than one of them with different sets of parameters but the same name.

You already have overloaded the constructor.

What you need to do is overload the << operator.
Last edited on
If you want to output a custom data type with cout, or populate one with cin, you must provide proper overloads for the << or >> operators respectively.

For example, this operator<< overload will allow you to output a custom struct with cout:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Data
{
    int x, y;
}

// Global operator<< overload:
std::ostream &operator<<(std::ostream &os, const Data &d)
{
    os << "(" << d.x << ", " << d.y << ")";
    return os;
}

//Now this will work:
Data someData;
std::cout << someData << std::endl;
Last edited on
Thanks htirwin and Thumper for the replies.

I commented most of the driver.cpp out as I began to understand "what it what" This way I don't get confused with later problems. All I'm dealing with right now is

1
2
3
4
5
6
7
8
cout<<"Creating Date object with default constructor..."<<endl;
    Date date1;
    cout<<"Stream insertion operator..."<<endl;
    cout<<date1<<endl;
    cout<<"\nStream extraction operator..."<<endl;
    cin>>date1;
    cout<<"Stream insertion operator..."<<endl;
    cout<<date1<<endl;


I have tried to implement Thumpers code into my header file .. not excactly succesfull... so I kept researching and found out about templates

I adjusted Thumpers code snippet so it fits mine without syntax errors.

like I stated in the original post the .cpp can't be changed so it hall has to happen inside the header file.

I have messed with this all night and finally got Visual Studio to look happy "Syntax wise"

but all that my code produces now

is
1 1,1


It should be
December 25, 2005


Where does it get the ones from???

Here is my "grown pretty big" header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Class specific #includes
#ifndef DATE_H
#define DATE_H
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
// Class Declaration
class Date
{
	//private by default
	int month;
	int day;
	int year;
public://public until changed again	
	//standard date
	int getMonth() const;
	void setMonth(int);
	int getDay() const;
	void setDay(int);
	int getYear() const;
	void setYear(int);
	void display(int);
	//standard values
	Date()
	{
		day = 25;
		month = 12;
		year = 2005;
	}
	//get set template
	Date (int m, int d, int y)
	{
		day = d;
		month = m;
		year = y;
	}
	//function to display date 
	void display(int m, int d, int y)
	{
		if ( month = 1)		
			cout << "January";
		else if (month = 2)
			cout << "February";
		else if (month = 3)
			cout << "March";
		else if (month = 4)
			cout << "April";
		else if (month = 5)
			cout << "May";
		else if (month = 6)
			cout << "June";
		else if (month = 7)
			cout << "July";
		else if (month = 8)
			cout << "August";
		else if (month = 9)
			cout << "September";
		else if (month = 10)
			cout << "October";
		else if (month = 11)
			cout << "November";
		else if (Date::month = 12)
			cout << "December";		
		//display date in standard format
		//cout<<" "<<day<<", "<<year<<endl;
	}	
};//class end
	//set month
	void Date::setMonth(int m)
	{ 
		month = m;
	}
	//get month
	int Date::getMonth() const
	{ 
		return month;
	}	
	//set day
	void Date::setDay(int d)
	{ 
		day = d;
	}
	//get day
	int Date::getDay() const
	{
		return day;
	}
	//set year
	void Date::setYear(int y)
	{
		year = y;
	}		
	//get year
	int Date::getYear() const
	{
		return year;
	}	
	

	
template <class Date>Date date1();
	ostream& operator << (ostream& output, Date& date1)
	{
		
		output << &Date::getMonth <<" "<<&Date::getDay<<","<<&Date::getYear<<endl;
		return  output;
	};
template <class Date>
istream& operator >> (istream& input,  Date& date1)
{
	input >>  date1;
	return input;
};
#endif 
Can you say what is it?

1
2
3
4
5
6
	ostream& operator << (ostream& output, Date& date1)
	{
		
		output << &Date::getMonth <<" "<<&Date::getDay<<","<<&Date::getYear<<endl;
		return  output;
	};
it's supposed to be the class template for the output stream.

I tried to implement Thumpers suggestion
1
2
3
4
5
6
// Global operator<< overload:
std::ostream &operator<<(std::ostream &os, const Data &d)
{
    os << "(" << d.x << ", " << d.y << ")";
    return os;
}



I have to admit I have hacked around until the syntax got cleared of errors.

Here is what I think it does:
1
2
3
4
5
6
7
8
9
//output stream      << (  stream     Name, class    object)
ostream&  operator<< (ostream& output, Date& date1)
	{
		//name << Class::function << space << Class::function
		output << &Date::getMonth <<" "<<&Date::getDay
             //<<comma<<Class::function
               <<","<<&Date::getYear<<endl;
		return  output;
	};
Last edited on
Topic archived. No new replies allowed.