Skip to content

Commit e5e866e

Browse files
committed
add: cpp-programming-ques 06_operator friend 11
1 parent f1be117 commit e5e866e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// // Write a C++ program to overload unary operators that is increment and decrement.
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 insertion (<<) operator
52+
friend Complex operator<<(ostream &, Complex &);
53+
54+
// // overload extraction (>>) operator
55+
friend Complex operator>>(istream &, Complex &);
56+
};
57+
58+
// // overload insertion (<<) operator
59+
Complex operator<<(ostream &temp, Complex &c)
60+
{
61+
cout << "\n"
62+
<< c.real << " + " << c.imag << "i" << endl;
63+
return c;
64+
}
65+
66+
// // overload extraction (>>) operator
67+
Complex operator>>(istream &temp, Complex &c)
68+
{
69+
cout << "\nEnter Real => ";
70+
cin >> c.real;
71+
cout << "\nEnter Imaginary => ";
72+
cin >> c.imag;
73+
return c;
74+
}
75+
76+
// // Main Function Start
77+
int main()
78+
{
79+
Complex c1; // create object of Complex
80+
81+
// // Get complex number
82+
cout << "\n>>>>>>>> Enter Complex Number <<<<<<<<<\n";
83+
cin >> c1;
84+
85+
// // display complex number
86+
cout << "\n>>>>>>>> Complex Number <<<<<<<<<";
87+
cout << c1;
88+
89+
cout << endl; // Add new line
90+
getch();
91+
return 0;
92+
}
93+
// // Main Function End
Binary file not shown.

0 commit comments

Comments
 (0)