question on placement of function

in class I am working on inheritance with classes. I have created a function to display the state of an object but I am having trouble figuring out were to place the function. I think the function should be placed in my Employee.cpp file because it is the parent class of the other classes but went I call the function in the source it says the function in not defined. Also when I declare the function in the source it does not have access to the memberfunctions of the employee class. any help would be greatly appreciated

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
  // decleration in Emplyee.cpp

void Employee::displayEmployee(Employee* emp)
{
	cout << "First name: " << emp->fName << endl;
	cout << "Last name: " << emp->lName << endl;
	cout << "SSN: " << emp->ssn << endl;
	cout << "Phone number: " << emp->phone << endl;
	cout << "Weekly pay: " << emp->calculatePay() << endl;

	Hourly* hry = dynamic_cast<Hourly*>(emp);    // try to convert Employee parent object to a Hourly child object
	if (hry != NULL)                               // if the hry is not NULL, then we have a Hourly object!
	{
		cout << "Hours: $" << hry->getHours() << endl;
		cout << "Pay rate: $" << hry->getRate() << endl;
	}

	Manager* mgr = dynamic_cast<Manager*>(emp);    // try to convert Employee parent object to a Manager child object
	if (mgr != NULL)                               // if the mgr is not NULL, then we have a Manager object!
	{
		cout << "Bonus: $" << mgr->getBonus() << endl;
	}

	Salary* sal = dynamic_cast<Salary*>(emp);    // try to convert Employee parent object to a Salary child object
	if (sal != NULL)                               // if the sal is not NULL, then we have a Salary object!
	{
		cout << "Annual salary: $" << sal->getAnnualSalary() << endl;
	}
}
//getters and setters in Employee.h
	//getters and setters
	virtual float calculatePay();
	virtual string toString();
	void displayEmployee(Employee* emp);
};
// source.cpp
#include <iostream>
#include <string>
#include <conio.h>
#include "Employee.h"
#include "Hourly.h"
#include "Salary.h"
#include "Manager.h"
using namespace std;


int main()
{
	//create three objects using each class
	Hourly emp1("bob", "smith", "123456789", "5553243", 40.0f, 12.50f);
	Salary emp2("shannon", "ray", "785426258", "5552685", 52000.00);
	Manager emp3("fred", "hope", "846258456", "5558486", 60000.00, 10000.00);

	//display the size of the Hourly employee
	cout << "The size of the hourly employee is: " <<  sizeof(emp1) << endl;
	cout << " The size of the pointer to the hourly employee is: " << sizeof(&emp1) << endl;

	displayEmployee(emp1);

	cout << "Press any key to continue...";
	_getch();

	return 0;

}

You need to include the #include "Employee.h" header in the Employee.cpp file at the top. Also, full code will help us figure it out better.
Last edited on
I have included the Employee.h header, I am by no means an expert and I appreciate your time. I will post the rest of the code here

Employee.cpp
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
#include "Employee.h"
#include "Manager.h"
#include "Hourly.h"
#include <string>
#include <iostream>

Employee::Employee()
{
	fName = "Unknown";
	lName = "Unknown";
	ssn = "Unknown";
	phone = "Unknown";
}

Employee::Employee(string fName, string lName, string ssn, string phone)
{
	if (fName.length() > 0)
		this->fName = fName;
	else
		this->fName = "Unknown";

	if (lName.length() > 0)
		this->lName = lName;
	else
		this->lName = "Unknown";

	if (ssn.length() > 0)
		this->ssn = ssn;
	else
		this->ssn = "Unknown";

	if (phone.length() > 0)
		this->phone = phone;
	else
		this->phone = "Unknown";
}

Employee::~Employee()
{
}

 float Employee::calculatePay()
{
	return 0.0f;
}

string Employee::toString()
{
	return "First name: " + fName + ", Last name: " + lName + ", SSN: " + ssn + "Phone number: " + phone;
}

void Employee::displayEmployee(Employee* emp)
{
	cout << "First name: " << emp->fName << endl;
	cout << "Last name: " << emp->lName << endl;
	cout << "SSN: " << emp->ssn << endl;
	cout << "Phone number: " << emp->phone << endl;
	cout << "Weekly pay: " << emp->calculatePay() << endl;

	Hourly* hry = dynamic_cast<Hourly*>(emp);    // try to convert Employee parent object to a Hourly child object
	if (hry != NULL)                               // if the hry is not NULL, then we have a Hourly object!
	{
		cout << "Hours: $" << hry->getHours() << endl;
		cout << "Pay rate: $" << hry->getRate() << endl;
	}

	Manager* mgr = dynamic_cast<Manager*>(emp);    // try to convert Employee parent object to a Manager child object
	if (mgr != NULL)                               // if the mgr is not NULL, then we have a Manager object!
	{
		cout << "Bonus: $" << mgr->getBonus() << endl;
	}

	Salary* sal = dynamic_cast<Salary*>(emp);    // try to convert Employee parent object to a Salary child object
	if (sal != NULL)                               // if the sal is not NULL, then we have a Salary object!
	{
		cout << "Annual salary: $" << sal->getAnnualSalary() << endl;
	}
}


Employee.h
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
#pragma once
#include <string>
using namespace std;

class Employee
{

protected:
	//attributes

	string fName;
	string lName;
	string ssn;
	string phone;

public:
	// constructors and destructor
	Employee();

	Employee(string fName, string lName, string ssn, string phone);

	~Employee();

	//getters and setters
	virtual float calculatePay();
	virtual string toString();
	void displayEmployee(Employee* emp);
};


source.cpp
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
#include <iostream>
#include <string>
#include <conio.h>
#include "Employee.h"
#include "Hourly.h"
#include "Salary.h"
#include "Manager.h"
using namespace std;


int main()
{
	//create three objects using each class
	Hourly emp1("bob", "smith", "123456789", "5553243", 40.0f, 12.50f);
	Salary emp2("shannon", "ray", "785426258", "5552685", 52000.00);
	Manager emp3("fred", "hope", "846258456", "5558486", 60000.00, 10000.00);

	//display the size of the Hourly employee
	cout << "The size of the hourly employee is: " <<  sizeof(emp1) << endl;
	cout << " The size of the pointer to the hourly employee is: " << sizeof(&emp1) << endl;

	displayEmployee(emp1);

	cout << "Press any key to continue...";
	_getch();

	return 0;

}


Hourly.cpp
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
#include "Hourly.h"

Hourly::Hourly()
{
	hours = 0.0f;
	rate = 0.0f;
}

Hourly::Hourly(string fName, string lName, string ssn, string phone, float hours, float rate)
{
	if (hours > 0.0f)
		this->hours = hours;
	else 
		this->hours = 0.0f;
	if (rate > 0.0f)
		this->rate = rate;
	else
		this->rate = 0.0f;
}

Hourly::~Hourly()
{
}

float Hourly::calculatePay()
{
	if (hours > 0 && hours <= 40)
		return hours * rate;
	if (hours > 40)
		return (rate * 40) + (hours - 40)* (rate * 1.5);
}
string Hourly::toString()
{
	string ptrRate;
	ptrRate = to_string(rate);
	return ptrRate;
}
float Hourly::getHours()
{
	return hours;
}
float Hourly::getRate()
{
	return rate;
}


Hourly.h
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
#pragma once
#include "Employee.h"

class Hourly :
	public Employee
{
private:

	//attributes
	float hours;
	float rate;

public:

	// constructors and destructor
	Hourly();
	Hourly(string fName, string lName, string ssn, string phone, float hours, float rate);
	~Hourly();

	//behaviors

	float calculatePay();
	string toString();
	float getHours();
	float getRate();
};


Salary.cpp
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
#include "Salary.h"

Salary::Salary()
{
	annualSalary = 0;
}

Salary::Salary(string fName, string lName, string ssn, string phone, double annualSalary)
{
	if (annualSalary > 0)
		this->annualSalary = annualSalary;
	else
		this->annualSalary = 0;
}

Salary::~Salary()
{
}

float Salary::calculatePay()
{
	return annualSalary / 52;
}
string Salary::toString()
{
	string ptrAnnualSalary;
	ptrAnnualSalary = to_string(annualSalary);
	return ptrAnnualSalary;
}

double Salary::getAnnualSalary()
{
	return annualSalary;
}


Salary.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#include "Employee.h"

class Salary :
	public Employee
{
protected: 

	//attributes
	double annualSalary;

public:
	//constructors and destructor
	Salary();
	Salary(string fName, string lName, string ssn, string phone, double annualSalary);
	~Salary();

	float calculatePay();
	string toString();
	double getAnnualSalary();
};


Manager.cpp
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
#include "Manager.h"

Manager::Manager()
{
	bonus = 0;
}

Manager::Manager(string fName, string lName, string ssn, string phone, double annualSalary, double bonus)
{
	if (bonus > 0)
		this->bonus = bonus;
	else
		this->bonus = 0;
}

Manager::~Manager()
{
}

float Manager::calculatePay()
{
	return (annualSalary + bonus) / 52;
}
string Manager::toString()
{
	string ptrBonus;
	ptrBonus = to_string(bonus);
	return ptrBonus;
}

double Manager::getBonus()
{
	return bonus;
}


Manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include "Salary.h"

class Manager :
	public Salary
{
private:
	//attributes
	double bonus;

public:
	Manager();
	Manager(string fName, string lName, string ssn, string phone, double annualSalary, double bonus);
	~Manager();

	//behaviors
	float calculatePay();
	string toString();
	double getBonus();
};
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
#include <iostream>
#include <string>
#include <conio.h>
#include "Employee.h"
#include "Hourly.h"
#include "Salary.h"
#include "Manager.h"
using namespace std;


int main()
{
	//create three objects using each class
	Hourly emp1("bob", "smith", "123456789", "5553243", 40.0f, 12.50f);
	Salary emp2("shannon", "ray", "785426258", "5552685", 52000.00);
	Manager emp3("fred", "hope", "846258456", "5558486", 60000.00, 10000.00);

	//display the size of the Hourly employee
	cout << "The size of the hourly employee is: " <<  sizeof(emp1) << endl;
	cout << " The size of the pointer to the hourly employee is: " << sizeof(&emp1) << endl;

	displayEmployee(emp1);

	cout << "Press any key to continue...";
	_getch();

	return 0;

}


the function displayEmployee is a class member, which means you need to use the class object to call it, such as emp1.displayEmployee(). its already a member of the Employee class so you dont need to pass the object into it. Whatever classes inherit from Employee will be able to use this function to display information.
Last edited on
Here is the fixed code:

main.cpp

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
#include <iostream>
#include <string>
#include <conio.h>
#include "Employee.h"
#include "Hourly.h"
#include "Salary.h"
#include "Manager.h"
using namespace std;


int main()
{
	//create three objects using each class
	Hourly emp1("bob", "smith", "123456789", "5553243", 40.0f, 12.50f);
	Salary emp2("shannon", "ray", "785426258", "5552685", 52000.00);
	Manager emp3("fred", "hope", "846258456", "5558486", 60000.00, 10000.00);

	//display the size of the Hourly employee
	cout << "The size of the hourly employee is: " << sizeof(emp1) << endl;
	cout << " The size of the pointer to the hourly employee is: " << sizeof(&emp1) << endl;

	emp1.displayEmployee();

	cout << "Press any key to continue...";
	_getch();

	return 0;

}


Manager.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include "Salary.h"

class Manager :
	public Salary
{
private:
	//attributes
	double bonus;

public:
	Manager();
	Manager(string fName, string lName, string ssn, string phone, double annualSalary, double bonus);
	~Manager();

	//behaviors
	float calculatePay();
	string toString();
	double getBonus();
};


Manager.cpp

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
#include "Manager.h"

Manager::Manager()
{
	bonus = 0;
}

Manager::Manager(string fName, string lName, string ssn, string phone, double annualSalary, double bonus)
{
	if (bonus > 0)
		this->bonus = bonus;
	else
		this->bonus = 0;
}

Manager::~Manager()
{
}

float Manager::calculatePay()
{
	return (annualSalary + bonus) / 52;
}
string Manager::toString()
{
	string ptrBonus;
	ptrBonus = to_string(bonus);
	return ptrBonus;
}

double Manager::getBonus()
{
	return bonus;
}



Salary.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include "Employee.h"

class Salary :
	public Employee
{
protected:

	//attributes
	double annualSalary;

public:
	//constructors and destructor
	Salary();
	Salary(string fName, string lName, string ssn, string phone, double annualSalary);
	~Salary();

	float calculatePay();
	string toString();
	double getAnnualSalary();
};



Salary.cpp

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
#include "Salary.h"

Salary::Salary()
{
	annualSalary = 0;
}

Salary::Salary(string fName, string lName, string ssn, string phone, double annualSalary)
{
	if (annualSalary > 0)
		this->annualSalary = annualSalary;
	else
		this->annualSalary = 0;
}

Salary::~Salary()
{
}

float Salary::calculatePay()
{
	return annualSalary / 52;
}
string Salary::toString()
{
	string ptrAnnualSalary;
	ptrAnnualSalary = to_string(annualSalary);
	return ptrAnnualSalary;
}

double Salary::getAnnualSalary()
{
	return annualSalary;
}


Hourly.h

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
#pragma once
#include <string>

#include "Employee.h"

using std::string;

class Hourly :
	public Employee
{
private:

	//attributes
	float hours;
	float rate;

public:

	// constructors and destructor
	Hourly() = default;
	Hourly(string fName, string lName, string ssn, string phone, float hours, float rate):
		hours(hours), rate(rate),
		Employee(fName, lName, ssn, phone){}

	//behaviors

	float calculatePay();
	string toString();
	float getHours();
	float getRate();
};


Hourly.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Hourly.h"


float Hourly::calculatePay()
{
	if (hours > 0 && hours <= 40)
		return hours * rate;
	if (hours > 40)
		return (rate * 40) + (hours - 40) * (rate * 1.5);
}
string Hourly::toString()
{
	string ptrRate;
	ptrRate = to_string(rate);
	return ptrRate;
}
float Hourly::getHours()
{
	return hours;
}
float Hourly::getRate()
{
	return rate;
}


Employee.h

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
#pragma once

#include <string>
using namespace std;

class Employee
{

public:
	// constructors and destructor

	Employee(string& fName, string& lName, string& ssn, string& phone): fName(fName), lName(lName), ssn(ssn), phone(phone){}
	Employee() = default;

	//getters and setters
	virtual float calculatePay();
	void displayEmployee();

protected:
	//attributes

	string fName;
	string lName;
	string ssn;
	string phone;
};


Employee.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Employee.h"
#include "Manager.h"
#include "Hourly.h"
#include <string>
#include <iostream>

float Employee::calculatePay()
{
	return 0.0f;
}

void Employee::displayEmployee()
{
	cout << "First name: " << fName << endl;
	cout << "Last name: " << lName << endl;
	cout << "SSN: " << ssn << endl;
	cout << "Phone number: " << phone << endl;
	cout << "Weekly pay: " << calculatePay() << endl;
}


To display the information for the other employees you need to do what i did in the Hourly.h class. Also when initializing variables in a class you should do : Employee(string& fName, string& lName, string& ssn, string& phone): fName(fName), lName(lName), ssn(ssn), phone(phone) instead of :
1
2
3
4
5
6
7
Employee::Employee()
{
	fName = "Unknown";
	lName = "Unknown";
	ssn = "Unknown";
	phone = "Unknown";
}


Also for outputting class object info, you could overload the bit shift operator << like so:

This is from my project

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ostream>

...

ostream& operator<< (ostream& os, Character& character)
{
	os << "Class Name: " << character.GetClassName() << endl;
	os << "Health: " << character.GetHealth() << endl;
	os << "Max Health: " << character.GetMaxHealth() << endl;
	os << "Attack Power: " << character.GetAttackPwr() << endl;
	os << "Max Attack Power: " << character.GetMaxAttackPower() << endl;

	return os;
}


and then you can simply do:

1
2
cout << emp1 << endl;
cout << emp2 << endl;

etc.
Last edited on
Topic archived. No new replies allowed.