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 USE INHERITANCE?
To increase the reusability of the code
and to make further usable for another
classes. We use the concept of
inheritance.
7. 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.
10. 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
{
};
11. 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
12. 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.
13. 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;
}
};
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;
}