0% found this document useful (0 votes)
16 views22 pages

Operator Overloading in C++ Classes

1. The document describes a lab report assignment to create classes for complex numbers, distances, and a calculator class. 2. For complex numbers, the student created a class to handle real and imaginary parts separately with constructors, get/set methods, and overloaded the + operator. 3. For distances, the class included feet and inches with constructors, get methods, and overloaded the % operator. 4. For the calculator class, the student overloaded all arithmetic and bitwise operators like ++, --, +, -, *, /, >>, << for a complex number class.

Uploaded by

Syed Shams
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

Operator Overloading in C++ Classes

1. The document describes a lab report assignment to create classes for complex numbers, distances, and a calculator class. 2. For complex numbers, the student created a class to handle real and imaginary parts separately with constructors, get/set methods, and overloaded the + operator. 3. For distances, the class included feet and inches with constructors, get methods, and overloaded the % operator. 4. For the calculator class, the student overloaded all arithmetic and bitwise operators like ++, --, +, -, *, /, >>, << for a complex number class.

Uploaded by

Syed Shams
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

COMSATS UNIVERSITY

ISLAMABAD

SYED SHAMS HAIDER

FA19-BEE-203\ISB

LAB REPORT 11

OBJECT ORIENTED
PROGRAMING

MAAM MEHWISH MEHMOOD

ELECTRICAL ENGINEERING
LAB TASK

5.1. Create a counter class, overload ++ operator for counter post and
pre increment, use
the object of counter class as a loop counter for printing a table in main
function.

CODE
#include<iostream>
using namespace std;
//syed shams haider reg 203
class Counter
{ private:
int number;
public:
Counter(int n = 0)
{
number = n;
}
Counter operator ++();
Counter operator ++(int);
Counter operator --();
Counter operator --(int);
void display();
};
Counter Counter::operator ++()
{ return Counter(++number);
}
Counter Counter::operator ++(int)
{ return Counter(number++);
}
Counter Counter::operator --()
{ return Counter(--number);
}Counter Counter::operator --(int)
{ return Counter(number--);
}void Counter::display()
{ cout<<"number:"<<number<<endl;
}int main()
{ Counter c1,c2(10),c3,c4;
cout<<"Before pre increment for c1:"<<endl;
[Link]();
++c1;
cout<<"After pre increment for c1:"<<endl;
[Link]();
cout<<"Before post increment for c1:"<<endl;
[Link]();
c3 = c1++;
cout<<"After post increment for c1:"<<endl;
[Link]();
cout<<"Value of c3:"<<endl;
[Link]();
cout<<"Before pre decrement for c2:"<<endl;
[Link]();
--c2;
cout<<"After pre decrement for c2:"<<endl;
[Link]();
cout<<"Before post decrement for c2:"<<endl;
[Link]();
c4 = c2--;
cout<<"After post decrement for c2:"<<endl;
[Link]();
cout<<"Vaue of c4:"<<endl;
[Link]();
return 0;}

DEV C++
5.2. A complex number is a number which can be put in the form a + bi.
Create a class
for complex numbers which handle real and imaginary part separately.
Class should
consist minimum required constructors, get and show methods also
Overload the +
operator for this class which work like this formula.

CODE
#include <iostream>
using namespace std;
//syed shams haider fa19-bee-203/isb
class Complex {
public:
Complex(float realpart=0, float imaginarypart=0);
Complex Add(const Complex &c) const;
float GetReal() const;
float GetImaginary() const;
void SetReal(float r);
void SetImaginary(float i);
private:
float real;
float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {


SetReal(realpart);
SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
real = r;
}

void Complex::SetImaginary(float i) {
imaginary = i;
}

float Complex::GetReal() const {


return real;
}

float Complex::GetImaginary() const {


return imaginary;
}

Complex Complex::Add(const Complex &c) const {


return Complex(GetReal() + [Link](), GetImaginary() +
[Link]());
}

Complex operator+(const Complex &c1, const Complex &c2) {


return [Link](c2);
}

int main(void) {
Complex x(-1,6);
Complex y(55,8);
Complex z = x + y;
cout << [Link]() << '+' << [Link]() << 'i' << endl;
return 0;
}

DEV C++

5.3. Create a class of Distance including feet and inches. Class should
consist minimum
required constructors, get and show methods also overload the %
operator for this
class.

CODE
#include <iostream>
using namespace std;
//syed shams haider reg 203
class Distance
{
private:
int feet;
int inches;

public:
Distance()//default constructor
{
this->feet = 0;
this->inches = 0;
}
Distance(int feet,int inches)//argument constructor
{
this->feet = feet;
this->inches = inches;
}
//get methods
int getFeet()
{
return feet;
}
double getInches()
{
return inches;
}

void operator%(int x)
{
this->inches = this->inches + this->feet%x;
this->feet = this->feet/x;

}
};
int main() {

Distance d(10,5);

cout<<[Link]()<<" feet "<<[Link]()<<" inches";

d%2;

cout<<endl;
cout<<[Link]()<<" feet "<<[Link]()<<" inches";

return 0;
}

DEV C++

HOME TASK
6.1. Create a calculator for the complex number by
creating a class of complex number
with overloading all operators in it.(Operators: ++,--,
+,-,/,*, >>, <<).

CODE
#include <iostream>
//syed shams haider reg 203
#include <math.h>
using namespace std;
class ComplexNumber {
private:
float real, imaginary;
public:
// Default constructor
ComplexNumber() {

real = imaginary = 0;
}
/* Parameterized constructor initializing

* real and imaginary values based on the parameters


*/
ComplexNumber(float real, float imag) {

this->real = real;

imaginary = imag;
}

/* Overloading prefix increment operator

* Increments both real and imaginary values


*/
void operator++() {
++real;

++imaginary;
}
/* Overloading postfix increment operator

* Increments both real and imaginary values


*/
void operator++(int) {
real++;
imaginary++;
}
/* Overloading prefix decrement operator
* Decrements both real and imaginary values
*/
void operator--() {
--real;
--imaginary;
}
/* Overloading postfix decrement operator
* Decrements both real and imaginary values
*/
void operator--(int) {
real--;
imaginary--;
}
/* Overloading addition operator
* (a+bi) + (c+di) = (a+c)+(b+d)i
*/
ComplexNumber operator+(ComplexNumber number) {
ComplexNumber addition;
[Link] = real + [Link];
[Link] = imaginary + [Link];
return addition;
}
/* Overloading subtraction operator
* (a+bi) - (c+di) = (a+c)-(b+d)i
*/
ComplexNumber operator-(ComplexNumber number) {
ComplexNumber subtraction;
[Link] = real - [Link];
[Link] = imaginary - [Link];
return subtraction;
}
/* Overloading multiplication operator
* (a+bi) * (c+di) = (ac-bd)+(bc+ad)i
*/
ComplexNumber operator*(ComplexNumber number) {
ComplexNumber multiplication;
[Link] = (real * [Link]) - (imaginary * [Link]);
[Link] = (imaginary * [Link]) + (real *
[Link]);
return multiplication;
}
/* Overloading division operator
* (a+bi) / (c+di) = [(ac+bd)/(c^2+d^2)]+[(bc-ad)/(c^2+d^2)]i
* because (a+bi) / (c+di) = [(a+bi) * (c-di)] / [(c+di) * (c-di)]

*/
ComplexNumber operator/(ComplexNumber number) {
ComplexNumber division;
float denominator = pow([Link], 2) + pow([Link], 2);
[Link] = ((real * [Link]) + (imaginary * [Link])) /
denominator;
[Link] = ((imaginary * [Link]) - (real *
[Link])) / denominator;
return division;
}
/* Overloading right shift operator
* Right shift both real and imaginary numbers
*/
void operator>>(int shift) {
real = ((int)real) >> shift;
imaginary = ((int)imaginary) >> shift;
}
/* Overloading left shift operator
* Left shift both real and imaginary numbers
*/
void operator<<(int shift) {
real = ((int)real) << shift;
imaginary = ((int)imaginary) << shift;
}
/* Print the complex number
*/
void print() {
cout << real << "+" << imaginary << "i";
}
};
int main() {
// Create a few complex numbers
ComplexNumber postfixInc(4, 5);
ComplexNumber prefixInc(4, 5);
ComplexNumber postfixDec(2, 3);
ComplexNumber prefixDec(2, 3);
ComplexNumber c1(3, 6);
ComplexNumber c2(4, 4);
ComplexNumber add = c1 + c2; // Add complex nos
ComplexNumber sub = c1 - c2; // Subtract complex nos
ComplexNumber mul = c1 * c2; // Multiply complex nos
ComplexNumber div = c1 / c2; // Divide complex nos
// Postfix increment the complex no and print
cout << "("; [Link](); cout << ")++"; cout << " = ";
postfixInc++;
[Link](); cout << endl;
// Prefix increment the complex number and print
cout << "++("; [Link](); cout << ")"; cout << " = ";
++prefixInc;
[Link](); cout << endl;
// Postfix decrement the complex no and print
cout << "("; [Link](); cout << ")--"; cout << " = ";
postfixDec--;
[Link](); cout << endl;
// Prefix decrement the complex number and print
cout << "--("; [Link](); cout << ")"; cout << " = ";
--prefixDec;
[Link](); cout << endl << endl;
// Print the complex numbers - sum, difference, mul & division
cout << "("; [Link](); cout << ") + ("; [Link](); cout << ") = "; [Link]();
cout << endl;
cout << "("; [Link](); cout << ") - ("; [Link](); cout << ") = "; [Link](); cout
<< endl;
cout << "("; [Link](); cout << ") * ("; [Link](); cout << ") = "; [Link]();
cout << endl;
cout << "("; [Link](); cout << ") / ("; [Link](); cout << ") = "; [Link](); cout
<< endl;
// Right shift the complex no and print
cout << "("; [Link](); cout << ") >> 1"; cout << " = ";
c1 >> 1; // Right shifting
[Link](); cout << endl;
// Left shift the complex no and print
cout << "("; [Link](); cout << ") << 1"; cout << " = ";
c2 << 1; // Left shifting
[Link](); cout << endl;
return 0;
}

DEV C++
CONCLUSION:
We came to know the use of unary, binary and stream
operators for user defined classes i.e. operator overloading in
this lab.

You might also like