More Inheritance and OOP

Our original implementation of the Employee class used character arrays to represent the private data members:
char firstName[MAX_LENGTH];  
char lastName[MAX_LENGTH];   
However, me could have used dynamically allocated memory for the arrays:
char* firstName;  
char* lastName;   
but for this example, we wanted to keep it very simple. Another option is to use an ADT for these data members. Specifically, we could have used our String class that we implemented earlier:
String firstName;  
String lastName;   
Using one object to represent the private data of another object is called composition or (containment.) We call it composition because one object is composed of another object (or one object contains another object.) Below is how our specification for the Employee class might look. Notice that we've overloaded the constructor and setName() methods as a convenience to the client. This allows the client to provide the first name and last name using either a character array or a String object. Of course, the implementation of the functions would be quite different.
#include "mystring.h"  // for the String class

#ifndef EMPLOYEE_H

  #define EMPLOYEE_H

  class Employee           
  {
    public:                
      Employee(const char* first, const char* last, 
               float salary, int years);

      Employee(const String& first, const String& last, 
               float salary, int years);

      void setName(const char* first, const char* last);
      void setName(const String& first, const String& last);
      void setSalary(float newSalary);
      void setYears(int numYears);
      void Display(void) const;

    private:               
      String firstName;  
      String lastName;   
      float salary;    
      int years;       
  };

#endif

Back to Outline