Sunday 25 September 2011



Virtual base class


Aim:
To write a C++ program to display student details using virtual base class.
Algorithm:
Step 1: Create a base class as student.
1.1 Declare the data members as rollno.
1.2 Declare the member functions as
void get(int r)
void put()
1.3 get(int r) function is used to assign the r value to rollno.
1.4 put() function is used to display the rollno.
Step 2: Create a derived class test with virtual keyword. (Base class – student)
2.1 Declare the data members mark1 and mark2.
2.2 Declare the member functions as
void get1(int y,int z)
void put1()
2.3 get1(int y,int z) function is used to assign the value of y and z to mark1 and
mark2..
2.4 put1() function is used to display the department and college name.
Step 3: Create a derived class as sport with virtual keyword. (Base class – student)
3.1 Declare the data member score.
3.2 Declare the member functions as
void get2(int  s)
void put2()
3.3 get2(int s) function is used to assign the value of s to score.
3.4 put2() function is used to display the value of score.
Step 4: Create a derived class as result. (Base classes – test and sport)
3.1 Declare the data member total.
3.2 Declare the member function as
void put3()
3.3 put3() function is used to display the total value.
Step 5: In the main, create the object for the derived class result.
Step 6.Call the functions.
Step 7: 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;
}
};

class sport:virtual public student
{
protected:
int score;
public:
void get2(int s)
{
score=s;
}
void put2()
{
cout<<"\nScore:"<<score;
}
};

class result:public test,public sport
{
int total;
public:
void put3()
{
total=mark1+mark2+score;
put();
put1();
put2();
cout<<"\nTotal:"<<total;
}
};

void main()
{
clrscr();
result R;
R.get(101);
R.get1(97,99);
R.get2(5);
cout<<"\nSTUDENT DETAIL:\n";
R.put3();
getch();
}

Output:

STUDENT DETAIL:

Roll:101
Mark1:97
Mark2:99
Score:5
Total:201

0 comments:

Post a Comment