It is the process of deriving only one new class from single base class.
Here A is the base class and D is the derived class.
The syntax and example for single inheritance in case of C++ is given below:
Syntax:
class A
{
-------
-------
};
class D : visibility_label A
{
-------
-------
};
Here the visibility_label can be private, protected or public. If we do
not specify any visibility_label then by default is private.
For example:
class person //Declare base class
{
private:
char name[30];
int age;
char address[50];
public:
void get_data ( );
void put_data ( );
};
class student : private person //Declare derived class
{
private:
int rollno;
float marks;
public:
void get_stinfo ( );
void put_stinfo ( );
};
void person::get_data ( )
{
cout<<”Enter name:”;
cin>>name;
cout<<”Enter age:”;
cin>>age;
cout<<”Enter address:”;
cin>>address;
}
void person::put_data ( )
{
cout<<”Name: “ <<name;
cout<<”Age: “ <<age;
cout<<”Address: “ <<address;
}
void student::get_stinfo ( )
{
get_data ( );
cout<<”Enter roll number:”;
cin>>rollno;
cout<<”Enter marks:”;
cin>>marks;
}
void student::put_stinfo ( )
{
put_data ( );
cout<<”Roll Number:” <<rollno;
cout<<”Marks:”< <marks;
}
void main ( )
{
student st;
clrscr ( );
st.get_stinfo ( );
st.put_stinfo ( );
getch ( );
}
Here ‘person’ is base class and ‘student’ is derived class that has
been inherited from person privately. The private members of the
‘person’ class will not be inherited. The public members of the
‘person’ class will become private members of the ‘student’ class since
the type of the derivation is private. Since the inherited members have
become private they can not be accessed by the object of the ‘student’
class.