-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet03.cpp
More file actions
93 lines (76 loc) · 2.21 KB
/
Copy pathSet03.cpp
File metadata and controls
93 lines (76 loc) · 2.21 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
/* [OOP Practical Eaxm 2025 - Set 03]
Write a C++ program using the following specifications:
a) Create a class Student with: Data members: roll, name
Default constructor, Parameterized constructor
Destructor that prints: "Destroying Student object: <roll>"
b) Create another class Marks with: Data members: ml, m2, m3 (three subject marks)
Parameterized constructor
Destructor that prints: "Destroying Marks object"
c) Create a friend function. This function should:
Access private members of both classes
Calculate total marks and percentage
Display all details in formatted output
d) In main():
Create one Student object using parameterized constructor
Create one Marks object using parameterized constructor
Call the friend function to display the total and percentage
*/
#include <iostream>
using namespace std;
class Marks; // Forward declaration
class Student {
private:
int roll;
string name;
public:
Student() {
roll = 0;
name = "NULL";
}
Student(int r, string n) {
roll = r;
name = n;
}
// Friend Function Declaration
friend void displayResult(Student, Marks);
~Student() {
cout << "Destroying Student object: " << roll << endl;
}
};
class Marks {
private:
int m1, m2, m3;
public:
Marks(int a, int b, int c) {
m1 = a;
m2 = b;
m3 = c;
}
// Friend Function Declaration
friend void displayResult(Student, Marks);
~Marks() {
cout << "Destroying Marks object" << endl;
}
};
// Friend Function Definition
void displayResult(Student s, Marks m) {
int total;
float percentage;
total = m.m1 + m.m2 + m.m3;
percentage = total / 3.0;
cout << "\n----- Student Result -----\n";
cout << "Roll No : " << s.roll << endl;
cout << "Name : " << s.name << endl;
cout << "Marks 1 : " << m.m1 << endl;
cout << "Marks 2 : " << m.m2 << endl;
cout << "Marks 3 : " << m.m3 << endl;
cout << "Total Marks : " << total << endl;
cout << "Percentage : " << percentage << " %" << endl;
cout << "***************************"<< endl;
}
int main() {
Student s1(101, "Devhuti");
Marks m1(100, 98, 97);
displayResult(s1, m1);
return 0;
}