Help with Parent Child class, dynamic_cast

I have a lab for my object oriented programming class and I am hung up at the end trying to get the correct output. I have 3 objects from the Hourly, Salary, and Manager classes. Problem is that everything displays great except that the Manager object takes the sal->getAnnualSalary() and the other Salary objects and displays them at the end of the Manager object, but before the Salary object. What am I missing? There is a lot of code because of the multiple classes, so I will just include my 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
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


//libraries
#include<string>
#include<iostream>
#include<conio.h>
#include"Hourly.h"
#include"Salary.h"
#include"Manager.h"
using namespace std;
//display employee method
void displayEmployee(Employee* emp);

int main(void)  //entry into main application
{
	//Create 3 objects, one from each class
	Salary sal("Bob", "Smith", "222112222", "3024567829", 60000);
	Hourly hour("Joe", "Johnson", "111221111", "8563332222", 41.00, 16.00);
	Manager mgr("Billy", "Gunz", "123456789", "9874563210", 75000, 5000);

	cout << "Size of Hourly Employee object: " << sizeof(hour) << endl;
	cout << "Size of pointer to Hourly object: " << sizeof(&hour) << endl;
	cout << endl;

	displayEmployee(&mgr);   //call display functions and send object address
	displayEmployee(&sal);
	displayEmployee(&hour);
	
	

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

	return 0;
}

void displayEmployee(Employee* emp)
{
	cout << "First Name: " << emp->getFName() << endl;
	cout << "Last Name: " << emp->getLName() << endl;
	cout << "SSN : " << emp->getSSN() << endl;
	cout << "Phone: " << emp->getPhone() << endl;

	Manager* mgr = dynamic_cast<Manager*>(emp);
	if (mgr != NULL)
	{
		cout << "Salary $: " << mgr->getAnnualSalary() << endl;
		cout << "Bonus: $" << mgr->getBonus() << endl;
		cout << "Paycheck: $" << mgr->calculatePay() << endl;
		
	}
	
	Salary* sal = dynamic_cast<Salary*>(emp);
	if (sal != NULL)
	{
		cout << "Salary: $" << sal->getAnnualSalary() << endl;
		cout << "Paycheck: $" << sal->calculatePay() << endl;
	}
	Hourly* hour = dynamic_cast<Hourly*>(emp);
	if (hour != NULL)
	{
		cout << "Hours: " << hour->getHours() << endl;
		cout << "Rate: $" << hour->getRate() << endl;
		cout << "Paycheck: $" << hour->calculatePay() << endl;
	}

	
	

		
		
	

}
	

	
Last edited on
What is "Employee"? Is it the base class for each type of employee?

Wouldn't that display function be better off as a class member function? Then each type of "Employee" have it's own display function.

You're really not showing enough content, you need to show the class definitions at the very least.

You probably shouldn't use dynamic_cast here, which only works if Employee is polymorphic (has at least one virtual function) anyway. Consider:

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
#include <iostream>

class Employee {
    std::string name;
public:
    Employee(std::string nm) : name(nm) { }
    void print() {
        std::cout << name << '\n';
    }
};

class Manager : public Employee {
    int bonus;
public:
    Manager(std::string nm, int b) : Employee(nm), bonus(b) {}
    void print() {
        Employee::print();
        std::cout << "bonus: " << bonus << '\n';
    }
};

int main() {
    Manager mgr("bob", 42);
    mgr.print();
}

Last edited on
I'll include the entire code, but to answer the first question; yes Employee is the base class for all of the others. Salary and Hourly are derived and Manager is derived from the Salary class.
The instructions for the assignment want us to use dynamic cast, otherwise I would probably just use a toString() or something similar.

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#pragma once

#include<string>
#include<iostream>
using namespace std;

class Employee
{
//attributes
protected:
	string fName;
	string lName;
	string SSN;
	string phone;
public:
	//Constructors and Descructor
	Employee();


	Employee(string fName, string lName, string SSN, string phone);
	virtual~Employee();

	//behaviors
	float calculatePay();
	virtual string toString(void);

	//accessors and mutators
	string getFName(void);
	void setFName(string fName);

	string getLName(void);
	void setLName(string lName);
	
	string getSSN(void);
	void setSSN(string SSN);

	string getPhone(void);
	void setPhone(string phone);


};

#include "Employee.h"



//Constructors and Descructor
Employee::Employee()
{
	fName = "unknown";
	lName = "unknown";
	SSN = "unknown";
	phone = "unkown";
}
Employee::Employee(string fName, string lName, string SSN, string phone)
{
	setFName(fName);
	setLName(lName);
	setSSN(SSN);
	setPhone(phone);
}
Employee::~Employee()
{
	
}

//behaviors
//Method calculates pay
float Employee::calculatePay() //return 0.0f
{
	float pay = 0.0f;
	return pay;
}
//Method displays employee information
string Employee::toString(void)
{
	return "First Name: " + fName + "\nLast Name: " + lName + "\nSSN: "
		+ SSN + "\nPhone Number: " + phone;
}

//accessors and mutators
string Employee::getFName(void)
{
	return fName;
}
void Employee::setFName(string fName)
{
	this->fName = fName;		
}

string Employee::getLName(void)
{
	return lName;
}
void Employee::setLName(string lName)
{
	this->lName = lName;
}

string Employee::getSSN(void)
{
	return SSN;
}
void Employee::setSSN(string SSN)
{
	this->SSN = SSN;
}

string Employee::getPhone(void)
{
	return phone;
}
void Employee::setPhone(string phone)
{
	this->phone = phone;
}

#include "Employee.h"



//Constructors and Descructor
Employee::Employee()
{
	fName = "unknown";
	lName = "unknown";
	SSN = "unknown";
	phone = "unkown";
}
Employee::Employee(string fName, string lName, string SSN, string phone)
{
	setFName(fName);
	setLName(lName);
	setSSN(SSN);
	setPhone(phone);
}
Employee::~Employee()
{
	
}

//behaviors
//Method calculates pay
float Employee::calculatePay() //return 0.0f
{
	float pay = 0.0f;
	return pay;
}
//Method displays employee information
string Employee::toString(void)
{
	return "First Name: " + fName + "\nLast Name: " + lName + "\nSSN: "
		+ SSN + "\nPhone Number: " + phone;
}

//accessors and mutators
string Employee::getFName(void)
{
	return fName;
}
void Employee::setFName(string fName)
{
	this->fName = fName;		
}

string Employee::getLName(void)
{
	return lName;
}
void Employee::setLName(string lName)
{
	this->lName = lName;
}

string Employee::getSSN(void)
{
	return SSN;
}
void Employee::setSSN(string SSN)
{
	this->SSN = SSN;
}

string Employee::getPhone(void)
{
	return phone;
}
void Employee::setPhone(string phone)
{
	this->phone = phone;
}

#include "Hourly.h"



Hourly::Hourly()
{
	rate = 0;
	hours = 0;
}

Hourly::Hourly(string fName, string lName, string SSN, string phone, float hours, float rate)
	: Employee(fName, lName, SSN, phone)
{
	setHours(hours);
	setRate(rate);
}


Hourly::~Hourly()
{
}

//behaviors
float Hourly::calculatePay()
{
	float pay = 0.0f;
	float OT = hours - 40.0f;
	float OTPay;
	float OTRate = rate * 1.5f;
	
	if (OT > 0.0f)
		OTPay = OT * OTRate;
	else
		OTPay = 0.0f;

	pay = (hours * rate) + OTPay;

	return pay;
}
string  Hourly::toString()
{
	return Employee::toString() + ", Hours: " + to_string(hours) + ", Rate: " + to_string(rate) + "Paycheck: " + to_string(calculatePay());
}

//accessors and mutators
float  Hourly::getHours(void)
{
	return hours;
}
void  Hourly::setHours(float hours)
{
	if (hours > 0)
		this->hours = hours;
	else
		this->hours = 0;

}

float  Hourly::getRate(void)
{
	return rate;
}
void  Hourly::setRate(float rate)
{
	if (rate > 0)
		this->rate = rate;
	else
		this->rate = 0;
}

#pragma once
#include<string>
#include"Employee.h"
using namespace std;

class Salary : public Employee
{
private:

protected:
	double annualSalary;

public:
	Salary(string fName, string lName, string SSN, string phone, double annualSalary);
	Salary();
	~Salary();

	float calculatePay();
	string toString(void);

	double getAnnualSalary(void);
	void setAnnualSalary(double annualSalary);
};


#include "Salary.h"



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

Salary::Salary(string fName, string lName, string SSN, string phone, double annualSalary)
	: Employee(fName, lName, SSN, phone)
{
	setAnnualSalary(annualSalary);
}


Salary::~Salary()
{
}

float Salary::calculatePay()
{
	float pay = annualSalary / 52.0f;

	return pay;
		
}
string Salary::toString(void)
{
	return Employee::toString() + ", Salary: " + to_string(annualSalary) + ", Paycheck: " + to_string(calculatePay());
}

double Salary::getAnnualSalary(void)
{
	return annualSalary;
}
void Salary::setAnnualSalary(double annualSalary)
{
	if (annualSalary > 0)
		this->annualSalary = annualSalary;
	else
		this->annualSalary = 0;
}


#pragma once
#include"Salary.h"

class Manager : public Salary
{
private:
	double bonus;
public:
	Manager();
	Manager(string fName, string lName, string SSN, string phone, double annualSalary, double bonus);
	~Manager();

	float calculatePay();
	string toString();

	double getBonus(void);
	void setBonus(double bonus);
};


#include "Manager.h"



Manager::Manager()
{
}

Manager::Manager(string fName, string lName, string SSN, string phone, double annualSalary, double bonus)
	: Salary(fName, lName, SSN, phone, annualSalary)
{
	setBonus(bonus);
}


Manager::~Manager()
{
}

float Manager::calculatePay()
{
	float pay = (bonus + annualSalary) / 52.0f;

	return pay;
}
string Manager::toString()
{
	return Salary::toString() + ", Salary: " + to_string(annualSalary) + ", Bonus: " + to_string(bonus)
		+ ", Paycheck: " + to_string(calculatePay());
}

double Manager::getBonus(void)
{
	return bonus;
}
void Manager::setBonus(double bonus)
{
	if (bonus > 0)
		this->bonus = bonus;
	else
		this->bonus = 0;
}


/*
Christopher Brookens
CIS247C
WK5 Lab5
11/26/19
*/

//libraries
#include<string>
#include<iostream>
#include<conio.h>
#include"Hourly.h"
#include"Salary.h"
#include"Manager.h"
using namespace std;
//display employee method
void displayEmployee(Employee* emp);

int main(void)  //entry into main application
{
	//Create 3 objects, one from each class
	Salary sal("Bob", "Smith", "222112222", "3024567829", 60000);
	Hourly hour("Joe", "Johnson", "111221111", "8563332222", 41.00, 16.00);
	Manager mgr("Billy", "Gunz", "123456789", "9874563210", 75000, 5000);

	cout << "Size of Hourly Employee object: " << sizeof(hour) << endl;
	cout << "Size of pointer to Hourly object: " << sizeof(&hour) << endl;
	cout << endl;

	displayEmployee(&mgr);   //call display functions and send object address
	displayEmployee(&sal);
	displayEmployee(&hour);
	
	

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

	return 0;
}

void displayEmployee(Employee* emp)
{
	cout << "First Name: " << emp->getFName() << endl;
	cout << "Last Name: " << emp->getLName() << endl;
	cout << "SSN : " << emp->getSSN() << endl;
	cout << "Phone: " << emp->getPhone() << endl;

	Manager* mgr = dynamic_cast<Manager*>(emp);
	if (mgr != NULL)
	{
		cout << "Salary $: " << mgr->getAnnualSalary() << endl;
		cout << "Bonus: $" << mgr->getBonus() << endl;
		cout << "Paycheck: $" << mgr->calculatePay() << endl;
		
	}
	
	Salary* sal = dynamic_cast<Salary*>(emp);
	if (sal != NULL)
	{
		cout << "Salary: $" << sal->getAnnualSalary() << endl;
		cout << "Paycheck: $" << sal->calculatePay() << endl;
	}
	Hourly* hour = dynamic_cast<Hourly*>(emp);
	if (hour != NULL)
	{
		cout << "Hours: " << hour->getHours() << endl;
		cout << "Rate: $" << hour->getRate() << endl;
		cout << "Paycheck: $" << hour->calculatePay() << endl;
	}

	
}
	

	
Let us know if your problem persist:

Employee.hpp:
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
#ifndef EMPLOYEE_HPP
#define EMPLOYEE_HPP

#include <iostream>
#include <string>


class Employee {
public:
    Employee() = default;
    Employee( std::string f_name_arg,
              std::string l_name_arg,
              std::string ssn_arg,
              std::string phone_arg );

    virtual double calculatePay() = 0;
    virtual std::string toString();

    std::string getFName() const;
    void setFName(const std::string& f_name_arg);

    std::string getLName() const;
    void setLName(const std::string& l_name_arg);

    std::string getSsn() const;
    void setSsn(const std::string& ssn_arg);

    std::string getPhone() const;
    void setPhone(const std::string& phone_arg);

protected:
    std::string f_name { "unknown" };
    std::string l_name { "unknown" };
    std::string ssn    { "unknown" };
    std::string phone  { "unknown" };

    virtual ~Employee() = default;
};


#endif // EMPLOYEE_HPP 


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
#include "Employee.hpp"


Employee::Employee( std::string f_name_arg,
                    std::string l_name_arg,
                    std::string ssn_arg,
                    std::string phone_arg )
{
    setFName(f_name_arg);
    setLName(l_name_arg);
    setSsn(ssn_arg);
    setPhone(phone_arg);
}


std::string Employee::toString()
{
    return "First Name: " + f_name + "\nLast Name: " + l_name + "\nssn: "
           + ssn + "\nPhone Number: " + phone;
}



std::string Employee::getFName() const
{
    return f_name;
}


void Employee::setFName(const std::string& f_name_arg)
{
    f_name = f_name_arg;
}


std::string Employee::getLName() const
{
    return l_name;
}


void Employee::setLName(const std::string& l_name_arg)
{
    l_name = l_name_arg;
}


std::string Employee::getSsn() const
{
    return ssn;
}


void Employee::setSsn(const std::string& ssn_arg)
{
    ssn = ssn_arg;
}


std::string Employee::getPhone() const
{
    return phone;
}


void Employee::setPhone(const std::string& phone_arg)
{
    phone = phone_arg;
}


Hourly.hpp:
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
#ifndef HOURLY_HPP
#define HOURLY_HPP


#include "Employee.hpp"


class Hourly : public Employee {
public:
    Hourly() = default;
    Hourly( std::string f_name_arg,
            std::string l_name_arg,
            std::string ssn_arg,
            std::string phone_arg,
            double hours_arg = 0.0,
            double rate_arg = 0.0 );
    ~Hourly() = default;

    double calculatePay() override;
    std::string toString() override;

    double getHours() const;
    void setHours(double hours_arg = 0.0);
    double getRate() const;
    void setRate(double rate_arg = 0.0);

private:
    double rate {};
    double hours {};
};


#endif // HOURLY_HPP 


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Hourly.hpp"


Hourly::Hourly( std::string f_name_arg,
                std::string l_name_arg,
                std::string ssn_arg,
                std::string phone_arg,
                double hours_arg,
                double rate_arg )
    : Employee(f_name_arg, l_name_arg, ssn_arg, phone_arg)
{
    setHours(hours_arg);
    setRate(rate_arg);
}


//behaviors
double Hourly::calculatePay()
{
    double ot { hours - 40.0 };
    double ot_pay {};
    if (ot > 0.0) {
        ot_pay = ot * rate * 1.5;
    }

    return (hours * rate) + ot_pay;
}


std::string Hourly::toString()
{
    return Employee::toString() + ", Hours: " + std::to_string(hours)
                                + ", Rate: " + std::to_string(rate)
                                + "Paycheck: " + std::to_string(calculatePay());
}


double Hourly::getHours() const
{
    return hours;
}


void Hourly::setHours(double hours_arg)
{
    hours = hours_arg;
}


double Hourly::getRate() const
{
    return rate;
}


void Hourly::setRate(double rate_arg)
{
    rate = rate_arg;
}


Salary.hpp:
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
#ifndef SALARY_HPP
#define SALARY_HPP


#include "Employee.hpp"
#include <string>


class Salary : public Employee {
public:
    Salary() = default;
    Salary( std::string f_name_arg,
            std::string l_name_arg,
            std::string ssn_arg,
            std::string phone_arg,
            double annual_salary_arg = 0.0 );
    ~Salary() = default;

    double calculatePay() override;
    std::string toString() override;

    double getAnnualSalary() const;
    void setAnnualSalary(double annual_salary_arg = 0.0);

protected:
    double annual_salary {};
};


#endif // SALARY_HPP 


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
35
36
37
#include "Salary.hpp"


Salary::Salary( std::string f_name_arg,
                std::string l_name_arg,
                std::string ssn_arg,
                std::string phone_arg,
                double annual_salary_arg )
    : Employee(f_name_arg, l_name_arg, ssn_arg, phone_arg)
{
    setAnnualSalary(annual_salary_arg);
}


double Salary::calculatePay()
{
    return annual_salary / 52.0;
}


std::string Salary::toString()
{
    return Employee::toString() + ", Salary: " + std::to_string(annual_salary)
                                + ", Paycheck: " + std::to_string(calculatePay());
}


double Salary::getAnnualSalary() const
{
    return annual_salary;
}


void Salary::setAnnualSalary(double annual_salary_arg)
{
    annual_salary = annual_salary_arg;
}


Manager.hpp:
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
#ifndef MANAGER_HPP
#define MANAGER_HPP


#include "Salary.hpp"


class Manager : public Salary {
public:
    Manager() = default;
    Manager( std::string f_name_arg,
             std::string l_name_arg,
             std::string ssn_arg,
             std::string phone_arg,
             double annual_salary_arg = 0.0,
             double bonus_arg = 0.0 );
    ~Manager() = default;

    double calculatePay() override;
    std::string toString() override;

    double getBonus() const;
    void setBonus(double bonus_arg = 0.0);

private:
    double bonus {};
};


#endif // MANAGER_HPP 


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
35
36
37
38
39
#include "Manager.hpp"


Manager::Manager( std::string f_name_arg,
                  std::string l_name_arg,
                  std::string ssn_arg,
                  std::string phone_arg,
                  double annual_salary_arg,
                  double bonus_arg )
    : Salary(f_name_arg, l_name_arg, ssn_arg, phone_arg, annual_salary_arg)
{
    setBonus(bonus_arg);
}


double Manager::calculatePay()
{
    return (bonus + annual_salary) / 52.0;
}


std::string Manager::toString()
{
    return Salary::toString() + ", Salary: " + std::to_string(annual_salary)
                              + ", Bonus: " + std::to_string(bonus)
                              + ", Paycheck: " + std::to_string(calculatePay());
}


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


void Manager::setBonus(double bonus_arg)
{
    bonus = bonus_arg;
}


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
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
/*
    Christopher Brookens
    CIS247C
    WK5 Lab5
    11/26/19
*/
#include "Hourly.hpp"
#include "Salary.hpp"
#include "Manager.hpp"
#include <iostream>
#include <limits>
#include <string>


void displayEmployee(Employee* emp);
void waitForEnter();


int main()
{
    Salary sal("Bob", "Smith", "222112222", "3024567829", 60000);
    Hourly hour("Joe", "Johnson", "111221111", "8563332222", 41.00, 16.00);
    Manager mgr("Billy", "Gunz", "123456789", "9874563210", 75000, 5000);

    std::cout << "Size of Hourly Employee object: " << sizeof(hour) << '\n';
    std::cout << "Size of pointer to Hourly object: " << sizeof(&hour) << '\n';
    std::cout << '\n';

    std::cout << "\n### Displaying Manager ###\n";
    displayEmployee(&mgr);
    std::cout << "\n\n### Displaying Salary ###\n";
    displayEmployee(&sal);
    std::cout << "\n\n### Displaying Hourly ###\n";
    displayEmployee(&hour);

    waitForEnter();
    return 0;
}


void displayEmployee(Employee* emp)
{
    std::cout << "--- Employee *: ---\n" << emp->getFName()
              << "\nFirst Name: " << emp->getFName()
              << "\nLast Name: " << emp->getLName()
              << "\nssn : " << emp->getSsn()
              << "\nPhone: " << emp->getPhone() << '\n';

    Manager* mgr = dynamic_cast<Manager*>(emp);
    if (mgr != nullptr)
    {
        std::cout << "\n--- Manager *: ---\n" << emp->getFName()
                  << "\nSalary $: " << mgr->getAnnualSalary()
                  << "\nBonus: $" << mgr->getBonus()
                  << "\nPaycheck: $" << mgr->calculatePay() << '\n';
    }

    Salary* sal = dynamic_cast<Salary*>(emp);
    if (sal != nullptr)
    {
        std::cout << "\n--- Salary *:---\n" << emp->getFName()
                  << "\nSalary: $" << sal->getAnnualSalary()
                  << "\nPaycheck: $" << sal->calculatePay() << '\n';
    }

    Hourly* hour = dynamic_cast<Hourly*>(emp);
    if (hour != nullptr)
    {
        std::cout << "\n--- Hourly *: ---\n" << emp->getFName()
                  << "\nHours: " << hour->getHours()
                  << "\nRate: $" << hour->getRate()
                  << "\nPaycheck: $" << hour->calculatePay() << '\n';
    }
}


void waitForEnter()
{
    std::cout << "\n\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Output:
Size of Hourly Employee object: 152
Size of pointer to Hourly object: 8


### Displaying Manager ###
--- Employee *: ---
Billy
First Name: Billy
Last Name: Gunz
ssn : 123456789
Phone: 9874563210

--- Manager *: ---
Billy
Salary $: 75000
Bonus: $5000
Paycheck: $1538.46

--- Salary *:---
Billy
Salary: $75000
Paycheck: $1538.46


### Displaying Salary ###
--- Employee *: ---
Bob
First Name: Bob
Last Name: Smith
ssn : 222112222
Phone: 3024567829

--- Salary *:---
Bob
Salary: $60000
Paycheck: $1153.85


### Displaying Hourly ###
--- Employee *: ---
Joe
First Name: Joe
Last Name: Johnson
ssn : 111221111
Phone: 8563332222

--- Hourly *: ---
Joe
Hours: 41
Rate: $16
Paycheck: $680


Press ENTER to continue...

Topic archived. No new replies allowed.