What is Single Inheritance in C++ (Single Level)

Single Inheritance in C++

Inheritance is the type of intelligence where software extension is possible and where  you can have more properties than the previous software

Definition: In the single Inheritance a class is acquiring the properties and capabilities of  from a single class

  • The class that is acquiring the behaviors is called child class or derived class or subclass
  • The class from which behaviors are taken is called parent class or superclass or base class

Syntax to implement Single Inheritance

class <Parent>//super class
{
methods;
variables;
-----;
}
class <Child> : Acesss_Specifier <parent> //derived class
{
Parent Methods//extended from parent
Child methods;//written by itself
}
Single Inheritance in C++

Single Inheritance in Public Mode

If the derivation is done in public mode then all the then public and protected members of the parent  class will have the same public and protected access in the child class

class Parent//parent class
{
public:
void parent_property()//parent class method
{
cout<"\ncash+gold+land+vehicles";
}
int p=10;
};
class Child:public Parent//derivation mode as public
{
public:
void child_property()//Child specific data
{
cout<<"\n3bhhk Flat";
}
// contains 2 methods property() and 1 variable P;
};
int main()
{
Child c;
c.child_property();
c.parent_property();//child object accessing parent method
Parent p;
p.parent_property();
//p.child_property();//error:child has no such method
}

O/P

3bhhk Flat
cash+gold+land+vehicles
cash+gold+land+vehicles
  • In Inheritance, Child is superior to Parent because the child has its own data and data from the parent whereas Parent has only parent data
  • Here the child has both methods its own method child_property() and derived method parent_property()   whereas Parent class has only one method parent_property()
  • Hence It is advisable to create an object for the child rather than the parent

Private Members cannot be accessed in Child class

Suppose If we are having 10 methods we need to give 8 methods to the child and stop 2 methods accessing to the child  class, then that 2 methods are declared as private

class Parent//parent class
{
public:
void parent_property()//parent class method
{
cout<"\ncash+gold+land+vehicles";
}
int p=10;
private:
void donation_tocharity()//not given to child 
{
cout<<"\n100 crores";
}
};
class Child: public Parent//parent data is having public access inChild
{
public:
void child_property()//Child specific data
{
cout<<"\n3bhhk Flat";
}
// contains 2 methods property() and 1 variable P;

};
int main()
{
Child c;
c.child_property();
c.parent_property();//child object accessing parent method
Parent p;
//p.child_property();//error:child has no such method
p.donation_tocharity();//parents own method
//error: ‘void Parent::donation_tocharity()’ is private
}

O/P:

3bhhk Flat
cash+gold+land+vehicles
100 crores

In this example, donation_tocharity() is a private method of parent hence even if a child inherits that  all methods except private methods are derived

Please Login/Signup to comment