| 1. |
a) Consider an example of declaring the examination result. Design three classes: Student, Exam and Result. The Student class has data members such as those representing registration number, name etc. Create a class Exam by inheriting the student class. The exam class adds data members representing the marks scored in six subjects. Derive class Result from the exam class and it has own data members such as total_marks. Write an interactive program in C++ to model this relationship |
|
Answer» Answer: #include using namespace std;
#define MAX 10
class student { private: char name[30]; int ROLLNO; int total; float perc; public: //MEMBER function to get student's details void getDetails(void); //member function to print student's details void putDetails(void); }; void student::getDetails(void){ cout << "Enter name: " ; cin >> name; cout << "Enter ROLL number: "; cin >> rollNo; cout << "Enter total marks outof 500: "; cin >> total;
perc=(float)total/500*100; } void student::putDetails(void){ cout << "Student details:\n"; cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc; }
int main() { student std[MAX]; int n,loop;
cout << "Enter total number of students: "; cin >> n;
for(loop=0;loop< n; loop++){ cout << "Enter details of student " << loop+1 << ":\n"; std[loop].getDetails(); }
cout << endl;
for(loop=0;loop< n; loop++){ cout << "Details of student " << (loop+1) << ":\n"; std[loop].putDetails(); }
return 0; } Explanation: |
|