The Inheritance - A Porsche, a palace and a lot of jewellery

By
A Porsche, a palace and a lot of jewellery : The Inheritance
@codebasics

What’s the first thing that crosses your mind when you hear the word ‘inheritance’?
Money, house, jewellery would be the first for most of you. Because that’s what we usually inherit from our elders, among other important things like values, good/bad name etc.
When class A inherits member functions and member variables from class B,

Then
Class A becomes a subclass of class B &
Class B becomes a parent class of class A.

After class A has inherited class B, following things (can) happen:
1. Class A can access all the Public and Protected members in class B. Like, you can access your parents’ money(protected) and their house(public).
2. Class A CANNOT access Private members of class B. Like, you cannot inherit your parents’ passports or driving licenses.
3. Class A can become a parent class for class C and now class C can also access the members of class B because parent class of C is also a subclass of B.
                            B→A→C (MULTLEVEL INHRITANCE)
4. Class A can also inherit another class D to have 2 parent classes.
                            B→A, D→A (MULTIPLE INHERITANCE)


MULTILEVEL INHERITANCE:
A subclass can also be inherited to become a parent class for its subclasses.

For e.g.,

#include <iostream>
using namespace std;

class A
{
public:
void display()
{
cout<<"Base class content.";
}
};

class B : public A //class B inherits class A in Public mode
{

};
class C : public B // class C inherits class B in public mode
{

};
int main()
{
C obj;
obj.display(); // member function inherited from A
return 0;
}



Output:
Base class content.
MULTIPLE INHERITANCE:
When a class has two direct parent classes.



For e.g., 

#include <iostream>
using namespace std;

class Mammal
{
public:

Mammal() // Constructor, runs automatically in object creation
{
cout << "Mammals can give direct birth." << endl;
}
};

class WingedAnimal
{
public:

WingedAnimal() // Constructor, runs automatically in object creation
{
cout << "Winged animal can flap." << endl;
}
};

class Bat: public Mammal, public WingedAnimal
// class Bat inherits both classes in public mode

{
};
int main()
{
Bat b1;
return 0;
}


Output:
Mammals can give direct birth.
Winged animal can flap.



Modes of Inheritance:
1. Private:
Every inherited member becomes a private member of the subclass. Even the child classes of the subclasses cannot access them. These members are not accessible outside the class.

2. Protected:
Every inherited member becomes a protected member of the subclass. Only the child classes of the subclasses can access them. These members are not accessible outside the class.

3. Public:
Every inherited member becomes a public member of the subclass. All the child classes of the subclasses can access them. These members are also accessible outside the class.

Special thanks to @codebasics . You people are doing great job . Hope it will help our readers. 

0 comments:

Post a Comment