Hierarchical Inheritance
Hierarchical Inheritance
HIERARCHICAL
INHERITANCE
class B
{ Key Concepts
int a;
public:
int b; What is Inhertiance
The mechanism of deriving a new class from an old one is
void get_ab();
called Inheritance
int get_a(void);
void show_a(void)
Inheritance and its goal
};
The key goal of inheritance is reusability , saving time, and
class D : public B
increse relibility and to create new classes using pre-
{
existing ones.
int c;
public: What are properties
void mul(void); As shown in the code snippet on the
void display(void); left hnd side....
}
class B
{ Key Concepts
int a;
public:
Base class and Derived Class
int b;
The old class is referred to as Base class and the new
void get_ab();
class formed upon it is called the Derived Class
int get_a(void);
void show_a(void)
}; Hierarchial Inheritance
When the traits of one class is derved by more than one
class D : public B
class its called Hierarchical Inheritance
{
int c;
public:
void mul(void);
void display(void);
}
Its quite Similar to Single
Inheritance!
Single Hierarchical
Inheritance Inheritance
The Importance of
Hierarchical Inheritance
The uniqueness of hierarchical Inheritance
comes from its ability to support the
hierarchical design of a program .
Where the features of one class are shared by
many classes on the level below!
Short-term
Medium -Term
Long-Term
Lets Look At A Simple Program
#include <iostream.h> class C : public A // Subclass C Output:
#include <conio.h> {
public: calling from B:
class A // Superclass A void show_C() class B
{ { class A
public: cout << "class C" << endl;
} calling from C:
void show_A()
}; class C
{ class A
cout << "class A" << endl; void main()
} { }
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}
};
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Find The Hidden Parts
#include <iostream.h>
class C : public A // Subclass C
#include <conio.h> n Output:
{
public:
calling from B:
class A // Superclass A void show_C()
class B
{ {
class A
public: cout << "class C" << endl;
void show_A() }
calling from C:
};
{ class C
cout << "class A" << endl; class A
void main()
} {
}
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}; }
Answer
#include <iostream.h> class C : public A // Subclass C Output:
#include <conio.h> n {
public: calling from B:
class A // Superclass A void show_C() class B
{ { class A
public: cout << "class C" << endl;
} calling from C:
void show_A()
}; class C
{ class A
cout << "class A" << endl; void main()
} { }
}; B b; // b is an object of class B
cout << "calling from B: " << endl;
class B : public A // Subclass B b.show_B();
b.show_A();
{
public: C c; // c is an object of class C
void show_B() cout << "calling from C: " << endl;
{ c.show_C();
cout << "class B" << endl; c.show_A();
}
}
};
The End
THANK YOU!