0% found this document useful (0 votes)
39 views11 pages

Inheritance 1

Multiple inheritance allows a derived class to inherit features from multiple base classes. This can reduce duplication but also increases complexity as there is potential for ambiguity and conflicts between the different base classes.

Uploaded by

THANU SREE
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)
39 views11 pages

Inheritance 1

Multiple inheritance allows a derived class to inherit features from multiple base classes. This can reduce duplication but also increases complexity as there is potential for ambiguity and conflicts between the different base classes.

Uploaded by

THANU SREE
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/ 11

Inheritance

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.

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.

Advantages
reusability and readability When child class inherits the
properties and functionality of parent class, we need not to
write the same code again in child class. This makes it easier to
reuse the code, makes us write the less code and the code
becomes much more readable.
Implementing inheritance in C++
For creating a sub-class which is inherited from the base class
follow the below syntax.
Syntax:
Class subclass_name : access_mode base_class_name
{
//body of subclass
};

• Here, subclass_name is the name of the sub class,


access_mode is the mode in which you want to inherit this
sub class for example: public, private etc. and
base_class_name is the name of the base class from which
you want to inherit the sub class.
Types Of Inheritance
C++ supports five types of inheritance:
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
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.

• 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 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.
// C++ Implementation to show that a
derived class doesn’t inherit access to
private data members. However, it does
inherit a full parent object
class A class C : protected A
{ {
public: // x is protected
// y is protected
int x;
// z is not accessible from C
protected: };
int y;
private: class D : private A // 'private' is default for
int z; classes
}; {
// x is private
// y is private
class B : public A // z is not accessible from D
{ };
// x is public
// y is protected
// z is not accessible from B
};
• The below table summarizes the above three modes and
shows the access specifier of the members of base class in the
sub class when derived in public, protected and private
modes:
• 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.

Syntax:

class subclass_name : access_mode base_class


{
//body of subclass
};
#include<iostream.h> class physical:public student
#include<conio.h> {
private:
class student
float h,w;
{ public:
private: void getdetails()
int id; {
char name[10]; cout<<"Enter student height and weight:";
public: cin>>h>>w;
}
void getdata()
void showdetails()
{ {
cout<<"Enter student data:"; cout<<"The height and weight of a student
cin>>id>>name; is:"<<h<<endl<<w<<endl;
} }
void showdata() };
void main()
{
{
cout<<"Student details physical p;
are:"<<id<<endl<<name<<endl; clrscr();
} p.getdata();
}; p.showdata();
p.getdetails();
• Multilevel Inheritance: In this type of inheritance, a derived
class is created from another derived class.
#include<iostream.h> class marks:public student void showgrades()
#include<conio.h> { {
protected:
class student cout<<"Total
float m1,m2,m3;
{ public:
marks
private: void getmarks() is :"<<tot<<endl;
{ cout<<"Average
int id;
cout<<"Enter 3 subjects marks:"; is:"<<avg;
char name[10]; cin>>m1>>m2>>m3; }
public: } };
void getdata() void showmarks()
{
{ void main()
cout<<"marks in three subjects
cout<<"Enter student are:"<<m1<<endl<<m2<<endl<<m3 {
details:"; <<endl; clrscr();
} calculate c;
cin>>id>>name;
}; c.getdata();
} class calculate:public marks c.showdata();
void showdata() { c.getmarks();
{ private: c.showmarks();
float tot,avg;
cout<<"Student details c.getgrades();
public:
are:"<<id<<endl<<name<<end void getgrades() c.showgrades();
l; { getch();
} tot=m1+m2+m3; }
avg=tot/3;
}; }
• 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.

Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
//body of subclass
};

Here, the number of base classes will be separated by a comma (‘, ‘) and access
mode for every base class must be specified.

You might also like