UNIT 3
Extending Classes Using
Inheritance
Inheritance
• inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically.
• In such way, you can reuse, extend or modify the attributes and behaviors
which are defined in other class.
• In C++, the class which inherits the members of another class is called
derived class and the class whose members are inherited is called base
class.
• The derived class is the specialized class for the base class.
• Code reusability: Now you can reuse the members of your parent class.
So, there is no need to define the member again. So less code is required in
the class.
• Types Of Inheritance
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance
• Derived Classes
A Derived class is defined as the class derived from the base class.
The Syntax of Derived class:
class derived_class_name :: visibility-mode base_class_name
{
// body of the derived class.
}
• derived_class_name: It is the name of the derived class.
• visibility mode: The visibility mode specifies whether the features of the
base class are publicly inherited or privately inherited. It can be public or
private.
• Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is
inherited from the only one base class.
Where 'A' is the base class, and 'B' is the derived class.
#include <iostream>
class Account
{
public:
float salary = 60000;
};
class Programmer: public Account
{
public:
float bonus = 5000;
};
int main(void)
{
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
/ inheritance.cpp
#include <iostream.h>
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter the value of x = ";
cin >> x;
}
};
class derive : public base //single derived class
{
private:
int y;
public:
void readdata()
{
cout << "Enter the value of y = ";
cin >> y;
}
void product()
{
cout << "Product = " << x * y;
}
};
int main()
{
derive a; //object of derived class
a.getdata();
a.readdata();
a.product();
return 0;
} //end of program
• Out put
Enter the value of x = 3
Enter the value of y = 4
Product = 12
• The private member is not inheritable.
• If we modify the visibility mode by making it public, but this takes away
the advantage of data hiding.
• C++ introduces a third visibility modifier, i.e., protected.
• The member which is declared as protected will be accessible to all the
member functions within the class as well as the class immediately derived
from it.
Public: When the member is declared as public, it is accessible to all the
functions of the program.
Private: When the member is declared as private, it is accessible within
the class only.
Protected: When the member is declared as protected, it is accessible
within its own class as well as the class immediately derived from it.
Multilevel Inheritance
• Multilevel inheritance is a process of deriving a class from another
derived class.
#include <iostream>
class Animal
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
// inheritance.cpp
#include <iostream>
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter value of x= ";
cin >> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout << "\nEnter value of y= ";
cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= ";
cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};
int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}
• Output
• Enter value of x= 2
• Enter value of y= 3
• Enter value of z= 3
• Product= 18
Multiple Inheritance
• Multiple inheritance is the process of deriving a new class
that inherits the attributes from two or more classes.