Chapter-9 (Inheritance 31.8.2020)
Chapter-9 (Inheritance 31.8.2020)
IT-22015
Programming Language in C++
Chapter 9
Inheritance
1
SECOND YEAR
Topics
Class Hierarchies
2
SECOND YEAR
Objectives
3
SECOND YEAR
Learning Outcomes
4
SECOND YEAR
What is Inheritance?
5
SECOND YEAR
While defining a subclass like this, the super class must be already
defined or at least declared before the sub class declaration.
Access Mode is used to specify, the mode in which the properties of
super class will be inherited into sub class, public, private or protected.
: is operator which is used for inheriting the features of base class into
derived class it improves the functionality of derived class.
6
SECOND YEAR
The derived class is the class that inherit properties from base class
(es).
The derived class inherits all the capabilities of the base class and can
add additional features to the derived class.
7
SECOND YEAR
Example of Inheritance
Animal
Base Class /
Super Class
Derived from
Derived Class /
Sub Class
Dog class, Cat class and Cow class inherit the
properties from its super class Animal.
8
SECOND YEAR
9
SECOND YEAR
We can define data member and member function with the same name
in both base and derived class.
When the function with same name exists in both class and derived
class, the function in the derived class will get executed.
This means, the derived class function overrides the base class
function.
13
SECOND YEAR
s1.push(11);
s1.push(22);
s1.push(33);
16
SECOND YEAR
Class Hierarchies
Class Hierarchies
// models employee database using inheritance void putdata () const
#include <iostream> {
using namespace std; cout<< “\n Name: ” << name;
const int LEN = 80; cout<< “\n Number: ” << number; }
class employee };
{
private: class manager : public employee
char name[LEN]; {
unsigned long number; private:
char title[LEN]; double dues;
public:
public:
void getdata () void getdata()
{ {
cout<< “\n Enter last name: ”; employee::getdata();
cin >> name; cout<< “ Enter title: ”; cin >> title;
cout<< “ Enter number: ”; cout<< “ Enter golf club dues: ”;
cin >> number; cin>> dues;
} }
19
SECOND YEAR
Class Hierarchies
void putdata() const void putdata () const
{ {
employee::putdata(); employee :: putdata ();
cout<< “\n Title: ” << title; cout<< “\n Number of publications: ” <<
cout<< “\n Golf club dues: ” << pubs;
dues; }
} };
}; class laborer : public employee
class scientist : public employee {
{ };
private: int pubs; int main ()
public: {
void getdata() manager m1, m2;
{ scientist s1;
employee :: getdata ();
cout<< “ Enter number of pubs: ”; laborer l1;
cin>> pubs; cout<< endl;
} 20
SECOND YEAR
Class Hierarchies
cout<< “\nEnter data for manager
1”;
m1.getdata();
cout<< “\nEnter data for manager
2”;
m2.getdata();
cout<< “\nEnter data for scientist
1”; Output
s1.getdata();
cout<< “\nEnter data for laborer 1”;
l1.getdata();
cout<< “\n Data on manager 1”;
m1.putdata();
cout<< “\n Data on manager 2”;
m2.putdata();
cout << “\n Data on scientist1”;
s1.putdata();
21
cout<< “\n Data on laborer 1”;
SECOND YEAR
Summary
A derived class can inherit the features of a base class.
The derived class can add other features of its own, so it becomes a
specialized version of the base class.
22
SECOND YEAR
References
‐ https://www.studytonight.com/cpp/overview-of-inheritance.php
‐ https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/
23
SECOND YEAR
THANK YOU!
24