You are on page 1of 22

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;
c1.display();
++c1;
cout<<"After pre increment for c1:"<<endl;
c1.display();
cout<<"Before post increment for c1:"<<endl;
c1.display();
c3 = c1++;
cout<<"After post increment for c1:"<<endl;
c1.display();
cout<<"Value of c3:"<<endl;
c3.display();
cout<<"Before pre decrement for c2:"<<endl;
c2.display();
--c2;
cout<<"After pre decrement for c2:"<<endl;
c2.display();
cout<<"Before post decrement for c2:"<<endl;
c2.display();
c4 = c2--;
cout<<"After post decrement for c2:"<<endl;
c2.display();
cout<<"Vaue of c4:"<<endl;
c4.display();
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() + c.GetReal(), GetImaginary() +
c.GetImaginary());
}

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


return c1.Add(c2);
}

int main(void) {
Complex x(-1,6);
Complex y(55,8);
Complex z = x + y;
cout << z.GetReal() << '+' << z.GetImaginary() << '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<<d.getFeet()<<" feet "<<d.getInches()<<" inches";

d%2;

cout<<endl;
cout<<d.getFeet()<<" feet "<<d.getInches()<<" 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;
addition.real = real + number.real;
addition.imaginary = imaginary + number.imaginary;
return addition;
}
/* Overloading subtraction operator
* (a+bi) - (c+di) = (a+c)-(b+d)i
*/
ComplexNumber operator-(ComplexNumber number) {
ComplexNumber subtraction;
subtraction.real = real - number.real;
subtraction.imaginary = imaginary - number.imaginary;
return subtraction;
}
/* Overloading multiplication operator
* (a+bi) * (c+di) = (ac-bd)+(bc+ad)i
*/
ComplexNumber operator*(ComplexNumber number) {
ComplexNumber multiplication;
multiplication.real = (real * number.real) - (imaginary * number.imaginary);
multiplication.imaginary = (imaginary * number.real) + (real *
number.imaginary);
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(number.real, 2) + pow(number.imaginary, 2);
division.real = ((real * number.real) + (imaginary * number.imaginary)) /
denominator;
division.imaginary = ((imaginary * number.real) - (real *
number.imaginary)) / 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 << "("; postfixInc.print(); cout << ")++"; cout << " = ";
postfixInc++;
postfixInc.print(); cout << endl;
// Prefix increment the complex number and print
cout << "++("; prefixInc.print(); cout << ")"; cout << " = ";
++prefixInc;
prefixInc.print(); cout << endl;
// Postfix decrement the complex no and print
cout << "("; postfixDec.print(); cout << ")--"; cout << " = ";
postfixDec--;
postfixDec.print(); cout << endl;
// Prefix decrement the complex number and print
cout << "--("; prefixDec.print(); cout << ")"; cout << " = ";
--prefixDec;
prefixDec.print(); cout << endl << endl;
// Print the complex numbers - sum, difference, mul & division
cout << "("; c1.print(); cout << ") + ("; c2.print(); cout << ") = "; add.print();
cout << endl;
cout << "("; c1.print(); cout << ") - ("; c2.print(); cout << ") = "; sub.print(); cout
<< endl;
cout << "("; c1.print(); cout << ") * ("; c2.print(); cout << ") = "; mul.print();
cout << endl;
cout << "("; c1.print(); cout << ") / ("; c2.print(); cout << ") = "; div.print(); cout
<< endl;
// Right shift the complex no and print
cout << "("; c1.print(); cout << ") >> 1"; cout << " = ";
c1 >> 1; // Right shifting
c1.print(); cout << endl;
// Left shift the complex no and print
cout << "("; c2.print(); cout << ") << 1"; cout << " = ";
c2 << 1; // Left shifting
c2.print(); 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