-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ1.cpp
85 lines (63 loc) · 1.78 KB
/
Q1.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Aledutron
SPPU 2019 SE OOP Lab
SPPU Computer Engineering Second Year (SE) Object Oriented Programming (OOP) Lab Assignments (2019 Pattern)
Youtube OOP Lab Playlist Link: https://youtube.com/playlist?list=PLlShVH4JA0otmMld-dvDBGxqqSNy1zlf3
Problem Statement:
Group-A/Q1.cpp
Implement a class Complex which represents the Complex Number data type. Implement
the following
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.4. Overloaded << and >> to
print and read Complex Numbers.
Explaination Video Link: https://www.youtube.com/watch?v=q-IunMimiKg&list=PLlShVH4JA0otmMld-dvDBGxqqSNy1zlf3&index=2&pp=iAQB
*/
#include<iostream>
using namespace std;
class Complex{
public:
float real;
float img;
Complex(){
real = 0;
img = 0;
}
Complex(float a, float b){
real = a;
img = b;
}
void print(){
cout << real << " + " << img << "i" << endl;
}
Complex operator+(Complex c2){
Complex temp;
temp.real = real + c2.real;
temp.img = img + c2.img;
return temp;
}
};
Complex operator*(Complex c1, Complex c2){
Complex temp;
temp.real = (c1.real * c2.real) - (c1.img * c2.img);
temp.img = (c1.real * c2.img) + (c1.img * c2.real);
return temp;
}
ostream& operator<<(ostream& COUT, Complex c1){
COUT << c1.real << " + " << c1.img << "i" << endl;
}
istream& operator>>(istream& CIN, Complex& c){
CIN >> c.real >> c.img;
}
int main()
{
Complex z1; // default constructor
Complex z2(2,6); // parameterised constructor
Complex z3 = z1 + z2; // operator+
cout << z3 << z2; // operator<<
Complex z4;
cout << "Enter real and img part for a complex number: ";
cin >> z4; // operator>>
cout << z4; // operator<<
cout << z3*z4; // operator*
}