Inheritance in Object Oriented Programming:

The general form or the syntax of specifying a derived class is :

Class derivedclassname: acessspecifies baseclassname
{
//body of the derived class
}

The colon indicates that, the derived class named derivedclassname is derived from the base class named baseclassname.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:
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 Code:
#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();
}

Output:
Enter data for student 1
Enter your name:Ram
Enter your number:1
Enter 3 subject mark:75 65 85
Enter data for student 2
Enter your name :Sham
Enter your number:2
Enter marks in 3 subjects:65 85 75

The data for student 1
Name:Ram
Number:1
Marks of three subjects:75 65 85
The data for student 2
Name :Sham
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: