Inheritance Problem

Hey all, I am new the C++ language and come from a Java background. I have three C++ files (main.cpp , Person.cpp and Employee.cpp). Header files also exist respectively with constructor and function prototypes for Person.cpp and Employee.cpp. Person has a constructor that initializes its variables and a function which outputs the variables to the screen, Employee Inherits from Person. My question is, is there a way for Employee to pass its constructor arguments into the Base class constructor to initialize them that way? Or am I talking non-sense. Any help would be appreciated :)

Person.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
 #ifndef InheritanceEx_Person_h
#define InheritanceEx_Person_h

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

class Person{
    
private:
    string name;
    int age;
    string address;
    float salary;

public:
    Person(string n, int a, string addr, float s);
    ~Person();
    
    void displayPerson();
};


#endif


Person.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Person.h"

Person::Person(string n, int a, string addr, float s): name(n), age(a), address(addr), salary(s)
{
}

Person::~Person()
{
}

void Person::displayPerson()
{
    cout << "Name: " << name << "\nAge: " << age << "\nAddress: " << address << "\nSalary: " << salary << endl;
}


Employee.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Employee.h"

Employee::Employee()  // Can I initialize derived class members here with
                      // with the Base class constructor?
{
}

Employee::~Employee()
{
}



Employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef InheritanceEx_Employee_h
#define InheritanceEx_Employee_h

#include "Person.h"

class Employee: public Person{
    
public:
    Employee();
    ~Employee();
};


#endif
Last edited on
Yes.

I assume Employ's constructor should have the same parameters as Person. If that's the case, the constructor looks like:
1
2
3
4
Employee::Employee(string n, int a, string addr, float s) :
    Person(n, a, addr, s)
{
}
Last edited on
You assume right :) Thanks very much! Great help.
Topic archived. No new replies allowed.