In-Class Exercise

Derived Class with a Resource


This exercise upgrades an inheritance hierarchy to manage objects that access resources through a derived class.


Given Information

The following code is a solution to the Handout on Functions in a Hierarchy.

Client Module

The client program is listed on the left and the output is shown on the right

 // Functions in a Hierarchy
 // h13.cpp

 #include <iostream>
 #include "Employee.h"

 int main() {
     Employee employee(1234, "Jane Doe", 15.50); 
     Person person("John Doe");

     employee.display(std::cout);
     std::cout << std::endl;
     person.display(std::cout);
     std::cout << std::endl;
 }










 Jane Doe (1234) $15.50

 John Doe


Employee Module

Employee.h

 // Functions in a Hierarchy
 // Employee.h

 #include <iostream>

 const int MAX_NAME = 20;

 class Person {
     char name[MAX_NAME + 1];
   public:
     Person();
     Person(const char*);
     void display(std::ostream&) const;
 };

 class Employee : public Person {
     int  no;
     double rate;
 public:
     Employee();
     Employee(int, const char*, double); 
     void display(std::ostream&) const;
 };

Employee.cpp

 // Functions in a Hierarchy
 // Employee.cpp

 #include <iomanip>
 #include <cstring>
 #include "Employee.h"

 Person::Person() {
     name[0] = '\0';
 }

 Person::Person(const char* n) {
     strncpy(name, n, MAX_NAME);
     name[MAX_NAME] = '\0';
 }

 void Person::display(std::ostream& os) const {
     if (name[0] != '\0')
         os << name << " ";
 }

 const int MIN_WAGE = 10.25;
 const int SAFE_NUM = 0;

 Employee::Employee() {
     // safe empty state
     no   = SAFE_NUM;
     rate = MIN_WAGE;
 }

 Employee::Employee(int n, const char* nm, double r) 
 : Person(nm)
 {
     if (n > SAFE_NUM && r >= MIN_WAGE) {
         no = n;
         rate = r;
     }
     else {  // safe empty state
         Employee temp;
         *this = temp;
     }
 }

 void Employee::display(std::ostream& os) const {
     if (no != SAFE_NUM) {
         Person::display(os);
         os << std::fixed << std::setprecision(2);
         os << "(" << no << ") $" << rate;
     }
     else
         os << "no data available";
 }

Your Task

Upgrade the Employee class by adding the name of the employee's department.  Assume no limit on the number of characters in that name. 

Client Module

The client program that uses your upgraded class is listed on the left and the results are listed on the right:

 // Derived Class with a Resource
 // h14.cpp

 #include <iostream>
 #include "Employee.h"

 int main() {
     Employee employee(1234, "Jane Doe", 15.50, 
      "Sales"); 
     Person person("John Doe");

     employee.display(std::cout);
     std::cout << std::endl;
     person.display(std::cout);
     std::cout << std::endl;
 }











 Jane Doe (1234) $15.50 Sales 

 John Doe


Employee Module

The Employee module accepts the address of a C-style string containing the department's name through the fourth parameter of a four-argument constructor.  The name of the department may be of any length.  You need to allocate dynamic memory for a copy of the string and define a copy constructor, assignment operator, and destructor for the derived class. 

Call the base class' copy constructor from the derived class' copy constructor and the base class' assignment operator from the derived class' assignment operator.  Finally, output the department name from the display() member function.

Employee.h

 // Derived Class with a Resource
 // Employee.h

 #include <iostream>

 const int MAX_NAME = 20;

 class Person {
     char name[MAX_NAME + 1];
   public:
     Person();
     Person(const char*);
     void display(std::ostream&) const;
 };

 class Employee : public Person {
     int  no;
     double rate;


 public:
     Employee();
     Employee(int, const char*, double,            ); 



     void display(std::ostream&) const;
 };

Employee.cpp

 // Derived Class with a Resource
 // Employee.cpp

 #include <iomanip>
 #include <cstring>
 #include "Employee.h"

 Person::Person() {
     name[0] = '\0';
 }

 Person::Person(const char* n) {
     strncpy(name, n, MAX_NAME);
     name[MAX_NAME] = '\0';
 }

 void Person::display(std::ostream& os) const {
     if (name[0] != '\0')
         os << name << " ";
 }

 const int MIN_WAGE = 10.25;
 const int SAFE_NUM = 0;

 Employee::Employee() {
     // safe empty state
     no   = SAFE_NUM;
     rate = MIN_WAGE;


 }

 Employee::Employee(int n, const char* nm, double r,              ) 
 : Person(nm)
 {
     if (n > SAFE_NUM && r >= MIN_WAGE) {
         no = n;
         rate = r;


     }
     else {  // safe empty state
         Employee temp;
         *this = temp;
     }
 }


















 void Employee::display(std::ostream& os) const {
     if (no != SAFE_NUM) {
         Person::display(os);
         os << std::fixed << std::setprecision(2);
         os << "(" << no << ") $" << rate;

     }
     else
         os << "no data available";
 }






  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License