-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass15.cpp
More file actions
43 lines (40 loc) · 838 Bytes
/
Copy pathclass15.cpp
File metadata and controls
43 lines (40 loc) · 838 Bytes
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
// overloading of unary operator as a friend function
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class Complex
{
private:
int a,b;
public:
void setData(int x,int y)
{
a = x;
b = y;
}
void showData()
{
cout<<"a = "<<a<<" b = "<<b<<endl;
}
friend Complex operator-(Complex); //overloading of unary - operator
};
//defining friend function outside of the class
Complex operator-(Complex c)
{
Complex t;
t.a = -c.a;
t.b = -c.b;
return t;
}
int main(int argc, char const *argv[])
{
system("cls");
Complex c1,c2;
c1.setData(8,2);
// c2 = operator-(c1); //we can use like this
c2 = -c1; //this is after overlaoding
c2.showData();
system("pause");
return 0;
}