C++ Class Inheritance Extend public from base class

Description

C++ Class Inheritance Extend public from base class

 
#include <iostream>
#include <vector>
#include <string>
 
using std::string;
 
class Person//from  w w w . j  a v a2  s  .  com
{
protected:
  int age {};                                       // Age in years
  string name;
  char gender {'f'};                                   // 'm' or 'f'
public:
  Person() = default;                                  // Default constructor
  Person(int theAge, string theName, char theGender);
  void who() const;                                    // Display details
};
 
class Employee : public Person
{
protected:
  long personnelNumber {};
 
public:
  Employee() = default;                          // Default constructor - necessary to define arrays
  Employee(int theAge, string theName, char theGender, long persNum) :
                                                 Person {theAge, theName, theGender}, personnelNumber {persNum} {}
  void who() const;                              // Display details
 
};
 
class Executive: public Employee
{
public:
  Executive() = default;                         // Default constructor - necessary to define arrays
  Executive(int theAge, string theName, char theGender, long persNum) :
                                                 Employee {theAge, theName, theGender, persNum} {}
  void who() const;                              // Display details
};
 
Person::Person(int theAge, string theName, char theGender) : age {theAge}, name {theName}, gender {theGender}
{
}
 
// Display details of Person object
void Person::who() const
{
  std::cout << "\nThis is " << name << " who is " << age << " years old." << std::endl;
}
 
// Display details of Employee object
void Employee::who() const
{
  std::cout << name << " is a " << (gender=='f'? "female": "male") << " employee aged " << age << "." << std::endl;
}
 
// Display details of Executive object
void Executive::who() const
{
  std::cout << name << " is a " << (gender == 'f' ? "female" : "male") << " executive." << std::endl;
}
 
int main()
{
  std::vector<Employee> employees {
                                    Employee(22, "R", 'm', 3),
                                    Employee(22, "A", 'f', 4),
                                    Employee(26, "P", 'm', 5),
                                    Employee(27, "S", 'f', 6),
                                    Employee(25, "J", 'm', 7)
                                  };
 
  for(auto& employee : employees)
    employee.who();
 
  std::cout << std::endl;
 
  std::vector<Executive> executives {
                                      Executive(24, "I", 'm', 3),
                                      Executive(22, "A", 'f', 4),
                                      Executive(22, "S", 'm', 5),
                                      Executive(23, "S", 'f', 6),
                                      Executive(29, "M", 'f', 7)
                                    };
 
  for (auto& executive : executives)
  {
    executive.who();
    executive.Employee::who();
  }
}



PreviousNext

Related