-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_Operator_overloading_with_friend_function.cpp
More file actions
85 lines (65 loc) · 1.99 KB
/
binary_Operator_overloading_with_friend_function.cpp
File metadata and controls
85 lines (65 loc) · 1.99 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
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
Student() {
age = 18;
}
Student(string name, int age) {
this->name = name;
this->age = age;
}
string getName() {
return this->name;
}
void setName(string name) {
this->name = name;
}
int getAge() {
return this->age;
}
void setAge(int age) {
this->age = age;
}
void display() {
cout << "Student Information: " << endl;
cout << "------------------------------------------" << endl;
cout << "Name: " << this->name << endl;
cout << "Age: " << this->age << endl << endl;
}
friend int operator+(Student s1, int x) {
return s1.age + x;
}
friend Student operator+(Student s1, Student s2) {
//Student newStudent(this->name + " " +s2.getName(), this->age + s2.getAge());
string newName = s1.name + " " + s2.getName();
int newAge = s1.age + s2.getAge();
Student newStudent(newName, newAge);
return newStudent;
}
friend int operator+(int x, Student s1) {
return x + s1.age;
}
};
int main() {
Student s1("Adnan", 20);
s1.display();
Student s2("Sami", 30);
s2.display();
cout << "Case #4: s1 + (int)X ---> (int)Y" << endl;
cout << "------------------------------------------" << endl;
int result = s1 + 7;
cout << result << endl;
cout << "Case #5: s1 + s2 ---> (Student)Y" << endl;
cout << "------------------------------------------" << endl;
Student superStudent = s1 + s2;
superStudent.display();
cout << "Case #6: (int)7 + s1 ---> (int)Y" << endl;
cout << "------------------------------------------" << endl;
result = 7 + s1;
cout << result << endl;
return 0;
}