Professional Documents
Culture Documents
ISLAMABAD
FA19-BEE-203\ISB
LAB REPORT 11
OBJECT ORIENTED
PROGRAMING
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;
};
void Complex::SetReal(float r) {
real = r;
}
void Complex::SetImaginary(float i) {
imaginary = i;
}
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);
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
this->real = real;
imaginary = imag;
}
++imaginary;
}
/* Overloading postfix increment operator
*/
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.