Saturday 17 September 2011

INHERITANCE AND RTTI


Single inheritance
Aim:
To write a C++ program to display student details using single inheritance.
Algorithm:
Step 1: Create a base class as student.
1.1 Declare the data members as rollno and name[25].
1.2 Declare the member functions as
void get()
void put()
1.3 get() function is used to get the rollno and name.
1.4 put function is used to display the rollno and name.
Step 2: Create a derived class as person.
2.1 Declare the data members as dept[10] and college[10].
2.2 Declare the member functions as
void get1()
void put1()
  2.3 get1() function is used to get the dept and college.
2.4 put1() function is used to display the dept and college.
Step 3: In the main, create the object for the derived class person.
Step 4.Call the functions.
Step 5: Display the results.

Program:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
public:
void get(int r)
{
roll=r;
}
void put()
{
cout<<"\nRoll :"<<roll;
}
};

class test:virtual public student
{
protected:
int mark1,mark2;
public:
void get1(int y,int z)
{
mark1=y;
mark2=z;
}
void put1()
{
cout<<"\nMark1 :"<<mark1;
cout<<"\nMark2 :"<<mark2;
}
};

void main()
{
clrscr();
test R;
R.get(101);
R.get1(97,99);
cout<<"\nSTUDENT DETAIL:\n";
R.put();
R.put1();
getch();
}

Output:
STUDENT DETAIL:

Roll:101
Mark1:97
Mark2:99

0 comments:

Post a Comment