Today I will tell you about IN C++ What Does Single Inheritance Mean?. It is a crucial topic in C++. A derived class can inherit properties and behavior from a single parent class using single inheritance.
It allows a derived class to inherit a base class's properties and behavior, making the code considerably more elegant and less repetitive. One of the most important characteristics of object-oriented programming is inheritance (OOP).
Process exited after 0.1907 seconds with return value 0 Press any key to continue . . .
WHAT IS MULTIPLE INHERITANCE?
It is a crucial topic in C++. The many A sort of inheritance in which a class derives from multiple classes is known as inheritance. As a parent, it has class A and class B. One of the most important aspects of object-oriented programming is inheritance (OOP).
INPUT:
#include<iostream>
using namespace std;
class father
{
public:
void land()
{
cout<<"\nFATHER HAS OWN LAND";
}
};
class mother
{
public:
void gold()
{
cout<<"\nMOTHER HAS 5KG GOLD";
}
};
class son:public father,public mother
{
public:
void car()
{
cout<<"\nSON HAS OWN CAR";
}
};
int main()
{
son o;
o.land();
o.gold();
o.car();
}
OUTPUT:
FATHER HAS OWN LAND
MOTHER HAS 5KG GOLD
SON HAS OWN CAR
--------------------------------
Process exited after 0.1632 seconds with return value 0
Press any key to continue . . .
.
WHAT DOES IT MEAN TO HAVE MULTILEVEL INHERITANCE?
It is quite significant. A class is derived from another derived class under multilevel inheritance in C++.
Our implementations allow us to have as many levels of inheritance as we like.
INPUT:
#include<iostream>
using namespace std;
class grandfather
{
public:
void house()
{
cout<<"\nGRANDFATHER HAS OWN HOUSE";
}
};
class father:public grandfather
{
public:
void land()
{
cout<<"\nFATHER HAS OWN LAND";
}
};
class son:public father
{
public:
void car()
{
cout<<"\nSON HAS OWN CAR";
}
};
int main()
{
son o;
o.car();
o.land();
o.house();
}
SON HAS OWN CAR
FATHER HAS OWN LAND
GRANDFATHER HAS OWN HOUSE
--------------------------------
Process exited after 0.131 seconds with a return value of 0
Press any key to continue . . .
WHAT IS HYBRID INHERITANCE AND HOW DOES IT WORK?
Hybrid inheritance is when more than one form of inheritance is combined.
INPUT
#include<iostream>
using namespace std;
class grandfather
{
public:
void house()
{
cout<<"\nGRANDFATHER HAS OWN HOUSE";
}
};
class father:public grandfather
{
public:
void land()
{
cout<<"\nFATHER HAS OWN LAND";
}
};
class mother
{
public:
void gold()
{
cout<<"\nMOTHER HAS 5KG GOLD";
}
};
class son:public father,public mother
{
public:
void car()
{
cout<<"\nSON HAS OWN CAR";
}
};
int main()
{
son o;
o.car();
o.gold();
o.house();
o.land();
}
SON HAS OWN CAR
MOTHER HAS 5KG GOLD
GRANDFATHER HAS OWN HOUSE
FATHER HAS OWN LAND
--------------------------------
Process exited after 0.0775 seconds with return value 0
Press any key to continue . . .
WHERE DOES HIERARCHICAL INHERITANCE COME FROM?
More than one class inherits from a single basic class in hierarchical inheritance.
INPUT:
#include<iostream>
using namespace std;
class grandfather
{
public:
void house()
{
cout<<"\nGRANDFATHER HAS OWN HOUSE";
}
};
class father:public grandfather
{
public:
void land()
{
cout<<"\nFATHER HAS OWN LAND";
}
};
class mother
{
public:
void gold()
{
cout<<"\nMOTHER HAS 5KG GOLD";
}
};
class son:public father,public mother
{
public:
void car()
{
cout<<"\nSON HAS OWN CAR";
}
};
class daugther:public father,public mother
{
public:
void company()
{
cout<<"\nDAUGHTER HAS OWN COMPANY";
}
};
int main()
{
son o;
o.car();
o.gold();
o.house();
o.land();
cout<<"\n\n----------------";
Daughter o1;
o1.company();
o1.gold();
o1.house();
o1.land();
}
OUTPUT:
SON HAS OWN CAR
MOTHER HAS 5KG GOLD
GRANDFATHER HAS OWN HOUSE
FATHER HAS OWN LAND
----------------
DAUGHTER HAS OWN COMPANY
MOTHER HAS 5KG GOLD
GRANDFATHER HAS OWN HOUSE
FATHER HAS OWN LAND
--------------------------------
Process exited after 0.3478 seconds with a return value of 0
Press any key to continue . . .
Difference between Single and Multiple Inheritance in C++
Single Inheritance:
The derived class inherits the single base class either openly, privately, or protected in single inheritance.
The derived class uses the features or members of the single base class in single inheritance. According to the access specifier supplied when inheriting the parent class or base class, these base class members can be accessed by derived classes or child classes.
Syntax:
Class DerivedClass_name : access_specifier Base_Class
{
//Class's Body
};
Example of Single Inheritance:
When a derived class inherits from two or more base classes, this is known as multiple inheritances.
The derived class can leverage the shared features of the inherited base classes when multiple inheritances are used.
Syntax:
Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2
{
//Class's Body
};
Example of Multiple Inheritance:
#include<iostream>
using namespace std;
class A {
public:
float salary = 80000;
};
class B {
public:
float bonus = 8000;
};
class C : public A, public B {
public:
void ts()
{
cout<<"Total salary.."<<(salary + bonus)<<endl;
}
};
int main()
{
C b1;
b1.ts();
return 0;
}
Output:
Total salary..88000
In this example, A and B are the base classes and C is the derived class.
0 Comments