Search your knowledge

Loading...

Monday, June 18, 2012

Create a base class Student(Roll No, Name) which derives two classes Test (Mark1, Mark2) and Sport(Score). Result(total marks, grade) class inherits both Test and Sport classes. Write a C++ menu driven program to perform the following functions: - Build a master table - Calculate total of marks and grade - Display the details of all students in ascending order of marks.


#include<iostream.h>
#include<conio.h>
class stud
{
protected:
int rno;
char name[10];
public:
void accept(int n)
{
rno=n;
cout<<"\n\nEnter the name of student having roll no : "<<rno<<" ";
cin>>name;
}
void display()
{
cout<<"\n\nRoll no : "<<rno
<<"\n\nName    : "<<name;
}

};
class test:public virtual stud
{
protected:
int mk1,mk2;
public:
void get_mks()
{
cout<<"\n\nEnter the marks for 2 subjects : ";
cin>>mk1>>mk2;
}
void put_mks()
{
cout<<"\n\nMarks of Sub 1 : "<<mk1
<<"\n\nMarks of Sub 2 : "<<mk2;
}
};
class sports:public virtual stud
{
protected:
float scr;
public:
void get_scr()
{
cout<<"\n\nEnter score : ";
cin>>scr;
}
void put_scr()
{
cout<<"\n\nScore : "<<scr;
}
};
class result:public test,public sports
{
int tot;
char *grade[15];
float per;
public:
void cal_res();
void friend sort(result*,int);
void disp_res()
{
display();
put_mks();
put_scr();
cout<<"\n\nTotal Marks : "<<tot
<<"\n\nGrade : "<<*grade;
}
};
void result::cal_res()
{
tot=mk1+mk2+scr;
per=tot/3;
if(per>=70)
*grade="Distinction";
else if(per<70 &&per>=60)
*grade="First Class";
else if(per<60 && per>=50)
*grade="Second Class";
else if(per<50 && per>=40)
*grade="Pass";
else
*grade="Fail";
}
void sort(result *a,int n)
{
int srt[15],tmp;
for(int i=0;i<n;i++)
srt[i]=a[i].tot;
for(i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(srt[i]>srt[j])
{
tmp=srt[i];
srt[i]=srt[j];
srt[j]=tmp;
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(srt[i]==a[j].tot)
{
a[j].disp_res();
break;
}
}
}
int main()
{
clrscr();
result obj[15];
cout<<"\n\nEnter how many records you want to enter : ";
int n,srt[15];
cin>>n;
for(int i=0;i<n;i++)
{
obj[i].accept(i+1);
obj[i].get_mks();
obj[i].get_scr();
obj[i].cal_res();
}
// for(i=0;i<n;i++)

// obj[i].disp_res();
cout<<"\n\nDetails of all students in ascending order of marks ";
cout<<"\n\n------------------------------------------------\n\n";
sort(obj,n);
cout<<"\n\n------------------------------------------------\n\n";
getch();
return 0;
}