Error with inheritance

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//main
#include <string>
#include <cstdlib>
#include <iostream>
#include "Administrator.h"
#include "salariedEmployee.h"
using namespace std;
using namespace SavitchEmployees;
using SavitchEmployees::Administrator;
using SavitchEmployees::SalariedEmployee;

int main()
{
    //creating the admin object
    Administrator admin("Ethan", "12345", 4000);

    admin.readData();
    admin.print();
    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
29
  //employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using std::string;

namespace SavitchEmployees
{
    class Employee
    {
    public:
        Employee();
        Employee(const string &theName, const string &theSsn);
        string getName() const;
        string getSsn() const;
        double getNetpay() const;
        void setName(const string &newName);
        void setSsn(const 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
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
  //employee.cpp
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using std::cout;
using std::string;

namespace SavitchEmployees
{
    Employee::Employee() : name("No name yet"), ssn("No number yet"), netPay(0)
    {
        //left empty
    }

    Employee::Employee(const string &theName, const string &theNumber)
        : name(theName), ssn(theNumber), netPay(0)
    {
        //left empty
    }

    string Employee::getName() const
    {
        return name;
    }

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

    double Employee::getNetpay() const
    {
        return netPay;
    }

    void Employee::setName(const string &newName)
    {
        name = newName;
    }

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

    void Employee::setNetPay(double newNetPay)
    {
        netPay = newNetPay;
    }

    void Employee::printCheck() const
    {
        cout << "Error";
        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
  //salariedemployee.h
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H

#include <string>
#include "employee.h"

using std::string;

namespace SavitchEmployees
{
    class SalariedEmployee : public Employee
    {

    public:
        SalariedEmployee();
        SalariedEmployee(const string &theName, const 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  //salariedemployee.ccp
#include <iostream>
#include <string>
#include "salariedemployee.h"
using std::cout;
using std::endl;
using std::string;
namespace SavitchEmployees
{
    SalariedEmployee::SalariedEmployee() : Employee(), salary(0)
    {
        //deliberately empty
    }
    SalariedEmployee::SalariedEmployee(const string &theName,
                                       const string &theNumber,
                                       double theWeeklyPay)
        : Employee(theName, theNumber), salary(theWeeklyPay)
    {
        //deliberately empty
    }
    double SalariedEmployee::getSalary() const
    {
        return salary;
    }
    void SalariedEmployee::setSalary(double newSalary)
    {
        salary = newSalary;
    }

    void SalariedEmployee::printCheck()
    {
        setNetPay(salary);
        cout << "\n________________________________________________\n";
        cout << "Pay to the order of " << getName() << endl;
        cout << "The sum of " << getNetpay() << " Dollars\n";
        cout << "_________________________________________________\n";
        cout << "Check Stub NOT NEGOTIABLE \n";
        cout << "Employee Number: " << getSsn() << endl;

        cout << "Salaried Employee. Regular Pay: "
             << salary << endl;
        cout << "_________________________________________________\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
  //administrator.h
#ifndef ADMINISTRATOR_H
#define ADMINISTRATOR_H

#include <string>
#include "salariedEmployee.h"

using std::string;

namespace SavitchEmployees
{
    class Administrator : public SalariedEmployee
    {

        //constructors
    public:
        Administrator();
        Administrator(string theName, string theSsn, double theSalary);
        void setSupervisor(string newSupervisor);
        void readData();
        void print();
        void printCheck();

    protected:
        string title;
        string department;
        string supervisor;
        double annualSalary;
    }; //end
} //
#endif //ADMINISTRATOR_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
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
 //administrator.cpp
#include <string>
#include <cstdlib>
#include <iostream>
#include <string>
#include "Administrator.h"
using namespace std;

namespace SavitchEmployees
{
    Administrator::Administrator() : SalariedEmployee(), title("No title yet"), department("No department yet"), supervisor("No supervisor yet"), annualSalary(0)
    {
    }

    Administrator::Administrator(string theName, string theSsn, double theWeeklySalary)
        : SalariedEmployee(theName, theSsn, theWeeklySalary)
    {
    }

    void Administrator::setSupervisor(string newSupervisor)
    {
        supervisor = newSupervisor;
    }

    void Administrator::readData()
    {
        cout << "What is the administrator's title?: ";
        cin >> title;

        cout << "What is the administrators department?: ";
        cin >> department;

        cout << "Who is the administrators supervisor?: ";
        cin >> supervisor;

        cout << "What is the administrators annual salary?: ";
        cin >> annualSalary;
    }

    void Administrator::print()
    {
        setNetPay(salary);
        cout << "Employee name: " << getName() << "\n";
        cout << "Employee ssn: " << getSsn() << "\n";
        cout << "Employee netpay: " << getNetpay() << "\n";

        cout << "Administrator's title: " << title << "\n";
        cout << "Administrator's department: " << department << "\n";
        cout << "Administrator's supervisor: " << supervisor << "\n";
        cout << "Administrator's annual salary: " << annualSalary << "\n";
    }

    void Administrator::printCheck()
    {
        double monthPay = annualSalary / 12;
        setNetPay(monthPay);

        cout << "Pay check";

        cout << "Pay to " << getName() << "\n";
        cout << "A total of " << getNetpay() << "$"
             << "\n";
        cout << "Employee Number: " << getSsn() << "\n";
        cout << "Employee type: Administrator with title of: " << title << "\n";
        cout << "Department of: " << department << "\n";
    }

}


Here is the picture of the error that it gives: https://imgur.com/a/99sLt4Q

Something has to be wrong with either the administrator header or cpp file but I am at my wits end. If anyone can point me in the right direction that would be great.
You didn't link administrator.cpp (most likely)
Last edited on
I'm a complete newbie using Visual Studio Code. Honesty I'm a little sorry for the post, if I would have done more research on how linking files works with C++, then all of this would have been avoided.

For anyone else that might have this issue, I had better luck using codeblocks with all of my files in a single project instead of using Visual Code Studio that makes running multiple c++ files a hassle.

Thank you mbozzi for trying to help!
Last edited on
vs code is a poor tool. Try the real visual studio (2019 current) and it is much better about project management etc. It takes some getting used to maybe, but it makes sense once you understand that a top level build may have multiple projects (say, 3 dlls and 1 program) so you have an extra layer on simple programs (say for just the 1 program) that seems off at first.
Last edited on
Topic archived. No new replies allowed.