12.7. Exercises

1: In the tax() function on page 566, implement the isA() function for the Investment class hierarchy.
2:Study the following program and its output.
// fig.C - Circles and Squares with Figure base class 
#include <iostream.h>
#include "Figure.h"

int main()
{
   Circle c1(4);                  // define Circle
   Square s1(8);                  // define Square

   Circle c2(6);                  // define Circle
   Square s2(12);                 // define Square

   c1 += c2;                      // make Circle c1 bigger
   c1 += s2;                      // error

   s1 += s2;                      // make Square s1 bigger
   s1 += c2;                      // error

   cout << "c1 = " << c1 << endl;
   cout << "s1 = " << s1 << endl;
   return 0;
}

$ fig
Circle: right operand should be a Circle
Square: right operand should be a Square
c1 = 314.159 for a Circle
s1 = 400 for a Square

The program overloads operator+=() ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.