Programming and Graphics

(Kiana) #1

6.14 Inheritance 185


appears both in the main function and class implementation.


The union of the source files, header files, and the makefile constitutes a
project.


Problem


6.13.1. Split the algebra class and main program discussed in Section 6.8 into
separate files.


6.14 Inheritance


In C++, we can generate a hierarchy of derived classes that inherit the at-
tributes and functions of their ancestors and are endowed with added features.
In this way, the class of equilateral triangles can be derived from the class of
all triangles, and the class of roses can be derived from the class of flowers.
The class of flowers is the base-class or super-class, and the class of roses is the
derived class.


A derived class inherits all functions of the base class except for its con-
structor and destructor, the members of the assignation (=) class operator, and
the inherited function friends. A cynic defines friends as people with common
enemies.


As an example, we consider the class of all flowers available in a flower
shop, defined by the type (annual or perennial), color, and price. The flower
class definition is:


#include <iostream>
using namespace std;

//--- FLOWER CLASS DEFINITION

class flower
{
public:
flower(); // default constructor
flower(string, string, float); // parametered constructor
string gettype() const;
string getcolor() const;
float getprice() const;
void print() const;
protected:
string type;
string color;
float price;
};
Free download pdf