Skip to content

Commit 817cb81

Browse files
committed
add: cpp-programming-ques 06_operator friend 15
1 parent e836dc3 commit 817cb81

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// // Create a complex class and overload assignment operator for that class.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <conio.h>
6+
7+
// // use namespace
8+
using namespace std;
9+
10+
// // define class Complex
11+
class Complex
12+
{
13+
14+
private:
15+
// // instance member variables
16+
double real;
17+
double imag;
18+
19+
public:
20+
// // constructors
21+
Complex()
22+
{
23+
real = imag = 0;
24+
}
25+
26+
Complex(double r)
27+
{
28+
real = imag = r;
29+
}
30+
31+
Complex(double r, double i)
32+
{
33+
real = r;
34+
imag = i;
35+
}
36+
37+
// // instance member function to set compelx number
38+
void setData(double r, double i)
39+
{
40+
real = r;
41+
imag = i;
42+
}
43+
44+
// // instance member function to display compelx number
45+
void showData()
46+
{
47+
cout << "\n"
48+
<< real << " + " << imag << "i" << endl;
49+
}
50+
51+
// // overload assignment (=) operator
52+
Complex operator=(Complex c)
53+
{
54+
real = c.real;
55+
imag = c.imag;
56+
return c;
57+
}
58+
};
59+
60+
// // Main Function Start
61+
int main()
62+
{
63+
double real, imag;
64+
65+
// // Get complex
66+
cout << "\n>>>>>>>> Enter A Complex Number <<<<<<<<<\n";
67+
cout << "\nEnter Real Part => ";
68+
cin >> real;
69+
cout << "\nEnter Imaginary Part => ";
70+
cin >> imag;
71+
72+
Complex c1(real, imag), c2; // create objects of Complex
73+
c2 = c1; // assign c1 to c2
74+
75+
// // display first complex number
76+
cout << "\n>>>>>>>> First Complex Number <<<<<<<<<\n";
77+
c1.showData();
78+
79+
cout << "\n>>>>>>>> Copy of Complex Number <<<<<<<<<\n";
80+
c2.showData();
81+
82+
cout << endl; // Add new line
83+
getch();
84+
return 0;
85+
}
86+
// // Main Function End
Binary file not shown.

0 commit comments

Comments
 (0)