Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Introduction to Inheritance in c++

19CSE201-Advanced Programming
Objective
 Introduction
 Types of Inheritance
 Single Inheritance
 Multiple Inheritance
 Multi Level Inheritance
 Hybrid Inheritance
 Access specifier
 Public
 Protected
 Private
Introduction
What is Inheritance?
 In C++, inheritance is a process in which one object
acquires all the properties and behaviors of its parent
object automatically.
 The capability of a class to derive properties and
characteristics from another class is called Inheritance.
 The technique of deriving a new class from an old one
is called inheritance.
 Old Class is called : Super class or Base class
 New class is called: Derived class or sub class
Introduction
Type of class Definition

A class that is inherited is called a base class. In below


Base Class
diagram, the class 'vehicle' is a base class.

The class that does the inheriting is called a derived class. In


Derived Class
below diagram, the class 'car' is a derived class.
Introduction
Why and when to use Inheritance?
 Instead of trying to write programs repeatedly, using
existing code is a good practice for the programmer
to reduce development time and avoid mistakes. In
C++, reusability is possible by using Inheritance.
 Write common properties in Base class and extend to sub classes
Type of Inheritance
Single Inheritance
 Single Inheritance Block Diagram

 Syntax
Example
// inheritance.cpp //Derive Class //Main program
//Base Class
class derive : public base //single int main()
#include <iostream> derived class {
using namespace std; { derive a;
class base //single base class private: a.getdata();
{ int y; a.readdata();
public: public: a.product();
int x; void readdata() return 0;
void getdata() { } //end of program
{ cout << "Enter the value of y = ";
cout << "Enter the value of cin >> y;
x = "; }
cin >> x; void product()
} {
}; cout << "Product = " << x * y;
}
};
Different Modes of Inheritance in C++
 Public mode: class A {
 Public members of base class public:
become public in derived class int x;
 Protected members of base class
protected:
become Protected in derived int y;
class
private:
 Private members are inaccessible int z;
in derived class };

class B : public A {
// x stays public
// y stays protected
// z is not accessible from B
};
Public mode
class A {

public:
int x; int main()
{
protected: B b;
int y; b.x=5;
b.y=10;
private: b.z=15
int z; }
};

class B : public A { What is the output?


// x stays public
// y stays protected
// z is not accessible from B
};
Public mode
class A {

public:
int x;

protected:
int y; int main()
{
private: B b;
int z; b.x=5;
}; }

class B : public A {
// x stays public
// y stays protected
// z is not accessible from B
};
Public mode
class A { class B : public A
{
public: public:
int x; void display()
{
protected: y=20;
int y; z=25;
cout<<"x:"<<x<<"y:"<<y<<endl;
private: cout<<“z: ” <<z<<endl;
int z; }
}; };

int main()
{
B b;
What is the output? b.x=5;
b.display();
}
Public mode
class A { class B : public A
{
public: public:
int x; void display()
{
protected: y=20;
int y; cout<<"x:"<<x<<"y:"<<y<<endl;
}
private: };
int z;
};
int main()
{
B b;
b.x=5;
b.display();
}
Summary
Different Modes of Inheritance in C++
 Protected mode: class A {
 Public members of base class public:
become protected in derived int x;
class
protected:
 Protected members of base class int y;
become Protected in derived
class private:
int z;
 Private members are inaccessible };
in derived class
class C : protected A {
// x becomes protected
// y stays protected
// z is not accessible from C
};
Protected mode
class A {

public:
int x;

protected:
int y; int main()
{
private: C c;
int z; c.x=5;
}; c.y=10;
c.z=15;
class C : protected A { }
// x becomes protected
// y stays protected
// z is not accessible from C What is the output?
};
Protected mode
class A { class C : protected A
{
public: public:
int x; void display()
{
protected: x=10;
int y; y=20;
cout<<"x:"<<x<<"y:"<<y<<endl;
private: }
int z; };
};

int main()
{
C c;
What is the output? c.display();
}
Protected mode
class A { class C : protected A
{
public: public:
int x; void display()
{
protected: x=10;
int y; y=20;
z=25;
private: cout<<"x:"<<x<<"y:"<<y<<endl;
int z; cout<<“z: ” <<z<<endl;
}; }
};

int main()
{
C c;
What is the output? c.display();
}
Summary
Different Modes of Inheritance in C++
 Private mode: class A {
 Public members of base class public:
become private in derived class int x;
 Protected members of base class
protected:
become private in derived class int y;
 Private members are inaccessible
private:
in derived class int z;
};

// 'private' is default for classes


class D : private A {
// x becomes private
// y becomes private
// z is not accessible from D
}
Private mode
class A {

public:
int x; int main()
{
protected: D d;
int y; d.x=5;
d.y=10;
private: d.z=15;
int z; }
};

// 'private' is default for classes What is the output?


class D : private A {
// x becomes private
// y becomes private
// z is not accessible from D
}
Private mode
class A { class D : private A
{
public: public:
int x; void display()
{
protected: x=10;
int y; y=20;
cout<<"x:"<<x<<"y:"<<y<<endl;
private: }
int z; };
};

int main()
{
D d;
What is the output? d.display();
}
Private mode
class A { class D : private A
{
public: public:
int x; void display()
{
protected: x=10;
int y; y=20;
z=25;
private: cout<<"x:"<<x<<"y:"<<y<<endl;
int z; cout<<“z: ” <<z<<endl;
}; }
};

int main()
{
D d;
What is the output? d.display();
}
Summary

You might also like