Single level Inheritance
Single level Inheritance: Single level inheritance and inheritance are same where we can derive a new class from its base class(only one base class).
Consider a class A(Base class) and class B(derived class). here the derived class contains the information off its base class like the operators, constructors, access modifiers etc.. In object - oriented programming languages this inheritance is one of the major advantage factor. As this inheritance allows message passing and data hiding feature to these oops language.
The Syntax for these single level inheritance is as follows:
class derived_classname : visibility_mode base_classname
E.G. : class test : public school
here test is a derived class name and the visibility mode is public because it can be accessed through its base class but if we declare private the base class cannot be accessed so in order to avoid such kind of data loss we use public access specifier and school is the base class name from where this test has been derived.
Now let us consider an Example based on these single level inheritance:
#include<iostream>
using namespace std;
class school
{
private:
char name[20];
int reg_no;
public:
void input1()
{
cout<<"Enter the name and reg_no"<<endl;
cin>>name>>reg_no;
}
void output1()
{
cout<<"Your Name is:"<<name<<endl;
cout<<"Your reg_no is:"<<reg_no<<endl;
}
};
class Student:public school
{
private:
char name[20];
int mob_no;
void input2()
{
cout<<"Enter Father name and mobile number"<<endl;
cin>>name>>mob_no;
}
void output2()
{
cout<<"Your father name is"<<name<<endl
cout<<"Your mobile number is"<<mob_no<<endl;
}
};
int main()
{
Student s;
s.input1();
s.input2();
s.output1();
s.output2();
return 0;
}
0 Comments