Unit IIInheritance T1742060295831
Unit IIInheritance T1742060295831
7hrs
Prof. Tahreem Shaikh
Content
1. Inheritance : Class hierarchy , Derived classes
2. Types of inheritance
3. Constructor and Destructor execution in inheritance
4. Ambiguity in Multiple Inheritance
5. Virtual Base Class
6. Abstract class
7. Friend Class
8. Nested Class
Inheritance
1. The classes can be reused in several ways .
2. Once class has been created and tested it can be adapted by other programmer.
This is done by Creating new classes ,reusing the properties of existing one.
the Mechanism of deriving new class from old one is called “Inheritance “
3. It save the time and money and also increase the reliability
Inheritance
• 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.
• Reuse the code functionality and fast
implementation time.
NOTE : All members of a class except Private, are
4
inherited
The old class is referred to as the base class and new class is called the derived
class.
Class hierarchy
A class hierarchy is a group of related classes that are connected through inheritance to do similar things
Example :
1. A base class is a class in Object-Oriented Programming language, from which other classes are derived.
2. The Base class members and member functions are inherited to Object of the derived class.
2. The derived class inherits all members and member functions of a base class.
3. The derived class can have more functionality with respect to the Base class and
//body of subclass
};
class Vehicle
{ Vehicle
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
}; Car
Int main()
{
Clrscr();
Car obj; // as soon as object created it call the Constructor of Vehicle & Car class
return 0;
}
Multilevel Inheritance
Inheritance is transitive so the last derived class acquires all the members of all its base
classes.
Animal
Dog
BabyDog Here BabyDog can access Dog and Animal class member
#include <iostream> class BabyDog : public Dog
{
using namespace std; public:
void weep()
class Animal {
{ cout<<"Weeping...";
public: }
void eat() };
{
cout<<"Eating..."<<endl; int main(void)
} {
}; BabyDog d1;
d1.eat();
class Dog: public Animal d1.bark();
{ d1.weep();
public: return 0;
void bark()
{ }
cout<<"Barking..."<<endl;
}
};
Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more
than base one classes.
i.e one sub class is inherited from more than TWO base classes.
Multiple Inheritance
Syntax:
class subclass_name : access_mode Base_class1, access_mode Base_class2, ….
{
//body of subclass
};
Vehicle Fourwheeler
Car
#include <iostream>
class C : public A, public B
using namespace std;
{
class A public:
{
protected: void display()
int a; {
public: cout << "The value of a is : " <<a<< endl;
void get_a(int n) cout << "The value of b is : " <<b<< endl;
{ cout<<"Addition of a and b is : "<<a+b;
a = n; }
};
}
};
class B int main()
{ {
protected: C c;
int b; c.get_a(10);
public: c.get_b(20);
void get_b(int n) c.display();
{
return 0;
b = n;
}
}
};
Example of Multiple Inheritance
2. Destructor is always called in the reverse order of the constructor. In C++, variables
and objects are allocated on the Stack.
3. The Stack follows the LIFO (Last-In-First-Out) pattern. So, the deallocation of memory
and destruction is always carried out in the reverse order of allocation and construction.
4. Destructor is a special class function which destroys the object as soon as the scope of
object ends
#include <iostream>
class C :public B
using namespace std;
class A {
{ public:
public: C()
A() {
{ cout<<"C's Constructor"<<endl;
cout<<"A's Constructor"<<endl; }
} ~C()
~A()
{
{
cout<<"A's Destructor"<<endl; cout<<"C's Destructor"<<endl;
} }
}; };
class B :public A int main()
{ {
public: C c;
B() return 0;
{
}
cout<<"B's Constructor"<<endl;
} A's Constructor
~B()
B's Constructor
{
cout<<"B's Destructor"<<endl;
C's Constructor
} C's Destructor
}; B's Destructor
A's Destructor
#include <iostream> class child : public parent
using namespace std; {
public:
class parent //parent class child() //constructor
{ {
public: cout << "Child class Constructor\n";
}
parent() //constructor
{ ~ child() //destructor
cout << "Parent class Constructor\n"; {
} cout << "Child class Destructor\n";
}
~parent()//destructor };
{
cout << "Parent class Destructor\n"; int main()
} {
//automatically executes both child and
}; parent class
//constructors and destructors because of
inheritance
child c;
return 0;
}
Ambiguity in Multiple Inheritance
1. A derived class with two base classes and these
two base classes have one common base class is
called multipath inheritance.
class B : public A
{
};
class C : public A
{
};
A. Using scope resolution operator we can manually specify the path from which
data member a will be accessed
2. But Problem is Derived class still having the two copies from base class shown in
below example
#include<iostream.h> int main()
#include<conio.h> {
D ob;
class A ob.C::show(); //use :: by specifying the path through class C
{ ob.B::show(); // use :: by specifying the path through class B
public : return 0;
void show() }
{
cout<<“A function is called ";
}
}; Note : Still, there are two copies of Class A in Class D.
class B : public A
{
};
class C : public A
{
};
class A {
public:
void show()
{
cout << "Hello form A \n";
}
};
class B : public A
{
};
class C : public A
{
};
class D : public B, public C
{
};
int main()
{
D object;
object.show();
}
#include<iostream.h> int main()
#include<conio.h> {
// Avoid Ambiguity by using Virtual Base class
class A D ob;
{
public : ob.show(); // call the Class A function without Ambigiuty
void show()
{ getch();
cout<<“A is called "; return 0;
} }
};
// An abstract class
class Test
{
// Data members of class
public:
MAKE ABSTRACT CLASS // Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
Why Abstract class ?
1. The purpose of an abstract class is to provide an appropriate base class from which other classes (Derived)
can inherit.
2. Abstract classes cannot be used to Create objects and serves only as an interface.
3. We are not able to create the object of Abstract class , if we try to create the object compiler show error
4. A pure virtual function (or abstract function) in C++ is a virtual function for which we can have
implementation, If we do not override the pure virtual function in derived class, then derived class also
becomes abstract class.
5. Failure to override a pure virtual function in a derived class, then attempting to create objects of that class,
is a compilation error.
6. Classes that can be used to create objects are called concrete classes.
A pure virtual function is implemented by classes which are derived from a Abstract class.
public: d.fun();
virtual void fun() = 0;
}; return 0;
}
// This class inherits from Base and implements fun()
class Test
{
public:
int x;
int main()
{
/*If class is ABC(Abstract class) then we are NOT able to create the object of abstract class */
Test t;
return 0;
}
Friend Class
1. We can also make a class a friend of another class.
2. In that case, all the member function of the class declared as friend become the friend
functions of the other class. Let's see how to make a class a friend of another.
class A
{
friend class B;
};
class B
{
};
Class B is declared as a friend of class A in the above code. So, now all the member functions of class B became friend
functions of class A.
Friend Class in C++
We can also use a friend Class in C++ using the friend keyword.
For example,
class A
{
// Class B is a friend class of Class A
friend class B;
};
class B
{
};
When a class is declared a friend class, all the member functions of the friend class become friend functions.
Since Class B is a friend class, we can access all members of Class A from inside Class B.
#include <iostream>
class Square
{ int getArea()
// declaring Rectangle as friend class {
friend class Rectangle; return length * breadth;
public: }
int side;
};
Square ( int s )
{ int main()
side = s; {
} Square sq(5);
};
class Rectangle Rectangle rec;
{
int length, breadth; rec.shape(sq);
2. The nested class is also a member variable of the enclosing class and has the same access rights as
3. However, the member functions of the enclosing class have no special access to the members of a
nested class.
#include<iostream> int main() {
using namespace std;
class A { cout<<"Nested classes in C++"<< endl;
public:
A :: B obj;
class B
{ obj.getdata(9);
private:
int num; obj.putdata();
public:
void getdata(int n) return 0;
{ }
num = n;
}
void putdata()
{
cout<<"The number is "<<num;
}
}; // End of class A