Write a C++ program to calculate the total mark of a student using the concept of inheritance and virtual base class.

Algorithm:
Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the functions getnumber() and putnumber().
Step 4: Create the derived class test virtually derived from the base class student.
Step 5: Declare and define the function getmarks() and putmarks().
Step 6: Create the derived class sports virtually derived from the base class student.
Step 7: Declare and define the function getscore() and putscore().
Step 8: Create the derived class result derived from the class test and sports.
Step 9: Declare and define the function display() to calculate the total.
Step 10: Create the derived class object obj.
Step 11: Call the function get number(),getmarks(),getscore() and display().
Step 12: Stop the program.




Program Code:
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};
class test:virtual public student
{
public:
int Mark1,Mark2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Mark1:";
cin>>Mark1;
cout<<"Mark2:";
cin>>Mark2;
}
void putmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tMark1:"<<Mark1;
cout<<"\n\tMark2:"<<Mark2;
}
};
class sports:public virtual student
{
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:"; cin>>score;
}
void putscore()
{
cout<<"\n\tSports Score is:"<<score;
}
}; class result:public test,public sports
{
int total;
public: void display()
{
total=Mark1+Mark2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\tTotal Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}




Output:
Enter Roll No: 2
Enter Marks
Mark1: 90
Mark2: 80
Enter Sports Score:80
Roll No: 2
Marks Obtained
Mark1: 90
Mark2: 80
Sports Score is: 80
Total Score is: 250
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: