-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40d05ee
commit 17e7401
Showing
7 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Challenge Yourself Here/Labs/Lab-section-A/Lab_8_OperatorOverloading/task1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Complex | ||
{ | ||
private: | ||
int real; | ||
int imag; | ||
|
||
public: | ||
Complex(int real = 0, int imag = 0) : real(real), imag(imag) {} | ||
friend istream &operator>>(istream &in, Complex &obj) | ||
{ | ||
cout << "Enter the real part of the complex number :"; | ||
in >> obj.real; | ||
cout << "Enter the imaginary part of the complex number :"; | ||
in >> obj.imag; | ||
return in; | ||
} | ||
friend ostream &operator<<(ostream &out, Complex &obj) | ||
{ | ||
if (obj.real != 0 && obj.imag != 0) | ||
{ | ||
out << obj.real; | ||
obj.imag >= 0 ? cout << "+" : cout << "-"; | ||
cout << obj.imag<< "i" | ||
<< "\n"; | ||
} | ||
else | ||
{ | ||
cout << 0 << "\n"; | ||
} | ||
|
||
return out; | ||
} | ||
Complex operator-(const Complex &obj) const | ||
{ | ||
return Complex(real - obj.real, imag - obj.imag); | ||
} | ||
bool operator==(const Complex &obj) const | ||
{ | ||
return (real == obj.real && imag == obj.imag); | ||
} | ||
bool operator!=(const Complex &obj) const | ||
{ | ||
return !(*this == obj); | ||
} | ||
}; | ||
int main() | ||
{ | ||
Complex C1; | ||
Complex C2(5, 6); | ||
Complex C3; | ||
|
||
cout << "Input a complex number" << endl; | ||
cin >> C3; | ||
|
||
cout << C1 << C2 << C3; | ||
|
||
if (C1 == C2) | ||
cout << "C1 == C2" << endl; | ||
else | ||
cout << "C1 != C2" << endl; | ||
|
||
if (C1 != C3) | ||
cout << "C1 != C3" << endl; | ||
else | ||
cout << "C1==C3" << endl; | ||
|
||
Complex C4 = C2 - C3; | ||
cout << C4; | ||
|
||
system("pause"); | ||
return 0; | ||
} |
Binary file added
BIN
+46.8 KB
Challenge Yourself Here/Labs/Lab-section-A/Lab_8_OperatorOverloading/task1.exe
Binary file not shown.
56 changes: 56 additions & 0 deletions
56
Challenge Yourself Here/Labs/Lab-section-A/Lab_8_OperatorOverloading/task2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Box | ||
{ | ||
private: | ||
double length, breadth, height; | ||
static int objectCount; | ||
|
||
public: | ||
Box() : length(0), breadth(0), height(0) {} | ||
Box(double length, double breadth, double height) : length(length), breadth(breadth), height(height) {} | ||
void setLength(double length) { this->length = length; } | ||
void setbreadth(double breadth) { this->breadth = breadth; } | ||
void setheigth(double heigth) { this->height = heigth; } | ||
double getLength() { return length; } | ||
double getbreadth() { return breadth; } | ||
double getheigth() { return height; } | ||
void print() | ||
{ | ||
cout << "The length of the object = " << length << "\n"; | ||
cout << "The breadth of the object = " << breadth << "\n"; | ||
cout << "The height of the object = " << height << "\n"; | ||
} | ||
static int getCount() | ||
{ | ||
return objectCount; | ||
} | ||
double getVolume() | ||
{ | ||
return (length * breadth) * height; | ||
} | ||
double getArea() | ||
{ | ||
return length * breadth; | ||
} | ||
friend void print_surfaceArea(const Box &b1) | ||
{ | ||
cout << "The surface area of the box = " << (2 * (b1.length * b1.breadth) + 2 * (b1.length * b1.height) + 2 * (b1.breadth * b1.height)); | ||
} | ||
friend void double_data_members(double &length, double &breadth, double &height) | ||
{ | ||
length += length; | ||
breadth += breadth; | ||
height += height; | ||
} | ||
}; | ||
int Box::objectCount=1; | ||
int main() | ||
{ | ||
Box b(2, 2, 2); | ||
b.print(); | ||
cout << Box::getCount(); | ||
return 0; | ||
} |
Binary file added
BIN
+44.1 KB
Challenge Yourself Here/Labs/Lab-section-A/Lab_8_OperatorOverloading/task2.exe
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters