What is Single Inheritance in C++ (Single Level)
Single level 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. In the single Inheritance a class is acquiring the properties and capabilities of from a single class.
Single Inheritance in C++
- 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 for implementing Single Inheritance
class //super class { methods; variables; -----; } class : Acesss_Specifier //derived class { Parent Methods//extended from parent Child methods;//written by itself }
Single Inheritance in Public Mode
If the derivation is done in public mode then all the public and protected members of the parent class will have the same public and protected access in the child class
#include <iostream> using namespace std; 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 }
Output
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
#include <iostream> using namespace std; 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: b
Output:
3bhhk Flat cash+gold+land+vehicles
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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment