The genetic characters transmitted from parent to offspring, taken collectively.
Something, as a quality, characteristic, or other immaterial possession, received from progenitors or predecessors as if by succession.
↓↓↓↓ Read More:
@ Kindly Follow my Instagram Page to discuss about your mental health problems-
-----> https://instagram.com/mentality_streak?utm_medium=copy_link
@ Appreciate my work:
-----> behance.net/burhanahmed1
Thank-you !
How to do quick user assign in kanban in Odoo 17 ERP
Inheritance
1.
2. INTRODUCTION
Inheritance is the most important feature of OOP. It is
a form of a program re-usability in which new classes
are made by using code of existing classes.
Program/Software re-usability saves times of
programmer in program development.
The word “inherit” means “to receive”.
3. Definition
Inheritance is the process of creating new classes,
called “derived class” from existing classes called “base
classes”.
The base class is also known as super class or parent
class while derived class is also known as sub class or
child class.
5. Figure Explained…
The above figure shows the relationship between a
derived class and the base class. The arrow is drawn
from the derived class to the base class. The direction
of the arrow indicates that the derived class can access
members of the base class but base class cannot access
members of its derived class.
The derived class inherits all the capabilities of the
base class. It means that it can use the data members
and member function of its base class. It can also have
its own data members and member functions.
6. Cont…
So the derived class can be larger than its base class.
Each derived class it self becomes a candidate to be a
base class for some future derived classes.
7. Categories Of Inheritance
A new class can be derived from one or more existing
classes. Therefore inheritance is further divided into
different categories given below:
1) Single Inheritance
2) Multiple Inheritance
3) Multilevel Inheritance
8. Single Inheritance
With single inheritance, a new class is derived from
one base class. It is very simple and straight forward
process.
For example: A new class B is derived from base class
A. The derived class inherits all the attributes(data
members) and operations (member function) of the
base class.
10. Explanation
In figure, the derived class has two members of its own
and the members shown with dark shadow represents
the members of the base class. The derived class can
also access these two members of the base class. Thus
an object of the base class can access only two
members, while an object of the derived class can
access four members (two members of the base class
and tow of the derived class)
11. Defining Derived Class with Single
Inheritance
A derived class is defined in the similar way as base
class is defined. The syntax for defining a derived class
is slightly different from syntax of base class definition.
The general syntax for defining a derived class is:
class derived_class_name : access_specifier base_class
{
Body of derived class
}
12. Example:
#include<iostream>
using namespace std;
class std_address //base class
{
private:
int code;
string name, phone_no;
public:
void input();
void display();
};
// derived class
class std_result : public std_address
{
private:
float sub1, sub2,sub3, average ,
total;
public:
void input_marks();
void display_marks();
};
13. Cont…
//Defining Member Functions of Base
Class
void std_address :: input()
{
cout<<“Enter code of student”<<endl;
cin>>code;
cout<<“Enter name of student”<<endl;
cin>>name;
cout<<“Enter phone of student”<<endl;
cin>>phone_no;
}
void std_address :: display()
{
cout<<“Code : “<<code<<endl;
cout<<“Name : “<<name<<endl;
cout<<“Phone : “<<phone_no<<endl;
}
//Defining Member Functions of Derived Class
void std_result :: input_marks();
{
cout<<“Enter marks of first subject?”<<endl;
cin>>sub1;
cout<<“Enter marks of second subject?”<<endl;
cin>>sub2;
cout<<“Enter marks of third subject?”<<endl;
cin>>sub3;
total = sub1+sub2+sub3;
Average = total/3;
}
14. void std_result :: display_marks()
{
cout<<“Marks of first subject: “<<sub1;
cout<<“Marks of second subject:“<<sub2;
cout<<“Marks of third subject: “<<sub3;
cout<<“Total Marks :“<<total;
cout<<“Average marks : “<<average;
}
//main function
int main()
{
std_result res;
res.input();
res.input_marks();
res.display();
res.display_marks();
}
15. Multiple Inheritance
A new class can be derived from more than one base class
(or multiple base classes).
This type of inheritance is called multiple inheritance.
In multiple inheritance, the derived class inherits members
of all base classes. It can also have its own data members.
Multiple inheritance reduces the program size and saves
program development time in single inheritance.
Multiple inheritance is complex than single inheritance.
Some object oriented programming language do not
support this feature but C++ support this feature.
17. Example
#include<iostream>
using namespace std;
class std_address
{
private:
string name, city;
public:
void input();
void print();
};
class std_marks
{
private:
float sub1, sub2, sub3, average, total;
public:
void input_marks()
void print_marks()
};
class std_result : public std_address, public std_marks
{
public:
void display()
{
cout<<“COMPLETE RECORD”<<endl;
print();
print_marks();
}
};
18. int main()
{
std_result res;
res.input();
res.input_marks();
res.display();
}
void std_address:: input()
{
cout<<“Enter student name”<<endl;
cin>>name;
cout<<“Enter name of city”<<endl;
cin>>city;
void std_address :: print()
{
cout<<“Name: “<<name<<endl;
cout<<“City: “<<city<<endl;
}
void std_marks :: input_marks()
{
cout<<“Enter marks of first subject”<<endl;
cin>>sub1;
cout<<“Enter marks of second subject”<<endl;
cin>>sub2;
cout<<“Enter marks of third subject”<<endl;
cin>>sub3;
total = sub1+sub2+sub3;
19. average = total/3;
}
void std_marks :: display()
{
cout<<“Marks of first subject: “<<sub1<<endl;
cout<<“Marks of second subject: “<<sub2<<endl;
cout<<“Marks of third subject: “<<sub3<<endl;
cout<<“Total marks: “<<total<<endl;
cout<<“Average marks:” <<average<<endl;
}
20. Multilevel Inheritance
A class can be derived from another derived class. The
type of inheritance is called multilevel inheritance. For
example if a class “C” is derived from base class “B” and
a new class “D” is derived from derived class C. Then
the derived class D will inherit all the capabilities of C
and B.
21. Syntax
class A
{
body of class A;
};
class B : public A
{
body of derived class B;
};
class C : public B
{
body of derived class C;
};
22. Example
#include<iostream>
class values
{
private:
float m, n;
public:
float assign(float x, float y)
{
m = x;
n = y;
}
};
class sum : public values
{
public:
float sum()
{
cout<<“sum of”<<m<<“and”<<n<<“=“<<m+n;
}
};
class product : public sum
{
float pro()
{
cout<<“product of”<<m<<“and”<<n<<“=“<<m*n;
}
};
25. Types of inheritance
The accessibility of members of the base class can be
controlled by using private, public and protected
Specifier. Thus, the inheritance can be divide on the
basis of accessing members of the base class.
Therefore, types of inheritance are:
Private Inheritance
Public Inheritance
Protected Inheritance
26. Private Inheritance
With private inheritance public and protected
members of the base class become the private
members of the derived class. The public and
protected members of the base class can only be
accessed inside the derived class but private members
of base class cannot be accessed inside the derived
class.
27. Public Inheritance
With public inheritance public members of the base
class become the public members of derived class.
Similarly, protected members of the base class
becomes the protected members of the derived class.
28. Protected Inheritance
The protected inheritance is similar to private
inheritance. It is rarely used. With protected
inheritance, public and protected members of the base
class become the protected member of derived class.
The object of derived class can access only protected
members of base class.