In this, we are going to see a program to calculate percentage of each subject using Multiple Inheritance in C++ Programming Language.Â
The Code given below can be used in TURBO C++ Compilers: -
#include<iostream.h>
#include<conio.h>
class student
{
public:
int stuid, phy, chem, math, comps;
string stuname;
void getdata()
{
cout << "***Enter Student details***" << endl;
cout << "Enter Student ID: ";
cin >> stuid;
cout << "Enter Student Name: ";
cin >> stuname;
cout << "Enter Math marks out of 80: ";
cin >> math;
cout << "Enter Phy marks out of 80: ";
cin >> phy;
cout << "Enter Chem marks out of 80: ";
cin >> chem;
cout << "Enter CS marks out of 80: ";
cin >> comps;
}
};
class calculate_percentage
{
public:
float a, b, c, d, e;
void calculate(int m, int p, int ch, int cs)
{
a = (float)m / 80 * 100;
b = (float)p / 80 * 100;
c = (float)ch / 80 * 100;
d = (float)cs / 80 * 100;
e = (a + b + c + d) / 4;
}
};
class display : public student, public calculate_percentage
{
public:
void print()
{
calculate(math, phy, chem, comps);
cout << "\nMath Percentage = " << a << "%" << endl;
cout << "Phy Percentage = " << b << "%" << endl;
cout << "Chem Percentage = " << c << "%" << endl;
cout << "CS Percentage = " << d << "%" << endl;
cout << "Aggregate Percentage = " << e << "%" << endl;
}
};
void main()
{
clrscr();
display s1;
s1.getdata();
s1.print();
getch();
}
//...........Coded by Ananya Vidyadharan
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class student
{
public:
int stuid, phy, chem, math, comps;
string stuname;
void getdata()
{
cout << "***Enter Student details***" << endl;
cout << "Enter Student ID: ";
cin >> stuid;
cout << "Enter Student Name: ";
cin >> stuname;
cout << "Enter Math marks out of 80: ";
cin >> math;
cout << "Enter Phy marks out of 80: ";
cin >> phy;
cout << "Enter Chem marks out of 80: ";
cin >> chem;
cout << "Enter CS marks out of 80: ";
cin >> comps;
}
};
class calculate_percentage
{
public:
float a, b, c, d, e;
void calculate(int m, int p, int ch, int cs)
{
a = (float)m / 80 * 100;
b = (float)p / 80 * 100;
c = (float)ch / 80 * 100;
d = (float)cs / 80 * 100;
e = (a + b + c + d) / 4;
}
};
class display : public student, public calculate_percentage
{
public:
void print()
{
calculate(math, phy, chem, comps);
cout << "\nMath Percentage = " << a << "%" << endl;
cout << "Phy Percentage = " << b << "%" << endl;
cout << "Chem Percentage = " << c << "%" << endl;
cout << "CS Percentage = " << d << "%" << endl;
cout << "Aggregate Percentage = " << e << "%" << endl;
}
};
int main()
{
display s1;
s1.getdata();
s1.print();
return 0;
}
//...........Coded by Ananya Vidyadharan
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP