Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’Grade Accredited, ISO 9001:2015 Certified
Department of Computer Engineering
(NBA Accredited)
Subject- Object Oriented Programming (CO212)
Unit 2 – Overloading and Inheritance
Topic – 2.4 Introduction to Inheritance
Prof. V.N.Nirgude
Assistant Professor
E-mail :
nirgudevikascomp@sanjivani.org.in
Contact No: 9975240215
Introduction
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2
• Inheritance in C++ is one of the major aspects
Programming.
of Object Oriented
• It is the process by which one object can inherit or acquire the
features of another object.
• It is a way of creating new class(derived class) from the existing
class(base class) providing the concept of reusability.
Introduction
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3
• The base class is called as superclass and derived class is called a
subclass.
• Semantically, inheritance denotes an “is-a” relationship
between a class and one or more refined version of it.
Example
“Bicycle”is a generalization of
“Mountain Bike”.
“Mountain Bike” is a specialization of
“Bicycle”.
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4
Defining a Base Class
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5
• Base class has general features which are common to all the derived
classes and derived class adds specific features.
• This enables us to form a hierarchy of classes.
class Base-class
{
... ... ...
………….//Members of base class
};
Defining a Derived Class
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6
• The general form of deriving a subclass from a base class is as follows:
Class derived-class-name : visibility-mode base-class-name
{
………………
……………….// members of the derived class
};
• The visibility-mode is optional, it may be either private or public or
protected, by default it is private.
• This visibility mode specifies how the features of base class are
visible within the derived class.
Example using public inheritance
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark()
{
cout<<"Barking...";
}
};
int main()
{
Dog d1;
d1.eat();
d1.bark();
return 0;
}
Output:
Eating...
Barking...
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7
Example using private inheritance
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8
#include <iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};
class B : private A
{
public:
void display()
{
int result = mul();
cout <<"Multiplication of a and b is : "<<result;
}
};
int main()
{
B b;
b.display();
return 0;
}
Output:
Multiplication of a and b is : 20
Thank You..
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 9

Introduction to Inheritance and Its concepts

  • 1.
    Sanjivani Rural EducationSociety’s Sanjivani College of Engineering, Kopargaon-423 603 (An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune) NAAC ‘A’Grade Accredited, ISO 9001:2015 Certified Department of Computer Engineering (NBA Accredited) Subject- Object Oriented Programming (CO212) Unit 2 – Overloading and Inheritance Topic – 2.4 Introduction to Inheritance Prof. V.N.Nirgude Assistant Professor E-mail : nirgudevikascomp@sanjivani.org.in Contact No: 9975240215
  • 2.
    Introduction DEPARTMENT OF COMPUTERENGINEERING , SCOE,KOPARGAON 2 • Inheritance in C++ is one of the major aspects Programming. of Object Oriented • It is the process by which one object can inherit or acquire the features of another object. • It is a way of creating new class(derived class) from the existing class(base class) providing the concept of reusability.
  • 3.
    Introduction DEPARTMENT OF COMPUTERENGINEERING , SCOE,KOPARGAON 3 • The base class is called as superclass and derived class is called a subclass. • Semantically, inheritance denotes an “is-a” relationship between a class and one or more refined version of it.
  • 4.
    Example “Bicycle”is a generalizationof “Mountain Bike”. “Mountain Bike” is a specialization of “Bicycle”. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4
  • 5.
    Defining a BaseClass DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5 • Base class has general features which are common to all the derived classes and derived class adds specific features. • This enables us to form a hierarchy of classes. class Base-class { ... ... ... ………….//Members of base class };
  • 6.
    Defining a DerivedClass DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6 • The general form of deriving a subclass from a base class is as follows: Class derived-class-name : visibility-mode base-class-name { ……………… ……………….// members of the derived class }; • The visibility-mode is optional, it may be either private or public or protected, by default it is private. • This visibility mode specifies how the features of base class are visible within the derived class.
  • 7.
    Example using publicinheritance #include <iostream> using namespace std; class Animal { public: void eat() { cout<<"Eating..."<<endl; } }; class Dog: public Animal { public: void bark() { cout<<"Barking..."; } }; int main() { Dog d1; d1.eat(); d1.bark(); return 0; } Output: Eating... Barking... DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7
  • 8.
    Example using privateinheritance DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8 #include <iostream> using namespace std; class A { int a = 4; int b = 5; public: int mul() { int c = a*b; return c; } }; class B : private A { public: void display() { int result = mul(); cout <<"Multiplication of a and b is : "<<result; } }; int main() { B b; b.display(); return 0; } Output: Multiplication of a and b is : 20
  • 9.
    Thank You.. DEPARTMENT OFCOMPUTER ENGINEERING , SCOE,KOPARGAON 9