0% found this document useful (0 votes)
11 views25 pages

6 - Introduction To Inheritance in c++2

The document provides an introduction to inheritance in C++, explaining its definition, types (single, multiple, multi-level, hybrid), and access specifiers (public, protected, private). It emphasizes the importance of reusability in programming through inheritance, allowing derived classes to inherit properties from base classes. Additionally, it includes code examples to illustrate different modes of inheritance and their effects on member accessibility.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

6 - Introduction To Inheritance in c++2

The document provides an introduction to inheritance in C++, explaining its definition, types (single, multiple, multi-level, hybrid), and access specifiers (public, protected, private). It emphasizes the importance of reusability in programming through inheritance, allowing derived classes to inherit properties from base classes. Additionally, it includes code examples to illustrate different modes of inheritance and their effects on member accessibility.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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
Base Class below diagram, the class 'vehicle' is a base
class.

The class that does the inheriting is called a


Derived Class derived class. In 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
//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; }
};

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;

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' is default for What is the output?


classes
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;

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];
};

class b1: public base { };

class b2: public base { };

class derived: public b1, public b2 {};

int main(void)
{
cout << sizeof(derived);
return 0;
}

You might also like