PREPARED BY :- PAUMIL PATEL
INHERITANCE IN C++
WHAT IS INHERITANCE
 Inheritance is one of the important feature of the OOP.
Inheritance is the method by which objects of one class
gets the properties of another class.
 The mechanism that allows us to extend the
definition of a class without making any physical
changes to the existing class is inheritance.
OVERVIEW OF INHERITANCE
 Inheritance is the capability of one class to acquire
properties and characteristics from another class. The class
whose properties are inherited by other class is called
the Parent or Base or Super class. And, the class which
inherits properties of other class is
called Child or Derived or Sub class.
 Inheritance lets you create new classes from existing class.
Any new class that you create from an existing class is
called derived class; existing class is called base class.
ADVANTAGES OF INHERITANCE
 Sometimes we need to repeat the code or we need
repeat the whole class properties. So It helps in
various ways.
1.) It saves memory space.
2.) It saves time.
3.) It will remove frustration.
4.) It increases reliability of the code
5.) It saves the developing and testing efforts
WHY WE USE INHERITANCE?
To increase the reusability of the code
and to make further usable for another
classes. We use the concept of
inheritance.
EXAMPLE OF INHERITANCE
BASIC SYNTAX OF INHERITANCE
 class Subclass_name : access mode Superclass_name
 While defining a subclass like this, the super class(base
class)must be already defined or at least declared before the
subclass(derived class) declaration.
 Access Mode is used to specify, the mode in which the
properties of superclass will be inherited into subclass, public,
private or protected.
FORMS OF INHERITANCE
SINGLE INHERITANCE
 In single level inheritance, there is only one base
class and has only one derived class.
 Example of single level inheritance:
class Base
{
};
class Derv: public Base
{
};
EXAMPLE OF SIMPLE CLASS INHERITANCE
 class Animal
{
public:
int legs = 4;
};
class Dog : public Animal
{
public:
int tail = 1;
};
int main()
{
Dog d;
cout << d.legs;
cout << d.tail;
}
Output : 4 1
MULTIPLE INHERITANCE
 A C++ class can inherit members from more than one class
and here is the extended syntax:
 class derived-class: access baseA, access baseB....
 Where access is one of public, protected, or private and
would be given for every base class and they will be
separated by comma as shown above.
EXAMPLE OF MULTIPLE INHERITANCE
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot;
cout<<"ntAverage : "<<avg;
}
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66
REFERENCES
 http://www.cpp.thiyagaraaj.com/c-programs/simple-
program-for-multiple-inheritance-using-c-
programming
 http://www.studytonight.com/cpp/overview-of-
inheritance.php
 http://www.tutorialspoint.com/cplusplus/cpp_inherita
nce.html
 http://www.cplusplus.com/doc/tutorial/inheritance/
THANK YOU 

Inheritance in c++

  • 1.
    PREPARED BY :-PAUMIL PATEL INHERITANCE IN C++
  • 2.
    WHAT IS INHERITANCE Inheritance is one of the important feature of the OOP. Inheritance is the method by which objects of one class gets the properties of another class.  The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is inheritance.
  • 3.
    OVERVIEW OF INHERITANCE Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called the Parent or Base or Super class. And, the class which inherits properties of other class is called Child or Derived or Sub class.  Inheritance lets you create new classes from existing class. Any new class that you create from an existing class is called derived class; existing class is called base class.
  • 4.
    ADVANTAGES OF INHERITANCE Sometimes we need to repeat the code or we need repeat the whole class properties. So It helps in various ways. 1.) It saves memory space. 2.) It saves time. 3.) It will remove frustration. 4.) It increases reliability of the code 5.) It saves the developing and testing efforts
  • 5.
    WHY WE USEINHERITANCE? To increase the reusability of the code and to make further usable for another classes. We use the concept of inheritance.
  • 6.
  • 7.
    BASIC SYNTAX OFINHERITANCE  class Subclass_name : access mode Superclass_name  While defining a subclass like this, the super class(base class)must be already defined or at least declared before the subclass(derived class) declaration.  Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, private or protected.
  • 9.
  • 10.
    SINGLE INHERITANCE  Insingle level inheritance, there is only one base class and has only one derived class.  Example of single level inheritance: class Base { }; class Derv: public Base { };
  • 11.
    EXAMPLE OF SIMPLECLASS INHERITANCE  class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout << d.legs; cout << d.tail; } Output : 4 1
  • 12.
    MULTIPLE INHERITANCE  AC++ class can inherit members from more than one class and here is the extended syntax:  class derived-class: access baseA, access baseB....  Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above.
  • 13.
    EXAMPLE OF MULTIPLEINHERITANCE #include<iostream.h> #include<conio.h> class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"; cin>>m1>>m2; } };
  • 14.
    class sports { protected: int sm;// sm = Sports mark public: void getsm() { cout<<"nEnter the sports mark :"; cin>>sm; } }; class statement:public student,public sports { int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot; cout<<"ntAverage : "<<avg; }
  • 15.
    void main() { clrscr(); statement obj; obj.get(); obj.getsm(); obj.display(); getch(); } Output: Enterthe Roll no: 100 Enter two marks 90 80 Enter the Sports Mark: 90 Roll No: 100 Total : 260 Average: 86.66
  • 16.
  • 17.