-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointertoobject.cpp
62 lines (55 loc) · 1.08 KB
/
pointertoobject.cpp
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
#include <iostream>
using namespace std;
class ComplexNumber
{
private:
int real;
float imaginary;
public:
ComplexNumber()
{
}
ComplexNumber(int r, float i)
{
real = r;
imaginary = i;
}
void displayData()
{
cout << "Complex number is: " << real << " + " << imaginary << "i" << endl;
}
int getReal()
{
return real;
}
int getImag()
{
return imaginary;
}
};
ComplexNumber addnum(ComplexNumber n1, ComplexNumber n2)
{
int r;
float i;
r = n1.getReal() + n2.getReal();
i = n1.getImag() + n2.getImag();
ComplexNumber temp(r, i);
return temp;
}
int main()
{
ComplexNumber c1(2, 3.5), c2(4, 5.5), c3;
c1.displayData();
c2.displayData();
cout << "Addition result: ";
c3 = addnum(c1, c2);
c3.displayData();
cout << "Pointer to object" << endl;
ComplexNumber *ptr1;
ptr1 = &c3;
ptr1->displayData();
ptr1 = &c1;
ptr1->displayData();
cout << ptr1;
return 0;
}