-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog15.cpp
More file actions
105 lines (81 loc) · 2.27 KB
/
prog15.cpp
File metadata and controls
105 lines (81 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Write a C++ Program to demostrate the virtual base class concept. Create a base
class student with protected members name, usn and member functions (read and
print).
Derive a class called test from the student class with sub1, sub2 (protected
members), getMarks() and putMarks() (two member functions of the class).
Derive one more class from the student class (Sports) with members: score,
member functions: getScore(), putScore().
Finally, the class result inherits from both test and sports classes with a data
member total and uses a display function to print all the details of the student
class. Create an aray of n objects of result class and demostrate.
Use virtual keyword to avoid ambiguity.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
protected:
string name, usn;
public:
void read() {
cout << "Enter name and usn" << endl;
cin >> name >> usn;
}
void print() { cout << "Name: " << name << endl << "USN: " << usn << endl; }
};
class Test : virtual public Student {
protected:
int sub1, sub2;
public:
void putMarks() {
cout << "Enter marks for Sub1 and Sub2" << endl;
cin >> sub1 >> sub2;
}
void getMarks() {
cout << "Subject 1 Marks: " << sub1 << endl
<< "Subject 2 Marks: " << sub2 << endl;
}
};
class Sports : virtual public Student {
protected:
int score;
public:
void putScore() {
cout << "Enter Sports Score: ";
cin >> score;
}
void getScore() { cout << "Sports Score: " << score << endl; }
};
class Result : public Test, public Sports {
private:
int total;
public:
void calculateTotal() { total = sub1 + sub2 + score; }
void display() {
Student::print();
Test::getMarks();
Sports::getScore();
cout << "Total Marks: " << total << endl;
}
};
int main() {
int n;
cout << "Enter the number of students: ";
cin >> n;
vector<Result> students(n);
for (int i = 0; i < n; i++) {
cout << "\nEnter details for Student " << i + 1 << ":" << endl;
students[i].read();
students[i].putMarks();
students[i].putScore();
students[i].calculateTotal();
}
cout << "\nStudent Results" << endl;
for (int i = 0; i < n; i++) {
cout << "\nDetails for Student " << i + 1 << ":" << endl;
students[i].display();
}
return 0;
}