Bonus Lab 4: Inheritance

Download

Class Inheritance

Inheritance makes software reuse possible. Rather than re-implementing common characteristics, a class inherits data members and member functions from its parent class.

Class derivation

In C++, inheritance is supported through a mechanism referred to as class derivation. The class that is inherited is called the base class and the new class is called the derived class. To derive a new class from a base class, we define the derived class as follows:

class B {
   // member functions and data members, etc.
   // ...
} ;

class D: public B {
   // member functions and data members, etc.
   // ...
} ;

Then B is a base class and D is a derived class of B.

Furthermore, if we have

class DD : public D {
   // ...
} ;

then DD is a (direct) derived class of D and an indirect derived class of B.

Access Control

The access specifier public may be replaced by protected or private. It specifies how the derived class inherits from its base class. We will come back to this later.

Let us first look at an example: derive.cpp

Member accessibility

In the above example, width and height are protected members of Polygon. They cannot be accessed by the main function. They can, however, be accessed by the member functions of its derived classes (e.g., Rectangle and Triangle).

Note: the constructors and destructor of the base class cannot be inherited to the derived class.

Construction and Destruction

Although constructors cannot be inherited, a constructor of the derived class can call a constructor of its base class in the member initialization list.

derived_class_constructor(parameters) : base_class_constructor(parameters) {
   // ...
} ;

The order of calling the constructors when an object of the derived class is created:

  1. base class constructor
  2. data member constructors (in the order specified in the class definition)
  3. derived class constructor

The order of calling the destructors is the reverse of that of calling the constructors. Let us look at an example: order.cpp

Substitution Principle

In class derivation, all non-private members of the base class are inherited and they can be accessed directly in the derived class. We say that every object of the derived class is an object of its base class. Wherever an object of the base class is used, an object of the derived class may also be used instead.

Here is an example: sub.cpp


Lab Task

In func.zip, you are provided a partially implemented inheritance hierarchy shown below:

Note: the implementation of class CosFunc is restricted to the functions of its parent (i.e., SinFunc) instead of the library cmath.

 

Your task for the lab assignment is to do the following:

  1. In tri_func.h and tri_func.cpp, complete the class CosFunc. You may refer to SinFunc to get some ideas (a useful reference link).
  2. In poly_func.cpp, implement PolyFunc::DerivativeAt(double x) in order to calculate the derivative of that polynomial function at x. You may read PolyFunc::EvaluateAt(double) too (a useful reference link).
  3. In impluse_func.cpp, complete ImpulseFunc::EvaluateAt(double).
  4. In impluse_func.h and impluse_func.cpp, override ImpulseFunc::IsDifferentiable(double).

Note: To test the equality of two float/double-type variables, you CANNOT use the equality (==) operator (e.g., d1 == d2). This is because float/double-type variables have numerical precision problems. Instead, you should first get the difference between them (double diff = d1 - d2;). If the difference is within a small range (-LIMIT < diff < LIMIT), you then treat them as equal. For this purpose, you should use EQUALITY_LIMIT defined in impulse_func.cpp.

Here is the expected output if your implementation is correct:

p
	name = poly
	is periodic = false
	value at 2 = 49
	is differentiable at 2 = true
	derivative at 2 = 72
-----------------------------------------------
s
	name = sin
	is periodic = true
	value at 0.785398 = 0.707107
	is differentiable at 0.785398 = true
	derivative at 0.785398 = 0.707107
-----------------------------------------------
c
	name = sin
	is periodic = true
	value at 1.5708 = 0
	is differentiable at 1.5708 = true
	derivative at 1.5708 = -2
-----------------------------------------------
i
	name = impulse
	is periodic = false
	value at 800 = 0
	is differentiable at 800 = true
	derivative at 800 = 0
i again
	name = impulse
	is periodic = false
	value at 10 = 7
	is differentiable at 10 = false