Calculate Percentage of each subject || Single Inheritance || C++

In this, we are going to see a program to Calculate Percentage of each subject using Single 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;
    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 outof 80: ";
        cin >> math;
        cout << "Enter Student's phy marks outof 80: ";
        cin >> phy;
        cout << "Enter Student's chem marks outof 80: ";
        cin >> chem;
        cout << "Enter Student's cs marks outof 80: ";
        cin >> cs;
    }
};
class Percent : public Student
{
    public:
    float m, p, ch, comp, aggPercent;
    void Math()
    {
        m = (float)math / 80 * 100;
        cout << "\nMath Percentage = " << m;
    }
    void Phy()
    {
        p = (float)phy / 80 * 100;
        cout << "\nPhy Percentage = " << p;
    }
    void Chem()
    {
        ch = (float)chem / 80 * 100;
        cout << "\nChem Percentage = " << ch;
    }
    void CS()
    {
        comp = (float)cs / 80 * 100;
        cout << "\nCS Percentage = " << cs;
    }
    void AggPercent()
    {
        aggPercent = (m + p + ch + comp) / 4;
        cout << "\nAggregate Percentage = " << aggPercent;
    }
};

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

//...........Coded by Shreya Idate

 

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 outof 80: ";
        cin >> math;
        cout << "Enter Student's phy marks outof 80: ";
        cin >> phy;
        cout << "Enter Student's chem marks outof 80: ";
        cin >> chem;
        cout << "Enter Student's cs marks outof 80: ";
        cin >> cs;
    }
};
class Percent : public Student
{
public:
    float m, p, ch, comp, aggPercent;
    void Math()
    {
        m = (float)math / 80 * 100;
        cout << "\nMath Percentage = " << m;
    }
    void Phy()
    {
        p = (float)phy / 80 * 100;
        cout << "\nPhy Percentage = " << p;
    }
    void Chem()
    {
        ch = (float)chem / 80 * 100;
        cout << "\nChem Percentage = " << ch;
    }
    void CS()
    {
        comp = (float)cs / 80 * 100;
        cout << "\nCS Percentage = " << cs;
    }
    void AggPercent()
    {
        aggPercent = (m + p + ch + comp) / 4;
        cout << "\nAggregate Percentage = " << aggPercent;
    }
};
int main()
{
    Percent stu;
    stu.Details();
    stu.Math();
    stu.Phy();
    stu.Chem();
    stu.CS();
    stu.AggPercent();
    return 0;
}

//...........Coded by Shreya Idate
    

#ENJOY CODING

Post a Comment

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

Previous Post Next Post