0% found this document useful (0 votes)
6 views

sdf-inheritance

The document provides an overview of Object-Oriented Programming (OOP) concepts, focusing on inheritance, its types, and access specifiers in C++. It explains how inheritance allows classes to acquire properties from other classes, enhancing code reusability and functionality. Additionally, it discusses various inheritance modes, types, and the implications of constructors and destructors in derived classes.

Uploaded by

singhaladi418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

sdf-inheritance

The document provides an overview of Object-Oriented Programming (OOP) concepts, focusing on inheritance, its types, and access specifiers in C++. It explains how inheritance allows classes to acquire properties from other classes, enhancing code reusability and functionality. Additionally, it discusses various inheritance modes, types, and the implications of constructors and destructors in derived classes.

Uploaded by

singhaladi418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Course Title:

Software Development Fundamentals-2


DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
&
INFORMATION TECHNOLOGY

Department of Computer Science & Engineering


Department of Information Technology
Basic concept of Object Oriented Concept
Attributes of Object Oriented Programming
• Class
• Object
• Encapsulation and Data hiding
• Data abstraction
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing

Prepared by Ashish Mishra 2


Inheritance
• Inheritance is the process by which objects of one class acquire the
properties of object of another class.
• The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most
important feature of Object Oriented Programming.
Super Class----- Sub Class: The class that inherits
properties from another class is called
Sub class or Derived Class.
Super Class: The class whose properties
are inherited by sub class is called Base
Class or Super class.
Sub Class------

Prepared by Ashish Mishra 3


Inheritance
▪ Inheritance is a way to include functionalities of a parent class to a
child class.
▪ It doesn’t allow inheritance of private members from parent class.
▪ It enhances code reusability. Class A
Data
Functions

Class B
Data
Functions
Inheritance
▪ Polymorphism enables to define many forms of a function and it is
invoked based on the object calling it.
▪ The functionality of a function differs according to the object that
calls it.
▪ Inheritance helps to implement polymorphism. Class A
Data
Functions

Class B
Data
Functions
Advantages of inheritance
• When a class inherits from another class, there are three benefits:
• (1) You can reuse the methods and data of the existing class
(2) You can extend the existing class by adding new data and new
methods
(3) You can modify the existing class by overloading its methods
with your own implementations
Inheritance

• Why and when to use inheritance?


• Modes of Inheritance
• Types of Inheritance

Prepared by Ashish Mishra 7


Why and when to use inheritance?
Consider a group of vehicles. You need to create classes for Bus, Car
and Truck. The methods fuelAmount(), capacity(), applyBrakes() will be
same for all of the three classes.

Without Inheritance
Prepared by Ashish Mishra 8
Why and when to use inheritance?

Code Reusability

With Inheritance
Prepard by Ashish Mishra 9
Modes of Inheritance
• Public mode: If we derive a sub class from a public base class. Then the
public member of the base class will become public in the derived class and
protected members of the base class will become protected in derived class.
Private members of the base class will never get inherited in sub class.
• Protected mode: If we derive a sub class from a Protected base class. Then
both public member and protected members of the base class will become
protected in derived class. Private members of the base class will never get
inherited in sub class.
• Private mode: If we derive a sub class from a Private base class. Then both
public member and protected members of the base class will become Private
in derived class. Private members of the base class will never get inherited
in sub class.

Prepard by Ashish Mishra 10


Types of Inheritance
Single Inheritance: In single inheritance, a class is allowed to inherit
from only one class. i.e. one sub class is inherited by one base class
only.

Prepard by Ashish Mishra 11


Types of Inheritance: Single Inheritance

Prepard by Ashish Mishra 12


Types of Inheritance: Single Inheritance
// C++ program to explain
// Single inheritance // sub class derived from two base classes
#include <iostream> class Car: public Vehicle{
using namespace std; };
// base class // main function
class Vehicle { int main()
public: {
Vehicle() // creating object of sub class will
{ // invoke the constructor of base classes
cout << "This is a Vehicle" << endl; Car obj;
} return 0;
}; }

Prepard by Ashish Mishra 13


Types of Inheritance- Multiple Inheritance
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a
class can inherit from more than one classes. i.e one sub class is
inherited from more than one base classes.

Prepard by Ashish Mishra 14


Multiple Inheritance Example Program
WingedAnimal()
#include <iostream> {
using namespace std; cout << "Winged animal can flap." << endl;

class Mammal { }
};
public:
class Bat: public Mammal, public WingedAnimal { };
Mammal()
int main()
{ {
cout << "Mammals can give Bat b1;
direct birth." << endl; return 0;
} }
}; Output
class WingedAnimal { Mammals can give direct birth.

public: Winged animal can flap.


Prepard by Ashish Mishra 15
Types of Inheritance- Multilevel Inheritance
Multilevel Inheritance: In this type of inheritance, a derived class is
created from another derived class.

Prepard by Ashish Mishra 16


Types of Inheritance: Multilevel Inheritance
• In C++ programming, not only you can derive a class from the
base class but you can also derive a class from the derived
class. This form of inheritance is known as multilevel
inheritance.

Prepard by Ashish Mishra 17


Multilevel Inheritance: Example

Prepard by Ashish Mishra 18


Types of Inheritance
Hierarchical Inheritance: In this type of inheritance, more than one
sub class is inherited from a single base class. i.e. more than one
derived class is created from a single base class.

Prepard by Ashish Mishra 19


Types of Inheritance
// C++ program to implement Hierarchical Inheritance // second sub class
class Bus: public Vehicle
#include <iostream>
{ };
using namespace std;
// main function
class Vehicle int main()
{ {
public: // creating object of sub class will
// invoke the constructor of base class
Vehicle()
Car obj1;
{ Bus obj2;
cout << "This is a Vehicle" << endl; return 0;
} }
Output:
};
This is a Vehicle
// first sub class This is a Vehicl
class Car: public Vehicle
{ }; Prepard by Ashish Mishra 20
Types of Inheritance
Hybrid Inheritance: Hybrid Inheritance is implemented by combining
more than one type of inheritance.

Prepard by Ashish Mishra 21


Access Specifier in Class Declaration
Class sample
{
Private:
//Visible to member function within its class but not in derived class
Protected:
//Visible to member function within its class and derived class
Public:
//visible to member functions within its class, derived classed and through
object
};
Prepared by Ashish Mishra 22
Access Specifier
Access modifiers or Access Specifiers in a class are used to set the
accessibility of the class members. That is, it sets some restrictions on
the class members not to get directly accessed by the outside
functions.
There are 3 types of access modifiers available in C++:
•Public
•Private
•Protected

Prepard by Ashish Mishra 23


Access Specifier

Prepard by Ashish Mishra 24


INHERITANCE
HOW TO DECLARE DERIVED CLASS

Class Derived_class_name : Visiblity Mode Base_class_name


{

Public
} Private
Protected
Prepard by Ashish Mishra 25
INHERITANCE
FOLLOWING ARE THE THREE POSSILBE STYLE OF DERIVATION

Public Derivative
class Derived_Class_name: public Base_class_name
{

Prepared by Ashish Mishra 26


INHERITANCE
FOLLOWING ARE THE THREE POSSILBE STYLE OF DERIVATION

Private Derivative
class Derived_Class_name: private Base_class_name
{

Prepared by Ashish Mishra 27


INHERITANCE
FOLLOWING ARE THE THREE POSSILBE STYLE OF DERIVATION

Protected Derivative
class Derived_Class_name: protected Base_class_name
{

Prepared by Ashish Mishra 28


INHERITANCE
Base class Derivation Derived class User of derived
member access level member class
Private
Private Protected Not accessible Not accessible
Public
Public
Private Private Not accessible
Protected
Public
Protected Protected Not accessible
Protected
Public Protected Protected Not accessible
Public Public Public
Prepard by Ashish Mishra
Public 29
B
Not Inheritable---------------------- Private -----------------Not Inheritable
Protected
Public

Class D2: private B


Class D1: public B

Private Private
Protected Protected
Public Public

Class X: public D1:protected D2

Private
Protected
Public

Prepard by Ashish Mishra 30


B
Not Inheritable---------------------- Private -----------------Not Inheritable
Protected
Public

Class D2: private B


Class D1: public B

Private Private
Protected Protected
Public Public

Class X: public D1:protected D2

Private
Protected
Public

Prepard by Ashish Mishra 31


Class Derivation
Constructors and Destructors
• Constructors and destructors are not inherited
• Each derived class should define its constructors/destructor
• If no constructor is written=> hidden constructor is generated and will call the base
default constructor for the inherited portion and then apply the default initialization
for any additional data members
• When a derived object is instantiated, memory is allocated for
• Base object
• Added parts
• Initialization occurs in two stages:
• the base class constructors are invoked to initialize the base objects
• the derived class constructor is used to complete the task
• The derived class constructor specifies appropriate base class constructor
in the initialization list
• If there is no constructor in base class, the compiler created default constructor used
• If the base class is derived, the procedure is applied recursively
Constructor Rules for Derived Classes
The default constructor and the destructor of the base class are
always called when a new object of a derived class is created or
destroyed.

class A { class B : public A


public: {
A() public:
{cout<< “A:default”<<endl;} B (int a)
A (int a) {cout<<“B”<<endl;}
{cout<<“A:parameter”<<endl;} };
};

output: A:default
B test(1); B 33
Constructor Rules for Derived Classes
You can also specify an constructor of the base
class other than the default constructor
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass
args )
{ DerivedClass constructor body }

class A { class C : public A {


public: public:
A() C (int a) : A(a)
{cout<< “A:default”<<endl;} {cout<<“C”<<endl;}
A (int a) };
{cout<<“A:parameter”<<endl;}
};
output: A:parameter
C test(1); C 34
The order is important
#include<iostream>
using namespace std;
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};

class B
{
public:
B() { cout << "B's constructor called" << endl; }
OUTPUT:-
}; B's constructor called
A's constructor called
C's constructor called
35
#include<iostream>
using namespace std;
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};

class B
{
public:
B() { cout << "B's constructor called" << endl; }
}; OUTPUT:-
A's constructor called
B's constructor called
C's constructor called
36
class C: public A, public B
Inheritance Types : Review

Image source: https://simplesnippets.tech/inheritance-in-cpp-inheritance-types/


Hybrid Inheritance : Diamond Problem

Class D inherits the members of class A via two paths i.e. class B and class C.

In the above example, both Class B & Class C inherit Class A, they both have single copy of Class A members.
However, Class D inherits both Class B & Class C, therefore Class D has two copies of Class A members, one from
Class B and another from Class C.
Example PERSON 39

Name
Phone number
address

STUDENT FACULTY

Name Name
Phone number Phone number
Address Address
STUDENT FACULTY
Enrollment No Salary
Enrollment No Subject taught
CGPA Subject taught
CGPA Salary

TA

Name Two copies of


TA Phone number
Address name , Phone
Semester Enrollment No
number and
Guide CGPA
Name Address
Phone number
Address
Salary
Subject taught
Semester
Guide
Example

class person class student : public person


{ {
char name[10]; int eno;
int age; double cgpa;
long pno; public:
public: student()
person() {
{ cout<<"\n student constructed";
cout<<"\n person constructed"; cout<<"\n enter eno and cgpa \n";
cout<<"\n enter name, age, pno \n"; cin>>eno>>cgpa;
cin>>name>>age>>pno; }
} void display()
void display() {
{ cout<<"\n student: "<<eno<<" "<<cgpa;
cout<<"\n person: "<<name<<" "<<age<<" "<<pno; }
}
}; };
Example

class ta: public student, public faculty


class faculty : public person {
{ int sem;
int sal; char guide[10];
char subject[10]; public:
public: ta()
faculty() {
{ cout<<"\n TA constructed";
cout<<"\n faculty constructed"; cout<<"\n enter sem and guide \n";
cout<<"\n enter salary and subject \n"; cin>>sem>>guide;
cin>>sal>>subject; }
} void display()
void display() error: 'person' is an ambiguous
{
{ cout<<"\n faculty: "<<sal<<" "<<subject; base of 'ta'
person::display();
} student::display();
}; faculty::display();
cout<<"\n ta:"<<sem<<" "<<guide;
}
};
Example

int main() error: request for member 'age' is ambiguous


{
ta obj;
obj.age=10; class TA would have duplicate sets of the members inherited from class Person. This introduces
obj.display(); ambiguity and should be avoided.
return 0;
}

The duplication of inherited members due to these multiple paths can be avoided by making a common
base class as virtual base class while declaring the direct or intermediate base classes as below:

class person
{ };
class student: virtual public person
{ };
class faculty: public virtual person
{ };
class TA: public student, public faculty
{ };
• When a class is made a virtual base class, C++ takes necessary care to
see that only one copy of that class members is inherited, regardless of
how many paths exist between the virtual base class and a derived class.
• You can visualize it as, A may not be direct base class of D, but
virtually it is a base class of D (shown in dotted line). So any
communication with A from D will be done as if A is its own base class
(and not via B or C).
More examples on virtual inheritance and
diamond/pyramid problems
Example 1

class a class b : public a class c: public a class d : public b, public c


{ { { {
int x; int y; int z; int u;
public: public: public: public:
a() b() c() d()
{ { { {
cout<<"\n a()"; cout<<"\n b()"; cout<<"\n c()"; cout<<"\n d()";
} } } }
a(int t) b(int t) c(int t) d(int t)
{ { { {
cout<<"\n a(int)"; cout<<"\n b(int)"; cout<<"\n c(int)"; cout<<"\n d(int)";
} } } }
};
}; }; };

OUTPUT:
int main()
a()
{ b()
d obj; a()
return 0; c()
} d()
Example 2

class a class b : public virtual a class c: public a class d : public b, public c


{ { { {
int x; int y; int z; int u;
public: public: public: public:
a() b() c() d()
{ { { {
cout<<"\n a()"; cout<<"\n b()"; cout<<"\n c()"; cout<<"\n d()";
} } } }
a(int t) b(int t) c(int t) d(int t)
{ { { {
cout<<"\n a(int)"; cout<<"\n b(int)"; cout<<"\n c(int)"; cout<<"\n d(int)";
} } } }
};
}; }; };

OUTPUT:
int main()
a()
{ b()
d obj; a()
return 0; c()
} d()
Example 3

class a class b : public a class c: public virtual a class d : public b, public c


{ { { {
int x; int y; int z; int u;
public: public: public: public:
a() b() c() d()
{ { { {
cout<<"\n a()"; cout<<"\n b()"; cout<<"\n c()"; cout<<"\n d()";
} } } }
a(int t) b(int t) c(int t) d(int t)
{ { { {
cout<<"\n a(int)"; cout<<"\n b(int)"; cout<<"\n c(int)"; cout<<"\n d(int)";
} } } }
};
}; }; };

OUTPUT:
int main()
a()
{ a()
d obj; b()
return 0; c()
} d()
Example 4

class a class b : public virtual a class c: virtual public a class d : public b, public c
{ { { {
int x; int y; int z; int u;
public: public: public: public:
a() b() c() d()
{ { { {
cout<<"\n a()"; cout<<"\n b()"; cout<<"\n c()"; cout<<"\n d()";
} } } }
a(int t) b(int t) c(int t) d(int t)
{ { { {
cout<<"\n a(int)"; cout<<"\n b(int)"; cout<<"\n c(int)"; cout<<"\n d(int)";
} } } }
};
}; }; };

OUTPUT:
int main()
a()
{ b()
d obj; c()
return 0; d()
}
Example 5

class a class b : public a class c: virtual public a class d : public b, public c


{ { { {
int x; int y; int z; int u;
public: public: public: public:
a() b() : a(3) c() d() : b(5), a(2)
{ { { {
cout<<"\n a()"; cout<<"\n b()"; cout<<"\n c()"; cout<<"\n d()";
} } } }
a(int t) b(int t) c(int t) : a(2) d(int t) : c(8)
{ { { {
cout<<"\n a(int)"; cout<<"\n b(int)"; cout<<"\n c(int)"; cout<<"\n d(int)";
} } } }
};
}; }; };

OUTPUT:
int main()
a(int) a()
{ a() a(int)
d obj; b(int) b()
d obj2(10); c() c(int)
return 0; d() d(int)
}
Example 6
class a class e class c: public b, public d
{ int x; { int x1; { int z;
public: public: public:
a() e() c(): e(2), d(7)
{ cout<<"\n a()"; {cout<<"\n e()"; { cout<<"\n c()";
} } }
a(int t) e(int t) c(int t) : a(1)
{ cout<<"\n a(int)"; {cout<<"\n e(int)"; { cout<<"\n c(int)";
} } } a()
}; }; }; e(int)
b()
class b : virtual public a class d : virtual public e
int main() d(int)
{ int y; { int y;
{ c()
public: public:
c obj;
b():a(3) d()
c obj2(10);
{ {
return 0; a(int)
cout<<"\n b()"; cout<<"\n d()";
} e()
} }
b(int t) d(int t) : e(2) b()
{ { d()
cout<<"\n b(int)"; cout<<"\n d(int)"; c(int)
} }
}; };
Example 7
Just changed the order
class a class e class c: public d, public b
here
{ int x; { int x1; { int z;
public: public: public:
a() e() c(): e(2), d(7)
{ cout<<"\n a()"; {cout<<"\n e()"; { cout<<"\n c()";
} } }
a(int t) e(int t) c(int t) : a(1)
{ cout<<"\n a(int)"; {cout<<"\n e(int)"; { cout<<"\n c(int)";
} } } e(int)
}; }; }; a()
d(int)
class b : virtual public a class d : virtual public e
int main() b()
{ int y; { int y;
{ c()
public: public:
c obj;
b():a(3) d()
c obj2(10);
{ {
return 0; e()
cout<<"\n b()"; cout<<"\n d()";
} a(int)
} }
b(int t) d(int t) : e(2) d()
{ { b()
cout<<"\n b(int)"; cout<<"\n d(int)"; c(int)
} }
}; };
Method Overriding
•As we know, inheritance is a feature of OOP that allows
us to create derived classes from a base class. The
derived classes inherit features of the base class.
•Suppose, the same function is defined in both the
derived class and the based class. Now if we call this
function using the object of the derived class, the
function of the derived class is executed.
•This is known as function overriding in C++. The
function in derived class overrides the function in base
class. 52
Method Overriding
• To override a function you must have the same signature in child
class. By signature I mean the data type and sequence of
parameters. Here we don’t have any parameter in the parent
function so we didn’t use any parameter in the child function.

• In function overriding, the function in parent class is called the


overridden function and function in child class is called overriding
function.

53
Requirements for Overriding a Function
1.Inheritance should be there. Function
overriding cannot be done within a class. For
this we require a derived class and a base
class.
2.Function that is redefined must have exactly
the same declaration in both base and derived
class, that means same name, same return
type and same parameter list.
54
Derived Function

Method Overriding
// C++ program to demonstrate function overriding class Derived : public Base {
public:
#include <iostream> void print() {
using namespace std; cout << "Derived Function" << endl;
}
class Base { };
public:
void print() { int main() {
cout << "Base Function" << endl; Derived derived1;
} derived1.print();
}; return 0;
}
Outpu: Derived Function
55

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy