-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop revised.cpp
More file actions
104 lines (91 loc) · 2.42 KB
/
oop revised.cpp
File metadata and controls
104 lines (91 loc) · 2.42 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
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string name;
int rollNo;
float marks;
public:
// Default constructor
Student()
{
name = "Unknown";
rollNo = 0;
marks = 0.0;
}
// Parameterized constructor
Student(string n, int r, float m) {
name = n;
rollNo = r;
marks = m;
}
// Copy constructor
Student(const Student &s)
{
name = s.name;
rollNo = s.rollNo;
marks = s.marks;
}
// Destructor
~Student() {
cout << "Student " << name << " record deleted!" << endl;
}
// Display method
void display() const {
cout << "Name: " << name << ", Roll: " << rollNo << ", Marks: " << marks << endl; }
// Getter methods
string getName() const { return name; }
int getRollNo() const { return rollNo; }
float getMarks() const { return marks; }
// Setter methods using the this pointer
void setName(string n) { this->name = n; }
void setRollNo(int r) { this->rollNo = r; }
void setMarks(float m) { this->marks = m; }
// Operator overloading (+)
Student operator+(const Student &s2) const {
Student s3;
s3.name = "Combined";
s3.rollNo = 0;
s3.marks = marks + s2.marks;
return s3;
}
// Operator overloading (==)
bool operator==(const Student &s2) const {
return (rollNo == s2.rollNo);
}
};
// Friend function to compare marks
void compareMarks(const Student &s1, const Student &s2)
{
if (s1.getMarks() > s2.getMarks())
{
cout << "Higher Marks: " << s1.getName() << endl;
} else {
cout << "Higher Marks: " << s2.getName() << endl;
}
}
int main() {
// s1: Default constructor
Student s1;
cout << "Student Records:" << endl;
s1.display();
// s2: Parameterized constructor
Student s2("Alice", 101, 95.5);
s2.display();
// s3: Copy constructor (copy s2)
Student s3(s2);
s3.display();
compareMarks(s1, s2);
Student s4 = s1 + s2;
cout << "Combined Marks: ";
s4.display();
// Check if same rollNo
if (s2 == s3) {
cout << "s2 and s3 have the same roll number." << endl;
} else {
cout << "s2 and s3 have different roll numbers." << endl;
}
return 0;
}