6 - Introduction To Inheritance in c++2
6 - Introduction To Inheritance in c++2
+
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
Base Class below diagram, the class 'vehicle' is a base
class.
Syntax
Example
// inheritance.cpp //Derive Class //Main
//Base Class program
class derive : public
#include <iostream> base //single derived class int main()
using namespace std; { {
class base //single base private: derive a;
class int y; a.getdata();
{ public: a.readdata();
public: void readdata() a.product();
int x; { return 0;
void getdata() cout << "Enter the value } //end of
{ of y = "; program
cout << "Enter the cin >> y;
value of x = "; }
cin >> x; void product()
} {
}; cout << "Product = " <<
x * y;
}
};
Different Modes of Inheritance in C++
Public mode: class A {
Public members of base public:
class become public in int x;
derived class
protected:
Protected members of int y;
base class become
Protected in derived class private:
int z;
Private members are };
inaccessible 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; }
};
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;
private: cout<<"x:"<<x<<"y:"<<y<<e
int z; ndl;
}; cout<<“z: ” <<z<<endl;
}
};
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<<e
private: ndl;
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 public:
class become protected in int x;
derived class
protected:
Protected members of int y;
base class 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 What is the output?
C
};
Protected mode
class A { class C : protected A
{
public: public:
int x; void display()
{
protected: x=10;
int y; y=20;
private: cout<<"x:"<<x<<"y:"<<y<<e
int z; ndl;
}; }
};
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:
int z; cout<<"x:"<<x<<"y:"<<y<<e
}; ndl;
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 public:
class become private in int x;
derived class
protected:
Protected members of int y;
base class become private
in derived class private:
int z;
Private members are };
inaccessible in derived
class // '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: cout<<"x:"<<x<<"y:"<<y<<e
int z; ndl;
}; }
};
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:
int z; cout<<"x:"<<x<<"y:"<<y<<e
}; ndl;
cout<<“z: ” <<z<<endl;
}
}; int main()
{
D d;
What is the output? d.display();
}
Summary
Activity
Assume that an integer takes 4 bytes and
there is no alignment in following classes,
predict the output.
#include<iostream>
using namespace std;
class base {
int arr[10];
};
int main(void)
{
cout << sizeof(derived);
return 0;
}