1

I guess I am doing something wrong here in the below code. I want to inherit the methods of class Person in class Employee.

#include<bits/stdc++.h>
using namespace std;

class Person{
    private:
        string name;
        int age;

    public:
        Person(string name, int age){ //Base parameterized constructor
            name = name;
            age = age;
        }
        void getName(){
            cout<<"Name: "<<name<<endl;
        }
        void getAge(){
            cout<<"Age: "<<age<<endl;
        }
};

class Employee: public Person{ //Default inheritance type is private
    private:
        int employeeID;
    public:
        Employee(string name, int age, int id) : Person(name, age){  //Derived parameterized constructor
            employeeID = id;
        }
        void getEmployeeDetails(){
            getName();
            getAge();
            cout<<"Employee ID: "<<employeeID<<endl;
        }
};

int main(){
    Employee* e = new Employee("John", 24, 14298);
    e->getEmployeeDetails();
    return 0;
}

I am getting the below output:

Name:

Age: 0

Employee ID: 14298

Please let me know what am I missing here. Any help would be appreciated!

Rahul Sharma
  • 358
  • 2
  • 10
  • Youre creating your Employee e on the heap with new but never delete it. This doesn't matter in this case because your program terminates right after but technically speaking it's a memory leak. You need to either call delete your Employee* once you don't need it anymore, use a smart pointer or create e on the Stack using Employee e("John", 24, 14298);. This is not related to your question but i think it's worth mentioning in case you didn't know. – Eric May 18 '20 at 12:14

2 Answers2

1

Nothing to do with inheritance, the code would have been wrong anyway.

This

    Person(string name, int age){ //Base parameterized constructor
        name = name;
        age = age;
    }

should be this

    Person(string n, int a){
        name = n;
        age = a;
    }

Because your constructor parameter declaration names are the same as your member variables, the member variables are hidden and you were just assigning the parameters to themselves.

A better way to write the same code is to use an initialiser list

    Person(string name, int age) : name(name), age(age)
    }

Initialiser lists have a couple of advantages, one of them is that there's no ambiguity, you can have the parameter names the same as the member variable names. The other (more important) is that in general, initialisation is more efficient than assignment.

john
  • 85,011
  • 4
  • 57
  • 81
1

The issue is not with inheritance, but with the fact that Person never initializes any of its fields.

This code:

    Person(string name, int age){ //Base parameterized constructor
        name = name;
        age = age;
    }

assigns local variable name to itself and same with age, because parameters shadow class member names. Member objects with the same name are never initialized.

Three solutions possible (listed in subjective order in which I prefer them):

  1. Use member initializer list to initialize your members:

    Person(string name, int age) : name{name}, age{age}
    { 
    }
    
  2. Use different names

    Person(string providedName, int providedAge)
    { 
        name = providedName;
        age = providedAge;
    }
    
  3. Use this to disambiguate objects

    Person(string name, int age){ //Base parameterized constructor
        this->name = name;
        this->age = age;
    }
    
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52