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 :)
#ifndef InheritanceEx_Person_h
#define InheritanceEx_Person_h
#include <string>
#include <iostream>
usingnamespace 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