Practice Programs

Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.

  1. Write a program that uses the class SalariedEmployee in Display 15.5. Your program is to define a class called Administrator, which is to be derived from the class SalariedEmployee. You are allowed to change private in the base class to protected. You are to supply the following additional data and function members:

    • A member variable of type string that contains the administrator’s title (such as Director or Vice President).

    • A member variable of type string that contains the company area of responsibility (such as Production, Accounting, or Personnel).

    • A member variable of type string that contains the name of this administrator’s immediate supervisor.

    • A protected: member variable of type double that holds the administrator’s annual salary. It is possible for you to use the existing salary member if you did the change recommended earlier.

    • A member function called setSupervisor, which changes the supervisor name.

    • A member function for reading in an administrator’s data from the keyboard.

    • A member function called print, which outputs the object’s data to the screen.

    • An overloading of the member function printCheck() with appropriate notations on the check.

  2. Add temporary, administrative, permanent, and other classifications of employee to the hierarchy from Displays 15.2, 15.4, and 15.5. Implement and test this hierarchy. Test all member functions. A user interface with a menu would be a nice touch for your test program.

  3. Listed below are definitions of two classes that use inheritance, code for their implementation, and a main function. Put the code into appropriate files with the necessary include statements and preprocessor statements so that the program compiles and runs. It should output “Circle has radius 2 and area 12.5664”.

    class Shape
    {
    public:
          Shape();
          Shape(string name);
          string getName(); void setName(string newName); virtual double getArea() = 0;
    private:
          string name;
    };
    Shape::Shape()
    {
          name="";
    }
    Shape::Shape(string name)
    {
          this->name = name;
    }
    string Shape::getName()
    {
          return this->name;
    }
     void Shape::setName(string newName)
    {
          this->name = newName;
    }
    class Circle : public Shape
    {
    public:
          Circle();
          Circle(int theRadius);
          void setRadius(int newRadius);
          double getRadius();
          virtual double getArea();
    private:
          int radius;
    };
    Circle::Circle() : Shape("Circle"), radius(0)
    { }
    Circle::Circle(int theRadius) : Shape("Circle"),
                   radius(theRadius)
    { }
    void Circle::setRadius(int newRadius)
    {
          this->radius = newRadius;
    }
    double Circle::getRadius()
    {
    return radius;
    }
    double Circle::getArea()
    {
    return 3.14159 * radius * radius;
    }
    int main()
    {
       Circle c(2);
       cout << c.getName() << " has radius " <<
          c.getRadius() << " and area " <<
          c.getArea() << endl;
       return 0;
    }

    Add another class, Rectangle, that is also derived from the Shape class. Modify the Rectangle class appropriately so it has private width and height variables, a constructor that allows the user to set the width and height, functions to retrieve the width and height, and an appropriately defined getArea function that calculates the area of the rectangle.

    The following code added to main should output “Rectangle has width 3 has height 4 and area 12.0”.

    Rectangle r(3,4);
    cout << r.getName() << " has width " <<
          r.getWidth() << " has height " <<
          r.getHeight() << " and area " <<
          r.getArea() << endl;