Write a C++ program, consider that there are two base classes namely class StudentsDetails and class Marks from which class C is inherited. The class StudentDetails contains member function getDetails() that reads “students name”, “Enrollment number” value as user input from keyboard. Class Marks contains member function getMarks() and reads “5 subject marks” value as user input from keyboard. The derived class C inherits all the public members of StudentDetails and Marks and computes the total marks of the studnets.
SAMPLE OUTPUT:
enter value of name: JOHN
enter value of eno.: JOHN123
enter value of marks [0] 89
enter value of marks [1] 78
enter value of marks [2] 67
enter value of marks [3] 86
enter value of marks [4] 57
Total = 377
I implemented the problem as follows:
#include<iostream>
using namespace std;
class StudentDetails
{
char Student_name[20];
char Enrollment_no[20];
public:
void getDetails()
{
cout<<"enter value of name: ";
cin>>Student_name;
cout<<"enter value of eno.: ";
cin>>Enrollment_no;
}
};
class Marks
{
float Marks[5];
public:
float* getMarks()
{
for(int i=0;i<5;i++)
{
cout<<"enter value of marks ["<<i<<"] ";
cin>>Marks[i];
}
return Marks;
}
};
class C: public StudentDetails,public Marks
{
float total;
public:
float gettotal()
{
getDetails();
float *ptr=getMarks();
for(int i=0;i<5;i++)
{
total=total+(*(ptr+i));
}
return total;
}
};
int main()
{
C c;
int t;
t=c.gettotal();
cout<<"Total = "<<t;
}
Is there any better approach to solve this question?
Aucun commentaire:
Enregistrer un commentaire