Monday, April 27, 2009

Single Inheritance


Single Inheritance
Deriving a new derived class from single base class is called single inheritance

Class B is derived from single base class A, here Class A is called base class and Class B is called derived class. Using Derived class object, we can access the members of both derived and bases class. But using Base class object we can not access members of derived class.

Example:
header files
class student
{
int rno;
char name[50];
char address[30];
public:
void get();
void display();
};
void student::get()
{
cout<<”Enter the Student Roll Number:”;
cin>> rno;
cout<<”Enter the student Name :”;
cin>> name;
cout<<”Enter the Student Address:”;
cin>> address;
}
void student::display()
{
cout<<”Roll No.: “<<>
cout<<”Name : “<<>
cout<<”Address :”<<>
}
class mark:public student
{
int mark1, mark2, mark3, mark4, mark5;
float avg;
public:
void getmark();
void displaymark();
};
void mark::getmark()
{
cout<<”Enter the five subject Marks one by one”<<”\n”;
cin>> mark1>> mark2>> mark3>> mark4>> mark5;
total=mark1+ mark2+ mark3+ mark4+ mark;
avg=total/5;
}
void mark::displaymarks()
{
cout<< mark1<< endl;
cout<< mark1<< endl;
cout<< mark2<< endl;
cout<< mark3<< endl;
cout<< mark4<< endl;
cout<< mark5<< endl;
cout<<“Total =“<< total<< endl;
cout<<“Average =“<
}
void main()
{
clrscr();
mark obj1;
obj1.get();
obj1.getmark();
obj1.diaplay();
obj1.displaymark();
getch();
}
Output will be
Enter the Student Roll Number: 101
Enter the student Name: Lak
Enter the Student Address: Chennai
Enter the five subject Marks one by one
80
75
80
90
100

80
75
80
90
100
Total = 425
Average = 85

No comments:

Post a Comment