C++ Program to Calculate Result of Student

C++ Program:


/* Write a program to display result of students using inheritance.
Name : Abhijit Somnath Kokane */

#include<iostream>
using namespace std;

class student 
{
 string name;
 int id;

 public:
 
 static int tmp;

 student(string n)
 {
  name=n;
  id=tmp++;
 }

 student()
 {
  id=tmp++;
 }

 void set()
 {
  cout<<"Enter student's name:- ";
  cin>>name;
 }

 void display()
 {
  cout<<endl<<"Id :"<<id;
  cout<<endl<<"Student Name : "<<name;  
 }
};

int student :: tmp=101;

class studentExam : public student 
{
 protected:
 const int n=3;
 float maths,comp,ele;

 public:

 studentExam()
 {
 }

 studentExam(string n, float m=0, float c=0, float e=0) : student(n)
 {
 maths=m;comp=c;ele=e;
 } 

 void set()
 {
  student :: set();
  cout<<"Enter marks of maths, computer, electronics :- ";
  cin>>maths>>comp>>ele;
 }

 void display()
 {
  student :: display();
  cout<<endl<<"Marks are as follows: "<<endl;
  cout<<"Maths \t Computer \t Electronics"<<endl;
  cout<<maths<<"\t \t"<<comp<<"\t \t"<<ele<<endl;
 } 
};

class studentResult : public studentExam
{
 float percentage;
 string grade;

 public:

 studentResult()
 {
  percentage=0;grade="Fail";
 }

 studentResult(string n, float m=0, float c=0, float e=0) : studentExam(n,m,c,e) 
 {
  percentage=0;grade="Fail";
 }


 void set()
 {
  studentExam :: set();
 }

 void display()
 {
  studentExam :: display();
  cout<<"Percentage : "<<percentage;
  cout<<endl<<"Grade : "<grade<<endl;
 } 

 char * findGrade(float per)
 {
  static char D[15]="Distinction";
  static char A[10]="A Class";
  static char B[10]="B Class";
  static char P[10]="Pass";
  static char F[10]="Fail";

  if(per>=75)
   return D; 
  else if(per>=60)
   return A;
  else if(per>=50)
   return B;
  else if(per>=35)
   return P;
  else
   return F;  
 }
 
 void computePercentage()
 {
  float tmp;
  tmp=(maths+comp+ele)/n;
  percentage=tmp;
  grade=findGrade(percentage);  
 }

};

int main()
{
 int i;

 studentResult S[5];

 for(i=0;i<3;i++)
 {
  S[i].set();
  S[i].computePercentage();
 }
 
 for(i=0;i<3;i++)
  S[i].display();

 return 0; 
}

/* Output of above code:- 

Enter student's name:- Shubham
Enter marks of maths, computer, electronics :- 75 78 78
Enter student's name:- Chinmay
Enter marks of maths, computer, electronics :- 80 81 72
Enter student's name:- Abhijit
Enter marks of maths, computer, electronics :- 300 300 300

Id :101
Student Name : Shubham
Marks are as follows: 
Maths   Computer   Electronics
75   78   78
Percentage : 77
Grade : Distinction

Id :102
Student Name : Chinmay
Marks are as follows: 
Maths   Computer   Electronics
80   81   72
Percentage : 77.6667
Grade : Distinction

Id :103
Student Name : Abhijit
Marks are as follows: 
Maths   Computer   Electronics
300   300   300
Percentage : 300
Grade : Distinction

*/