Inheritance

Process of creating a new class from an existing class.

 

New class is know as the Derived, or Child or Subclass.

 

Existing class from which it was derived from is know as the Base, or Parent or Superclass.

 

The most important feature of inheritance is that derived class is a type of the base class - the emphasis being on type!

 

 

 

*Note: In UML (Unified Modelling Language), Inheritance is shown as a large blank arrow head

 

Promotes code reuse and reduces code size.

 

Allows us to identify some shared members between objects.

 

Overriding Parent methods for specific Child behaviour.

 

Identify commonality, put into Parent, then let Child classes inherit.

 

This technique implements what is referred to as the "Is-A" relationship.

e.g.

a car is a vehicle

a Ferrari is a car

Therefore a Ferrari is also a vehicle

 

 

A class inherits from another by using a colon : in the declaration of the derived class, like so:

 

class childClass : public parentClass {

... code goes here...

}

 

Notice the use of the keyword public in the declaration, this instructs the compiler that the public members of the parent will be inherited by the child.

 

Similarly, the keywords private or protected could have been used to inherit to those levels.

 

The Child class inherits the Parent's methods as follows:

Access by: public private protected
Same class Yes Yes Yes
Derived class Yes Yes No
External class Yes No No

 

Public inheritance provides the Parent's Public and Protected methods to the Child (as its own Public and Protected methods) but not its Private methods, these should be accessed using calls to the Parent's Public methods.

 

Protected inheritance provides the Parent's Public and Protected members as Protected members in the Child.

 

Private inheritance provides the Parent's Public and Protected members as Private members in the Child.

 

Here, the two subclasses (Ducati and Yamaha) use the Parent Motorbike class. This Parent class is never called directly, but just acts to provide similar qualities (members) to the derived classes, in this case the ability to set the int speed. The Child classes then use their own methods unique for their own behaviour, in this case their public methods of getSpeed and saySpeed (just using different names to show they are different):

#include <iostream>
using namespace std;

//define a Parent Abstract class - this is not called directly, but used by the derived classes
class Motorbike {
	protected:
		int speed; //declare an attribute to be inherited
	public:
		void setSpeed(int a) { //define the public interface
			speed = a ;
		}
};

class Ducati: public Motorbike { //define a Child Concrete class
	public:
		void getSpeed()
			{cout << "Here's a Ducati Panigale Motorbike going: " << speed << "mph..."<< endl ; }
};

class Yamaha: public Motorbike { //define a Child Concrete class
	public:
		void saySpeed()
			{cout << "This Yamaha Enduro Motorbike goes: " << speed << "mph, off road!" << endl ; }
};

int main() {
	Ducati panigale ; //declare an instance of the (ADT) class Ducati, called panigale
	panigale.setSpeed(129); //use the public method inherited from the Motorbike Parent Abstract class
	panigale.getSpeed(); //use the public method in the Child to display the previously set value

	Yamaha enduro ;
	enduro.setSpeed(79);
	enduro.saySpeed();

	return 0;
}

Compile & Run:

Here's a Ducati Panigale Motorbike going: 129mph...
This Yamaha Enduro Motorbike goes: 79mph, off road!

 

 

C++ also allows multiple inheritance, by providing a comma separated list of the classes to be inherited. This is an extremely simple example, just to show multiple classes being inherited on line 20:

#include <iostream>
#include <string>
using namespace std;

//define a Parent Abstract class - this is not called directly, but used by the derived classes
class Motorbike {
	protected:
		int speed; //declare an attribute to be inherited
	public:
		void setSpeed(int a) { //define the public interface
			speed = a ;
		}
};

class paint {
	protected:
		string colour ="red";
};

class Ducati: public Motorbike, protected paint {
	public:
		void getSpeed()
			{cout << "Here's a " << colour << " Ducati Panigale going: " << speed << "mph..."<< endl ; }
};

int main() {
	Ducati panigale ; //declare an instance of the (ADT) class Ducati, called panigale
	panigale.setSpeed(129); //use the public method inherited from the Motorbike Parent Abstract class
	panigale.getSpeed(); //use the public method in the Child to display the previously set value

	return 0;
}

Compile & Run:

Here's a red Ducati Panigale Motorbike going: 129mph...

Leave a Reply