This is part of my homework due tomorrow and I am having difficulty understanding why the output is empty and then the code ends. I don't really understand the class pointer so it would help a lot if there is an explanation of how the class pointer affects the code. Why can I call employee_name using secretary and receptionist but I can't do it using emp1 or emp2. It would be great if someone can link me to videos where I can learn more on similar topics and better understand classes.
Header File
#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include <string>
class Employee{
public:
Employee(std::string n, double s);
std::string employee_name;
double salary;
};
class Department{
public:
Department(std::string n, Employee* s, Employee* r);
/*
set the receptionist to r; note that both receptionist and r are pointers
*/
void set_receptionist(Employee* r);
/*
set the secretary to s; note that both secretary and s are pointers
*/
void set_secretary(Employee* s);
/*
calculate the total salary and return it.
neglect the receptionist or secretary if it is nullptr.
count only once if receptionist and secretary point to the same employee (check their address instead of name!)
*/
double get_total_salary() const;
/*
display the department information, including department name, employees, and total salaries.
see details in main function.
neglect the receptionist or secretary if it is nullptr.
*/
void display_department() const;
private:
std::string department_name;
Employee* receptionist;
Employee* secretary;
};
#endif
Implementation File
Employee::Employee(std::string n, double s)
{
employee_name = n;
salary = s;
}
Department::Department(std::string n, Employee* s, Employee* r)
{
department_name = n;
}
void Department::set_receptionist(Employee* r)
{
receptionist = r;
}
void Department::set_secretary(Employee* s)
{
secretary = s;
}
double Department::get_total_salary() const
{
return 0.0;
}
void Department::display_department() const
{
cout << "department name: " << department_name << endl;
cout << "secretary name: " << secretary->employee_name << ", " << "salary: " << secretary->salary << endl;
cout << "receptionist name: " << receptionist->employee_name << ", " << "salary: " << receptionist->salary << endl;
//cout << "total salary: " << << endl;
}
Main File
int main(){
Employee emp1("Alice", 6000);
Employee emp2("Bob", 5500);
Department dep1("IT", &emp1, &emp2);
dep1.display_department();
/*
department name: IT
secretary name: Alice, salary: 6000
receptionist name: Bob, salary: 5500
total salary: 11500
*/
dep1.set_receptionist(&emp1);
dep1.display_department();
/*
department name: IT
secretary name: Alice, salary: 6000
receptionist name: Alice, salary: 6000
total salary: 6000
*/
dep1.set_secretary(nullptr);
dep1.display_department();
/*
department name: IT
receptionist name: Alice, salary: 6000
total salary: 6000
*/
}
The expected output is in the comment of the main file. I am trying to figure out display_department of the Employee class and I know that get_total_salary is incorrect.
Output:
department name: IT
secretary name:
It outputs this and then the program ends.
CodePudding user response:
Your problem is that you are not initialising or testing your pointers before you use them.
First you should set both pointers when you construct your department object (this doesn't happen automatically).
Department::Department(std::string n, Employee* s, Employee* r)
{
department_name = n;
secretary = s;
receptionist = r;
}
Then you should test if the pointer equals nullptr
before you try an use them to print the secretary or receptionist name.
void Department::display_department() const
{
cout << "department name: " << department_name << endl;
if (secretary == nullptr)
cout << "no secretary:" << endl;
else
cout << "secretary name: " << secretary->employee_name << ", " << "salary: " << secretary->salary << endl;
if (receptionist == nullptr)
cout << "no receptionist:" << endl;
else
cout << "receptionist name: " << receptionist->employee_name << ", " << "salary: " << receptionist->salary << endl;
//cout << "total salary: " << << endl;
}