Class composition help getting error.

Date.h actually validate and display the date.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Date.h
#ifndef DATE_H
#define DATE_H

class Date
{
public:
	static const int monthOfYear=12;
	Date(int=1, int=1, int=1900);
	void print () const;
	~Date();
private:
	int day;
	int month;
	int year;
	int checkDay (int ) const;
};
#endif 


Class Date member function defined below
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
//Date.cpp
#include <iostream>
#include <stdexcept>
#include "Date.h"
using namespace std;

Date::Date(int dy, int mn, int yr)
{
	if(mn>0 && mn <=monthOfYear)
		month = mn;
	else 
		throw invalid_argument (" month must be 1-12 " );
	year= yr;

	day = checkDay(dy);
	cout << "constructor for the class Date giving value of";
	print();
	cout << endl;
}

void Date::print () const
{
	cout << day << '/' << month << '/' << year;
}
Date::~Date ()
{
	cout << "destructor for the class Date ";
	print ();
	cout << endl;
}

int Date::checkDay (int testDay) const
{
	static const int daysOfMonths [monthOfYear + 1] = {0,31,28,31,30,31,30,31,30,31,30,31,30};

	if ( testDay>0 && testDay <= daysOfMonths[month] )
		return testDay;
	if ( month == 2 && testDay == 29 && (year % 4 )==0 )
		return testDay;
	throw invalid_argument ("given day is invakid" );
}


The below is another class Employee in which the above class is a host. The class shows the first and last name and takes date from above class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
#include "Date.h"
using namespace std;

class Employee
{
public:
	Employee ( const string &, const string &, const Date &, const Date &);
	void print () const;
	~Employee ();
private:
	string fname;
	string lname;
	const Date birthdate;
	const Date hiredate;
};
#endif 

Class Employee Definition is below
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
//Empolyee.cpp
#include <iostream>
#include "Date.h"
#include "Employee.h"

using namespace std;

Employee::Employee(const string &firstname, const string &lastname, const Date &DateOfBirth, const Date DateOfHire )
	: fname (firstname),
	lname (lastname),
	birthdate (DateOfBirth),
	hiredate (DateOfHire)
{
	cout << "constructor of class employee " << fname << " " << lname << endl;
}
void Employee::print () const 
{
	cout << lname << " " << fname << " Birthday ";
	birthdate.print();
	cout << " Hired ";
	hiredate.print ();
	cout << endl;
}

Employee::~Employee ()
{
	cout << " Distructor of employee class object "<< lname << " " << fname << endl;
}


and finally the main function is below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//three.cpp
#include <iostream>
#include <conio.h>
#include "Date.h"
#include "Employee.h"
using namespace std;

int main()
{
	Date birth (11,12,1910);
	Date hire (14,3,1950);

	Employee man ("arslan","afzal", birth, hire );

	cout << endl;
	man.print ();

	getch();
	return 0;
}


I'm getting error at Employee.cpp at line 8
Error: no instance of overloaded function Employee::Employee matches the specified type.
Any help will be appreciated.
The last date parameter in your cpp file is passed by value whereas the prototype in the header file is passed by reference.
iHutch105 thank you very much, I appreciate it that you looked my code to find the problem, Otherwise i was thinking who will check so long codes.
Thanks a lot again, you are great. God bless you.
Last edited on
Topic archived. No new replies allowed.