Calculate Percentage of each subject using Multilevel Inheritance
In this, we are going to see a program to calculate percentage of each subject using Multilevel 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, cs;
    char stuName[50];
    void Details()
    {
        cout << "Enter student details: \n";
        cout << "Enter Student ID: ";
        cin >> stuId;
        cout << "Enter Student Name: ";
        cin >> stuName;
        cout << "Enter Student's math marks out of 80: ";
        cin >> math;
        cout << "Enter Student's phy marks out of 80: ";
        cin >> phy;
        cout << "Enter Student's chem marks out of 80: ";
        cin >> chem;
        cout << "Enter Student's cs marks out of 80: ";
        cin >> cs;
    }
};

class Percent : public Student
{
    public:
    float m, p, ch, comp, aggPercent;
    void Math()
    {
        m = (float)math / 80 * 100;
    }
    void Phy()
    {
        p = (float)phy / 80 * 100;
    }
    void Chem()
    {
        ch = (float)chem / 80 * 100;
    }
    void CS()
    {
        comp = (float)cs / 80 * 100;
    }
    void AggPercent()
    {
        aggPercent = (m + p + ch + comp) / 4;
    }
};

class Report : public Percent
{
    public:
    void repshow()
    {
        cout << "\nMath Percentage = " << m;
        cout << "\nPhy Percentage = " << p;
        cout << "\nChem Percentage = " << ch;
        cout << "\nCS Percentage = " << comp;
        cout << "\nAggregate Percentage = " << aggPercent;
    }
};

void main()
{
    clrscr();
    Report stu;
    stu.Details();
    stu.Math();
    stu.Phy();
    stu.Chem();
    stu.CS();
    stu.AggPercent();
    stu.repshow();
    getch();
}

//...........Coded by Sahil Shaikh


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class Student
{
    public:
    int stuId, phy, chem, math, cs;
    string stuName;
    void Details()
    {
        cout << "Enter student details: \n";
        cout << "Enter Student ID: ";
        cin >> stuId;
        cout << "Enter Student Name: ";
        cin >> stuName;
        cout << "Enter Student's math marks out of 80: ";
        cin >> math;
        cout << "Enter Student's phy marks out of 80: ";
        cin >> phy;
        cout << "Enter Student's chem marks out of 80: ";
        cin >> chem;
        cout << "Enter Student's cs marks out of 80: ";
        cin >> cs;
    }
};

class Percent : public Student
{
    public:
    float m, p, ch, comp, aggPercent;
    void Math()
    {
        m = (float)math / 80 * 100;
    }
    void Phy()
    {
        p = (float)phy / 80 * 100;
    }
    void Chem()
    {
        ch = (float)chem / 80 * 100;
    }
    void CS()
    {
        comp = (float)cs / 80 * 100;
    }
    void AggPercent()
    {
        aggPercent = (m + p + ch + comp) / 4;
    }
};

class Report : public Percent
{
    public:
    void repshow()
    {
        cout << "\nMath Percentage = " << m;
        cout << "\nPhy Percentage = " << p;
        cout << "\nChem Percentage = " << ch;
        cout << "\nCS Percentage = " << comp;
        cout << "\nAggregate Percentage = " << aggPercent;
    }
};

int main()
{
    Report stu;
    stu.Details();
    stu.Math();
    stu.Phy();
    stu.Chem();
    stu.CS();
    stu.AggPercent();
    stu.repshow();
    return 0;
}

//...........Coded by Sahil Shaikh


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post