One of the most important conecpts in object oriented programming is that of inheritance.Inheritance allows us to define a class int terms of another class,which makes it easier to create and maintain an applicatrion.This also provides an opportunity to resue code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class.This existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the is a relationship.For example, mammal Is-A animal,monkey Is-A mammal hence monkey Is-A animal .And as well as and so on.
Base & Derived classes:
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. T o define a derived class we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:
class derived-class:access-specifier base-class
where access specifier is one public,protected or private, and base-class is the name of a previously defined class.If the access-specifier is not used, then it is private by default.
consider a base class Shape and it s derived class Rectangle as follows:
#include<iostream> using namespace std; //Base class class Shape { public: void setWidth(int w) { width=w; } void setHeight(int h) { height=h; } protected: int width; int height; }; //Derived class class Rectangle:public Shape { public: int getArea() { return (width*height); } }; int main(void) { Rectangle rect; rect.setWidth(5); rect.setHeight(7); //Print the area of the object cout<<"Total area: "<<rect.getArea()<<endl; return 0; }
Output:
Access Control and Inheritance:
A derived class can access all the non-private members of its base class.Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
We can summarize the different access types according to who can accesss them in the following way:
Access Public Protected Private
Same Class yes yes yes
Derived classes yes yes no
Outside classes yes no no
A derived class inherits all base class method with the following exceptions:
– Constructors, destructors and copy constructors of the base class.
– Overloaded operators of the base class.
– The friend function of the base class.
Type of Inheritance:
When deriving a class from a base class the base class may be inherited through public,protected or private inheritance.The type of inheritance is specified by the access-specifier as explained above.
We hardly use protected or private inheritance but public inheritance is commonly used.While using different type of inheritance following rules are applied:
Public Inheritance: When deriving a class freom a public base class,public members of the base class become public members of the derived class and protected members of the base class become the protected members of the derived class .A base class private members are never accessible directly from a derived class, but can be acccessible through calls to the public and protected members of the base class.
Protected Inheritanc:When deriving from a protected base class,public and protected members of the base class become protectecd members of the derived class.
Private Inheritance:When deriving from a private base class,public and protected members of the base class become private members of the derived class.
Muktiple Inheritances:
A C++ class can inherit members from more than one class and here is the exrended syntax:
class derived-class:access basA,access baseB,access baseC……
Here the example goes:
#include<iostream> using namespace std; //Base class Shape class Shape{ public: void setWidth(int w) { width=w; } void setHeight(int h) { height=h; } protected: int width; int height; }; //Base class PaintCost class PaintCost { public: int getCost(int area) { return area*70; } }; class Rectangle: public Shape,public PaintCost { public: int getArea() { return (width*height); } }; int main(void) { Rectangle rect; int area; rect.setWidth(5); rect.setHeight(7); area=rect.getArea(); //Print the area of the object. cout<<"Total area: "<<rect.getArea()<<endl; //print the total cost of painitng cout<<"Total pait cost: $"<<rect.getCost(area)<<endl; return 0; }