Inheritance
Inheritance
Module II
Inheritance
• Inheritance is the process of creating new classes from existing
classes.
• Base class
• The existing class from which new classes derived.
• Derived class
• Class that inherits data members and member functions from a previously
defined base class
• The derived class inherits all the capabilities of the base class but can
add properties of its own.
• The base class is unchanged by this process.
Inheritance
class A{…..};
class B:public A
{….
……};
Multiple Inheritance
• Derivation of a class from
several classes(two or more)
known as multiple inheritance
class A{…..};
class B{…..};
class c:public A, private B
{….
……};
Hierarchical Inheritance
Derivation of several classes from a
single class
class A{…..};
class B:public A
{…..};
class c:public A
{….
……};
class D:public A
{….
……};
Multilevel inheritance
• Derivation of a class from
another derived class is known
as multilevel inheritance
class A{…..};
class B:public A
{….
……};
class c:public B
{….
……};
Hybrid Inheritance
• Derivation of a class involving
multiple forms of inheritance is
known as hybrid inheritance
class A{…..};
class B:public A
{….};
class C{……};
class Scientist {…
char name[40];
public:
virtual void show_name();
virtual void show_all();
…};
class Physicist : public Scientist{ ...
char field[40];
public:
void show_all(); // redefined
virtual void show_field(); // new
...
};
How Virtual Functions Work
Abstract Classes and Pure Virtual Functions
• It is possible that you want to include a virtual function in a base
class so that it may be redefined in a derived class to suit the
objects of that class, but that there is no meaningful definition you
could give for the function in the base class.
• A pure virtual function is a virtual function that has no definition
within the base class.
• To declare a pure virtual function, use this general form:
virtual type func-name(parameter-list) = 0;
• When a virtual function is made pure, any derived class must provide its
own definition.
• If the derived class fails to override the pure virtual function, a compile-
time error will result.
Abstract class
Properties of Pure virtual function
Pure virtual function int main()
#include <iostream>
using namespace std; {
class Base { //base class // Base bad; //can’t make object from abstract
class
public: Base* arr[2]; //array of pointers to base class
virtual void show() =0; // pure virtual function Derv1 dv1; //object of derived class 1
}; Derv2 dv2; //object of derived class 2
class Derv1 : public Base { //derived class 1 arr[0] = &dv1; //put address of dv1 in array
public: arr[1] = &dv2; //put address of dv2 in array
void show() arr[0]->show(); //execute show() in both objects
{ cout << “Derv1\n”; } arr[1]->show();
}; return 0;
class Derv2 : public Base { //derived class 2
public:
void show() }
{ cout << “Derv2\n”; }
};
Virtual Destructors
• Deleting a derived class object using a pointer to a base class
that has a non-virtual destructor results in undefined behavior.
• To correct this situation, the base class should be defined with
a virtual destructor.
Example
#include <iostream>
using namespace std; Constructing A
class A { Constructing B
public: Destructing A
A() { cout << "Constructing A\n"; }
~A() { cout << "Destructing A\n"; }
};
class B : public A {
public:
B() { cout << "Constructing B\n"; }
~B() { cout << "Destructing B\n"; }
};
int main(){
A *ptr=new B();
delete ptr;
// B obj1;
return 0;}
Virtual destructor
#include <iostream>
Constructing A
using namespace std;
Constructing B
class A {
Destructing B
public:
Destructing A
A() { cout << "Constructing A\n"; }
virtual ~A() { cout << "Destructing A\n"; }
Process returned 0 (0x0) execution time :
}; 5.203 s
class B : public A { Press any key to continue.
public:
B() { cout << "Constructing B\n"; }
~B() { cout << "Destructing B\n"; }
};
int main(){
A *ptr=new B;
delete ptr;
return 0;}
Rules for Virtual function
Rules for Virtual function