0% found this document useful (0 votes)
6 views3 pages

Practical Program

The document outlines a C++ program that uses inheritance and virtual functions to calculate a student's total marks, average marks, and grade. It includes an algorithm detailing the steps for creating a base class 'Student' and a derived class 'Result' that overrides a virtual function to perform calculations. The main function demonstrates the program's functionality by creating an object of 'Result', collecting student data, and displaying the results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Practical Program

The document outlines a C++ program that uses inheritance and virtual functions to calculate a student's total marks, average marks, and grade. It includes an algorithm detailing the steps for creating a base class 'Student' and a derived class 'Result' that overrides a virtual function to perform calculations. The main function demonstrates the program's functionality by creating an object of 'Result', collecting student data, and displaying the results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Student Grade Calculation

Aim:

To devleop a c++ program using inheritance and virtual function to


calculate total marks, average marks, and grade of a student.

Algorithm:

Step 1: Start the program

Step 2: Create base class Student with name, roll and marks.

Step 3: Create function getData() to read input.

Step 4: Declare virtual function calculate().

Step 5: Create derived class Result.

Step 6: Override calculate() to compute total, average and grade.

Step 7: In main(), create object of Result.

Step 8: Use base class pointer to call overriden function

Step 9: Display student details and grade.

Step 10: Stop the program.

Program:

#include <iostream>
using namespace std;

class Student {
public:
string name;
int roll;
float mark1, mark2, mark3;
void getData() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll Number: ";
cin >> roll;
cout << "Enter Data structures marks: ";
cin >> mark1;
cout << "Enter c++ marks: ";
cin >> mark2;
cout << "Enter Maths marks: ";
cin >> mark3;
}

virtual void calculate() {


cout << "Marks Entered Successfully." << '\n';
}
};

class Result : public Student {


public:
float total, average;
char grade;

void calculate() override {


total = mark1 + mark2 + mark3;
average = total / 3;

if(average >= 90) {


grade = 'A';
}
else if(average >= 75) {
grade = 'B';
}
else if(average >= 50) {
grade = 'C';
}
else {
grade = 'F';
}
cout << "\nStudent Details\n";
cout << "Name: " << name << '\n';
cout << "Roll No: " << roll << '\n';
cout << "Total Marks: " << total << '\n';
cout << "Average: " << average << '\n';
cout << "Grade: " << grade << '\n';
}
};

int main() {

Result r;
Student *p = &r;

[Link]();
p->calculate();

return 0;
}

Output:

You might also like