Inheritance in Object-Oriented Programming

A class has specified data type and if you need another data type similar to the previous one with some additional features, instead of creating a new data type, C++ allows to inherit the members of the existing type and can add some more features to the new class. This property is referred to as inheritance.The old class is called as the base class and the new class is called a derived class or child class.




The general form or the syntax of specifying a derived class is :
Class derived_classname : access_specifies base_classname
{
//body of the derived class
}

The colon indicates that the derived class named derived_classname is derived from the base class named base_classname.The access specifier of the base class must be either private, protected or public. if no access specifier is present is assumed private by default.The body of the derived class contains member data and member functions of its own.
Single inheritance: A derived class with only one base class is called as single Inheritance.The derived class can access all the members of the base class and can add members of its own.

Write a program to explain Single inheritance.

Algorithm to explain Single inheritance:
step1:Take two classess teacher and student
step2:withina teacher class Enter name and number values with the help of getdata() function and display this data using putdata() function.
step3:within the student class Enter and display marks m1,m2,m3 with the help of getdata(),putdata() functions.
step4:within the student class extends teacher class and using data and their functions to the student class




Program implementation:
#include<iostream.h>
class teacher
{
private:
char name[25];
long number;
public:
void getdata()
{
cout<<”\n \t Enter your name:”;
cin>>name;
cout<<”\n \t Enter your number:”;
cin>>number;
}
void putdata()
{
cout<<”\n \t name:”<<name;
cout<<”\n \t number : ”<<number;

}
};
class student : public teacher
{
private:
int m1,m2,m3;
public:
void getdata()
{
teacher :: getdata();
cout<<”\n \t Enter marks in three subjects:”;
cin>>m1>>m2>>m3;
}
void putdata()
{
teacher :: putdata();
cout<<”\n \t Marks of three subjects:”<<m1<<m2<<m3;
}
};
void main()
{
student s1,s2;
cout<<”\n Enter data for student 1:\n”:
s1.getdata();
cout<<”\n Enter data for student2 :\n”;l
s2.getdata();
cout<<”\n the data of student 1:”;
s1.putdata();
cout<<”\n the data of student 2:”;
s2.putdata();
}

The output of the above program is:
Enter data for student1
Enter your name: James
Enter your number:1
Enter 3 subject mark:75 65 85
Enter data for student 2
Enter your name: Susila
Enter your number:2
Enter marks in 3 subjects:65 85 75
The data for student 1
Name: James

Number:1
Marks of three subjects:75 65 85
The data for student 2
Name: Susila
Number: 2
Marks of three subjects:65 85 75





Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: