problem with inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//usingemployee.cpp
#include <iostream>
#include "salariedemployee.h"
#include "administrator.h"
using std::cout;
using std::endl;
using SavitchEmployees::SalariedEmployee;
using SavitchEmployees::Administrator;

int main()
{
	SalariedEmployee boss("Mr. Big Shot", "987-65-4321", 10500.50);
	cout << "Check for " << boss.getName() << endl;
	boss.printCheck();

	Administrator admin("Superaitaotao", "13-14-520", 1.00);
	admin.inputAdminData();
	admin.outputAdminData();
	admin.printCheck();

	return 0;
}


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
//employee. h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

using std::string;

namespace SavitchEmployees
{
	class Employee
	{
	public:
		Employee();
		Employee(string theName, string theSsn);
		string getName() const;
		string getSsn() const;
		double getNetPay() const;
		void setName(string newName);
		void setSsn(string newSsn);
		void setNetPay(double newNetPay);
		void printCheck() const;
	private:
		string name;
		string ssn;
		double netPay;
	};
}//SavitchEmployees

#endif //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
//salariedemployee.h
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H

#include "employee.h"

using std::string;

namespace SavitchEmployees
{
	class SalariedEmployee : public Employee
	{
	public:
		SalariedEmployee();
		SalariedEmployee(string theName, string theSsn, double theWeeklySalary);
		double getSalary() const;
		void setSalary(double newSalary);
		void printCheck();
	protected:
		double salary;//weekly
	};
}//SavitchEmployees

#endif //SALARIEDEMPLOYEE_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
//administrator.h
#ifndef ADMINISTRATOR_H
#define ADMINISTRATOR_H

#include <iostream>

#include "salariedemployee.h"

using std::string;

namespace SavitchEmployees
{
    class Administrator : public SalariedEmployee
    {
        public:
            Administrator();
            Administrator(string theName, string theSsn, double salary);
		    void inputAdminData();
		    void outputAdminData();
		    void printCheck();
        private:
            string adminTitle;//administrator's title
            string workingArea;//area of responsibility
	     	string supervisorName;//immediate supervisor
    };
}

#endif // ADMINISTRATOR_H_INCLUDED 

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
//employee.cpp
#include <cstdlib>
#include <iostream>
#include "employee.h"
using std::cout;

namespace SavitchEmployees
{
	Employee::Employee():name("No name yet"),ssn("No number yet"),netPay(0){}
	Employee::Employee(string theName, string theSsn):name(theName),ssn(theSsn),netPay(0){}
	string Employee::getName() const
	{
		return name;
	}
	string Employee::getSsn() const
	{
		return ssn;
	}
	double Employee::getNetPay() const
	{
		return netPay;
	}
	void Employee::setName(string newName)
	{
		name = newName;
	}
	void Employee::setSsn(string newSsn)
	{
		ssn = newSsn;
	}
	void Employee::setNetPay(double newNetPay)
	{
		netPay = newNetPay;
	}
	void Employee::printCheck() const
	{
		cout << "\nERROR: pringCheck function called for an \n" <<"Undifferentiated employee. Aborting the program!\n";
		exit(1);
	}
}//SavitchEmployees 

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
//salariedemployee.cpp
#include <iostream>
#include <string>
#include "salariedemployee.h"

using std::string;
using std::cout;
using std::endl;

namespace SavitchEmployees
{
	SalariedEmployee::SalariedEmployee():Employee(),salary(0)
	{
		//deliberately empty
	}
	SalariedEmployee::SalariedEmployee(string theName, string theSsn, double theWeeklySalary)
	  :Employee(theName, theSsn), salary(theWeeklySalary)
	{
		//deliberately empty
	}
	double SalariedEmployee::getSalary() const
	{
		return salary;
	}
	void SalariedEmployee::setSalary(double newSalary)
	{
		salary = newSalary;
	}
	void SalariedEmployee::printCheck()
	{
		setNetPay(salary);
		cout << "\n___________________________________\n"
			 << "Pay to the order of " << getName() << endl
			 << "The sum of" << getNetPay() << "Dollars\n"
			 << "______________________________________\n"
			 << "Check Stub Not negotiable \n"
			 << "Employee Number: " << getSsn() << endl
			 << "Salaried Employee. Regular Pay: "
			 << salary << endl
			 << "______________________________________\n";
	}
}//SavitchEmployees 


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
//administrator.cpp
#include <iostream>
#include <string>

#include "administrator.h"

using std::string;
using std::cin;
using std::cout;

namespace SavitchEmployees
{
    Administrator::Administrator( ):Employee(), salary(0)
    {
        //deliberately empty
    }

    Administrator::Administrator(string theName, string theSsn, double theAnnualSalary)
      :Employee(theName, theSsn),salary(theAnnualSalary)
    {
        //deliberately empty
    }

    void Administrator::inputAdminData()
    {
        cout << " Enter the details of the administrator " << getname() << endl;
        cout << " Enter the admin title" << endl;
        getline(cin, adminTitle);
        cout << " Enter the area of responsibility " << endl;
        getline(cin, workingArea);
        cout << " Enter the immediate supervisor's name " << endl;
        getline(cin, supervisorName);
    }

    void Administrator::outputAdminData()
    {
        cout << "Name: " << getname() << endl;
        cout << "Title: " << adminTitle << endl;
        cout << "Area of responsibility: " << workingArea << endl;
        cout << "Immediate supervisor: " << supervisorName << endl;
    }

    void Administrator::printCheck()
    {
        setNetPay(salary);
		cout << "\n___________________________________\n"
		cout << "Pay to the order of " << getName() << endl
		cout << "The sum of" << getNetPay() << "Dollars\n"
        cout << "______________________________________\n"
             << "Check Stub Not negotiable \n"
        cout << "Employee Number: " << getSsn() << endl
        cout << "Salaried Employee(Administrator). Regular Pay: "
			 << salary << endl
		cout << "______________________________________\n";
    }
}//SavitchEmployees 



||=== employee, Debug ===|
obj\Debug\usingemployee.o||In function `main':|
D:\C++\employee\usingemployee.cpp|16|undefined reference to `SavitchEmployees::Administrator::Administrator(std::string, std::string, double)'|
D:\C++\employee\usingemployee.cpp|17|undefined reference to `SavitchEmployees::Administrator::inputAdminData()'|
D:\C++\employee\usingemployee.cpp|18|undefined reference to `SavitchEmployees::Administrator::outputAdminData()'|
D:\C++\employee\usingemployee.cpp|19|undefined reference to `SavitchEmployees::Administrator::printCheck()'|
||=== Build finished: 4 errors, 0 warni[/center]ngs ===|


Can someone tell me what is wrong with this code?
I think it is because of my administrator class, but after reading the code for more than half an hour, I still dunt get it.
Sorry if the code is too long...
Thank you very much!!!
closed account (z05DSL3A)
Administrator inherits from SalariedEmployee [ class Administrator : public SalariedEmployee ], so the Constructor initialization list should be that of SalariedEmployee and not Employee.

edit:
There are some other issues with administrator.cpp,
missing: using std::endl;
getname() instead of getName()
printCheck() needs some ; anding the ends of some of the lines.

but generally administrator.cpp is not compiling so there is not object file for the linker to link to, hence the undefined reference to errors.
Last edited on
Thank you Grey Wolf!!! I am so grateful that you can point out all my mistakes. Just started to learn C++ last month. Thanks again!

1
2
3
4
5
6
7
8
9
10
   Administrator::Administrator( ):SalariedEmployee()
    {
        //deliberately empty
    }

    Administrator::Administrator(string theName, string theSsn, double theAnnualSalary)
      :SalariedEmployee(theName, theSsn, theAnnualSalary)
    {
        //deliberately empty
    }


How come after changing the two constructors, I still get the same errors?_?
closed account (z05DSL3A)
How are you building (compiling/linking) the code? Do you use an IDE or Command line tools? What tools to you use?

The likelihood is that for some reason compiling administrator.cpp is failing or possibly that linking to the object code of administrator.cpp is not setup.
Im using Code::block to build and I just got the problem solved. For some reasons, I didnt put my files inside the project file. After putting the files all in the file, the programme works well.

Thank you:)

But I cant understand why before adding the administrator file, it works well and does not have any linking problem. Previously the files were not in the project file as well.
Topic archived. No new replies allowed.