Professional Documents
Culture Documents
Inheritance in C++
Inheritance in C++
Inheritance Concept
Polygon
class Rectangle{ private: int width, length; public: void set(int w, int l); int area(); } class Triangle{ private: int width, length; public: void set(int w, int l); int area(); }
Rectangle
Triangle
class Polygon { private: int width, length; public: void set(int w, int l); }
Inheritance Concept
Polygon
class Polygon { protected: int width, length; public: void set(int w, int l); } class Rectangle{ protected: int width, length; public: void set(int w, int l); int area(); }
Rectangle
Triangle
Inheritance Concept
Polygon
class Polygon { protected: int width, length; public: void set(int w, int l); } class Triangle{ protected: int width, length; public: void set(int w, int l); int area(); }
Rectangle
Triangle
Why Inheritance?
Reusability--building new components by utilising
existing components- is yet another important aspect of OO paradigm. It is always good/productive if we are able to reuse something that is already exists rather than creating the same all over again. Save time & money
Cond.
This mechanism of deriving a
new class from existing/old class is called inheritance. The old class is known as base class, super class or parent class; and the new class is known as sub class, derived class, or child class.
Parent
Inherited Capability
Child
Inheritance Concepts
Inheritance creates a hierarchy of related classes
(types) which share code and interface. This is also called a is a relationship:
A car is a vehicle
A dog is an animal
A teacher is a person
Inheritance Examples
Base Class Student Shape Derived Classes CommuterStudent ResidentStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan
Loan
TwoDimensionalShape
ThreeDimensionalShape
Circle
Square
Triangle
Sphere
Cube
Tetrahedron
What to inherit?
In principle, every member of a base class (data
Generialization
In UML, inheritance is called generalization
within its class and any class immediately derived from it.
derive from
Visibility mode
Private (default)
Public
Examples:
Public Inheritance
class A : public B { // Class A now inherits the members of Class B
} // with no change in the access specifier for // the inherited members
public base class (B) public members protected members private members
not inherited
Private Inheritance
class A : private B { // Class A now inherits the members of Class B // with public and protected members } // promoted to private
private base class (B) public members protected members private members
derived class (A) private private not inherited
Protected visibility
When a protected member is inherited in public
mode, it becomes protected in derived class. It is ready for further inheritance A Protected member, inherited in private mode, it becomes private in the derived class. But not further inherited.
Protected Inheritance
class A : protected B { // Class A now inherits the members of Class B // with public members promoted to protected } // but no other changes to the inherited members
protected base class (B) public members protected members private members
#include <iostream.h> class B { int a; public: int b; void get_ab(); int get_a(); void show_a(); }; class D: public B { int c; public: void mul(); void display(); };
void B::get_ab() { a=5; b=10; } int B::get_a() { return a; } void B::show_a() { cout<<"a="<<a<<endl; } void D::mul() { c=b*get_a(); } void D:: display() { cout <<"a="<<get_a()<<endl; cout<<"b="<<b<<endl; cout<<"c="<<c<<endl; }
void main() { D d; d.get_ab(); d. mul(); d.show_a(); d.display(); d.b =20; d.mul(); d.display(); } a=5 b=20 c=100 Output a=5 a=5 b=10 c=50
Forms of Inheritance:
The different forms of inheritance are:
Single inheritance (only one super class) Multiple inheritance (several super classes) Hierarchical inheritance (one super class, many sub
classes) Multi-Level inheritance (derived from a derived class) Hybrid inheritance (more than two types)
Multilevel Inheritance
Class A { .. }; Class B: public A { . }; Class C : public B { .. .. };
Class B-----intermediate class Chain ABC is known as inheritance path Process can be extended to any number of
levels.
Multiple Inheritance
A class can inherit the attributes of two or more classes.
A B C Class D: public A, public B { ----- } D
more than one base class. We can solve this problem by defining a named instance within the derived class, using the scope resolution operator with the function
Hierarchical Inheritance
Class A { .. }; Class B:public A { .. .. }; Class C:public A { . };
Hybrid Inheritance
There could be situations where we need to apply two or
test
Result
Multilevel Multiple
paths can be avoided by making the common base class as virtual base class. Class A { . }; Class B1:virtual public A { };
Abstract Classes
An abstract class is one that is not used to create
need to define constructor in derived class. If contains then there is a need to define ctor in derived class. Usually we create objects of derived classes, it make sense to pass arguments to the base class constructor. If both classes are having constructors, then the base constructor is executed first and then the constructor in the derived class is executed.
General Syntax
Derived-ctor (Arg1, n(argn)arg2,argn, argd): base1(arg1),base2(arg2),.basen(argn) { //body of derived constructor } D(int a1, int a2, int d1):B(a1,a2) { d=d1; }
Derived-class constructor
Calls the constructor for its base class first to initialize its
base-class members If the derived-class constructor is omitted, its default constructor calls the base-class default constructor
constructor calls.
Derived-class destructor is called before its base-class
destructor
ctor in derived
Class A { int a; public: A(){a=0;} void display() {cout<<a;} }; class B
Output is: 0
{int b; public: B(){b=5;} void display() {A::display(); cout<<b;}}; void main() {B b1; b1.display(); }
Output is: 05
Example 3
Class A { int a; public: A(){a=0;} A(int m) {a=m;} void display() {cout<<a;} }; class B
{int b; public: B(){b=5;} void display() {A::display(); cout<<b;}}; void main() {B b1; b1.display(); }
Output: 05
Example 4
Class A { int a; public: A(){a=0;} A(int m) {a=m;} void display() {cout<<a;} }; class B
{int b; Output is: 0 10 public: B(){b=5;} B(int n){b=n;} void display(){A::display(); cout<<b;}};
void main() {B b1(10); b1.display();
Example 5
Class A { int a; public: A(){a=0;} A(int m) {a=m;} void display() {cout<<a;} }; class B {int b; public: B(){b=5;} B(int m,int n):A(m),b(n){} void display(){A::display(); cout<<b;}}; void main() {B b1(10,20); b1.display(); }
class A { int a; public: A(int x) {a=x;} void show() {cout<<a;} }; class B {int b; public: B(int y) {b=y; }
void show() void main() { { cout<<b; C c1(4,5,6); }}; c1.A::show(); class C:public A, public B c1.B::show(); { c1.show(); int c; } public: C(int m, int n, int z):B(n),A(m),C(z) {cout<<objects initialized;} void show() {cout<<c;} };