Operator overloading in oop(object oriented programming)

 Operator overloading

Task 1:

Create a constructor that prevents a 0 denominator in a fraction, reduces or
simplifies
Create a class Rational Number (fractions) with the following capabilities: Overload the
relational and equality operators for this class.
 Overload the addition, subtraction, multiplication and division operators for this class.
 Fractions that are not in reduced form and avoids negative denominators.

#include<iostream>

using namespace std;

class Rational{

double p, q, temp1, temp2;

public:

Rational(int a, int b)

{

p = a;

if (b > 0)

{

q = b;

}

else

{

cout << "Please Enter the value of q greater than 0:" << endl;

cout << "q is set to 1" << endl;

q = 1;

}

negcheck();

show();

}

void reduce();

void negcheck();

void show();

void take();

Rational operator+(Rational &);

Rational operator-(Rational &);

Rational operator*(Rational &);

Rational operator/(Rational &);

bool operator==(Rational &);

bool operator!=(Rational &);

double getp()

{

return p;

}

double getq()

{

return q;

}

double gettemp1()

{

return temp1;

}

double gettemp2()

{

return temp2;

}

};

int main()

{

Rational r(-11, 12), r1(-12, 4);

int a; cout << endl << endl;

cout << "Please Enter 1 for addition" << endl;;

cout << "Enter 2 for subtract"<< endl;

cout << "Enter 3 for multiplication" << endl;

cout << "Enter 4 for division" << endl;

cin >> a;

switch (a)

{

case 1:

{Rational r3 = r + r1; r3.reduce(); r3.show(); break; }

case 2:

{ Rational r4 = r - r1; r4.reduce(); r4.show(); break; }

case 3:

{ Rational r5 = r*r1; r5.reduce(); r5.show(); break; }

case 4:

{ Rational r6 = r / r1; break; }

default:

{

cout << "Invalide Entery" << endl;

break;

}

}

getchar();

getchar();

return 0;

}

void Rational::reduce()

{

while (p != q)

{

if (p > q)

{

p = p - q;

}

else

{

q = q - p;

}

}

p = gettemp1() / getp();

q = gettemp2() / getq();

}

Rational Rational::operator+(Rational & r1)

{

if (*this == r1)

{

q = r1.q;

p = p + r1.p;

}

else if (*this != r1)

{

p = r1.q*p;

r1.p = q*r1.p;

p = p + r1.p;

q =q * r1.q;

}

temp1 = p;

temp2 = q;

return *this;

}

Rational Rational::operator-(Rational & r1)

{

if (*this == r1)

{

q = r1.q;

if (p > r1.p)

p = p - r1.p;

else

p = r1.p - p;

}

else if (*this!= r1)

{

p = r1.q*p;

r1.p = q*r1.p;

if (r1.p>p)

p = r1.p - p;

else p = p - r1.p;

q = q * r1.q;

}

temp1 = p;

temp2 = q;

return *this;

}

Rational Rational::operator*(Rational &r1)

{

q = q*r1.q;

p = p* r1.p;

temp1 =p;

temp2 =q;

return *this;

}

void Rational::negcheck()

{

if (p < 0)

{

p = -p;

}

if (q < 0)

{

q = -q;

}

}

Rational Rational::operator/(Rational &r1)

{

if (p>q)

{

p = p / q;

}

else

p = q / p;

if (r1.p > r1.q)

q = r1.p / r1.q;

else

q = r1.q / r1.p;

p = p / q;

cout <<"Value after Division is:"<< p << endl;

return *this;

}

void Rational::show()

{

cout << "Values are:" << getp() << "/" << getq() << endl;

}

bool Rational::operator==(Rational &r1)

{

return (q == r1.q);

}

bool Rational::operator!=(Rational & r1)

{

return (q != r1.q);

}       

          

       
 


Task 2:

Write a C++ program to overload binary operators '+' to add and '-' to subtract two complex numbers.

#include<iostream>

using namespace std;

class Imaginary{

int r,i;

public:

Imaginary()

{

r = 0; i = 0;

}

Imaginary(int a, int b)

{

r = a;

i = b;

}

Imaginary operator+(Imaginary &);

Imaginary operator-(Imaginary &);

void show()const;

};

int main()

{

Imaginary i1(22, 8), i2(3, 44); i1.show(); i2.show();

Imaginary i3 = i1 + i2; i3.show();

Imaginary i4 = i1 - i2; i4.show();

getchar();

getchar();

return 0;

}

Imaginary Imaginary::operator+(Imaginary &i2)

{

Imaginary t;

t.r = r + i2.r;

t.i = i + i2.i;

return t;

}

Imaginary Imaginary::operator-(Imaginary &i2)

{

Imaginary t;

t.r = r - i2.r;

t.i = i - i2.i;

return t;

}

void Imaginary::show()const

{

cout << "Number is:" << r << "+" << i << "i" << endl;

}
          

       
 


Task 3:

Create a distance class with data members feet and inches, add an overloaded –
operator that subtracts two distances. It should allow statements like dist3=dist1-
dist2;. Assume that the operator will never be used to subtract a larger number
from a smaller one (that is, negative distances are not allowed).

 #include<iostream>

#include<cmath>

using namespace std;

class Distance{

int x1,y1,d;

public:

Distance()

{

x1 = y1= 0;

}

void c1(int a, int b);

void c2(int a,int b);

void set();

Distance operator+(Distance &);

Distance operator-(Distance &);

void show();

};

int main()

{

int x1, x2, y1, y2,d2x1,d2y1,d2x2,d2y2;

cout << "Enter the x1 coordinate:";

cin >> x1;

cout << "Enter the x2 coordinate:";

cin >> x2;

cout << "Enter the y1 coordinate:";

cin >> y1;

cout << "Enter the y2 coordinate:";

cin >> y2;

cout << "Enter the x1 coordinate:";

cin >> d2x1;

cout << "Enter the x2 coordinate:";

cin >> d2x2;

cout << "Enter the y1 coordinate:";

cin >> d2y1;

cout << "Enter the y2 coordinate:";

cin >> d2y2;

Distance d1;

d1.c1(x1, y1); d1.c2(x2, y2); d1.set(); d1.show();

Distance d2;

d2.c1(d2x1, d2y1); d2.c2(d2x2, d2y2); d2.set(); d2.show();

Distance d3 = d1 - d2; d3.show();

getchar();

getchar();

return 0;

}

void Distance::c1(int a, int b)

{

x1 = (b - a)*(b - a);

}

void Distance::c2(int a, int b)

{

y1 = (b - a)*(b - a);

}

void Distance::set()

{

d = sqrt(x1 + y1);

}

Distance Distance::operator-(Distance &r1)

{

Distance d3;

if (d > r1.d)

d3.d = d - r1.d;

else

d3.d = r1.d - d;

return d3;

}

int i = 1;

void Distance::show()

{

cout << endl;

cout << "Distance " << i << " is:" << d << endl; i++;

}      

          

       
 

Comments

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........