0% found this document useful (0 votes)
15 views

Operartor Overloading

The document defines two C++ structs - Distance and Complex - to represent distance and complex number objects. Both structs define default and parameterized constructors, setter and getter methods, and operator overloading for arithmetic operations like addition, subtraction, multiplication and division. The Distance struct stores feet and inches attributes while Complex stores real and imaginary attributes. Both structs can perform operations with other objects of the same type or with integer values through operator overloading. The main function demonstrates creating and manipulating Distance and Complex objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Operartor Overloading

The document defines two C++ structs - Distance and Complex - to represent distance and complex number objects. Both structs define default and parameterized constructors, setter and getter methods, and operator overloading for arithmetic operations like addition, subtraction, multiplication and division. The Distance struct stores feet and inches attributes while Complex stores real and imaginary attributes. Both structs can perform operations with other objects of the same type or with integer values through operator overloading. The main function demonstrates creating and manipulating Distance and Complex objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

// 1.

Distance:
#include<iostream>
using namespace std;
#include<string.h>
struct Distance
{
int feet,inch;

Distance() // Default constructor


{
cout << "\n\n**Default constructor**\n\n";
this -> feet = 0;
this -> inch = 0;
}
Distance(int f, int i) // Parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> feet = f;
this -> inch = i;
}

//setter function

void setFeet(int fe)


{
this->feet = fe;
}

void setInch (int in)


{
this->inch = in;
}

//getter function

int getFeet()
{
return this -> feet;
}

int getInch()
{
return this -> inch;
}

//display function

void display() //void display(distance * ptr)


{
cout << "\nDistance is feet :" << this->feet;
cout << "\nDistance is inch :" << this->inch;
}

// Addition.
Distance operator+ ( Distance c2) // Member Function
{
cout << "\n****** Addition of d1 and d2 ******" ;
Distance temp;
temp.feet = this -> feet + c2.feet;
temp.inch = this -> inch + c2.inch;

return temp;
}

Distance operator+ ( int a) // Member Function


{
cout << "\n****** Addition of d1 and 10 ******" ;
Distance temp;
temp.feet = this -> feet + a;
temp.inch = this -> inch + a;

return temp;
}

// Substraction

Distance operator- ( Distance c2) // Member Function


{
cout << "\n****** Substraction of d1 and d2 ******" ;
Distance temp;
temp.feet = this -> feet - c2.feet;
temp.inch = this -> inch - c2.inch;

return temp;
}

Distance operator- ( int a) // Member Function


{
cout << "\n****** Substraction of d1 and 10 ******" ;
Distance temp;
temp.feet = this -> feet - a;
temp.inch = this -> inch - a;

return temp;
}

// Multiplication

Distance operator* ( Distance c2) // Member Function


{
cout << "\n****** Multiplication of d1 and d2 ******" ;
Distance temp;
temp.feet = this -> feet * c2.feet;
temp.inch = this -> inch * c2.inch;

return temp;
}

Distance operator* ( int a) // Member Function


{
cout << "\n****** Multiplication of d1 and 10 ******" ;
Distance temp;
temp.feet = this -> feet * a;
temp.inch = this -> inch * a;

return temp;
}

// Division

Distance operator/ ( Distance c2) // Member Function


{
cout << "\n****** Division of d1 and d2 ******" ;
Distance temp;
temp.feet = this -> feet / c2.feet;
temp.inch = this -> inch / c2.inch;

return temp;
}

Distance operator/ ( int a) // Member Function


{
cout << "\n****** Division of d1 and 10 ******" ;
Distance temp;
temp.feet = this -> feet / a;
temp.inch = this -> inch / a;

return temp;
}

}; //Struct ends here

Distance operator+(int,Distance);
Distance operator-(int,Distance);
Distance operator*(int,Distance);
Distance operator/(int,Distance);

int main()
{
Distance d1; //created d1 object
d1.display(); //display function call

int fe,in;

cout << "\n\n\nEnter Feet :\n" ;


cin >> fe;
cout << "\n\nEnter Inch : \n";
cin >> in;

d1.setFeet(fe); //d1.feet=12;
d1.setInch(in); //d1.inch=9;

d1.display(); //display(&d1);

Distance d2(12,3); //created d1 object


d2.display(); //display function call

// Add
Distance d3; //object Created
d3 = d1 + d2 ; //d3 = d1.operator+(d2);
d3.display();

Distance d4; //obejct created


d4 = d1 - 10 ; //d4 = d1.operator+(10);
d4.display();

Distance d5;
cout << "\n****** Addition of 10 and d1 ******";
d5 = 10 - d1 ; //d5 = operator+(10,d1);
d5.display();

//sub

Distance d6; //object Created


d6 = d1 - d2 ; //d6 = d1.operator-(d2);
d6.display();

Distance d7; //obejct created


d7 = d1 - 10 ; //d7 = d1.operator-(10);
d7.display();

Distance d8;
cout << "\n****** Substraction of 10 and d1 ******";
d8 = 10 - d1 ; //d8 = operator-(10,d1);
d8.display();

//mul

Distance d9; //object Created


d9 = d1 * d2; // d9 = d1.operator*(d2);
d9.display();

Distance d10; //obejct created


d10 = d1 * 10 ; //d10 = d1.operator*(10);
d10.display();

Distance d11;
cout << "\n****** Multiplication of 10 and d1 ******";
d11 = 10 * d1 ; //d11 = operator*(10,d1);
d11.display();

//operator/

Distance d12; //object Created


d12 = d1 / d2; //d12 = d1.operator/(d2);
d12.display();

Distance d13; //obejct created


d13 = d1 / 10 ; //d13 = d1.operator/(10);
d13.display();

Distance d14;
cout << "\n****** Division of 10 and d1 ******";
d14 = 10 / d1 ; //d14 = operator/(10,d1);
d14.display();

return 0;
}

// Outside main

// Add

Distance operator+ ( int a ,Distance d1) // Non Member Function


{
Distance temp;

int feet , inch ;

feet = a + d1.getFeet(); //getter


inch = a + d1.getInch();

temp.setFeet(feet); //setter
temp.setInch(inch);

return temp;

// Sub

Distance operator- ( int a ,Distance d1) // Non Member Function


{
Distance temp;

int feet , inch ;

feet = a - d1.getFeet(); //getter


inch = a - d1.getInch();

temp.setFeet(feet); //setter
temp.setInch(inch);

return temp;

// Mul

Distance operator* ( int a ,Distance d1) // Non Member Function


{
Distance temp;

int feet , inch ;

feet = a * d1.getFeet(); //getter


inch = a * d1.getInch();

temp.setFeet(feet); //setter
temp.setInch(inch);
return temp;

// Div

Distance operator/ ( int a ,Distance d1) // Non Member Function


{
Distance temp;

int feet , inch ;

feet = a / d1.getFeet(); //getter


inch = a / d1.getInch();

temp.setFeet(feet); //setter
temp.setInch(inch);

return temp;
}
// 2.Complex:
#include<iostream>
using namespace std;
#include<string.h>

struct Complex
{
int real,imag;

Complex()// default constructor


{
cout << "\n\n**Default constructor**\n\n";
this->real=0;
this->imag=0;
}
Complex(int re, int im)//parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this-> real =re;
this-> imag =im;
}

void setReal(int rel)


{
this->real = rel;
}

void setImag(int img)


{
this->imag = img;
}

int getReal()
{
return this -> real;
}

int getImag()
{
return this -> imag;
}

void display() // void display(complex * this)


{
cout <<"\n"<< this -> real << "+" << this -> imag << "i";
}

// Addition.

Complex operator+ ( Complex c2) // Member Function


{
cout << "\n****** Addition of C1 and C2 ******" ;
Complex temp;
temp.real = this -> real + c2.real;
temp.imag = this -> imag + c2.imag;

return temp;
}

Complex operator+ ( int a) // Member Function


{
cout << "\n****** Addition of C1 and 10 ******" ;
Complex temp;
temp.real = this -> real + a;
temp.imag = this -> imag + a;

return temp;
}

// Substraction

Complex operator- ( Complex c2) // Member Function


{
cout << "\n****** Substraction of C1 and C2 ******" ;
Complex temp;
temp.real = this -> real - c2.real;
temp.imag = this -> imag - c2.imag;

return temp;
}

Complex operator- ( int a) // Member Function


{
cout << "\n****** Substraction of C1 and 10 ******" ;
Complex temp;
temp.real = this -> real - a;
temp.imag = this -> imag - a;

return temp;
}

// Multiplication

Complex operator* ( Complex c2) // Member Function


{
cout << "\n****** Multiplication of C1 and C2 ******" ;
Complex temp;
temp.real = this -> real * c2.real;
temp.imag = this -> imag * c2.imag;

return temp;
}

Complex operator* ( int a) // Member Function


{
cout << "\n****** Multiplication of C1 and 10 ******" ;
Complex temp;
temp.real = this -> real * a;
temp.imag = this -> imag * a;

return temp;
}

// Division

Complex operator/ ( Complex c2) // Member Function


{
cout << "\n****** Division of C1 and C2 ******" ;
Complex temp;
temp.real = this -> real / c2.real;
temp.imag = this -> imag / c2.imag;

return temp;
}

Complex operator/ ( int a) // Member Function


{
cout << "\n****** Division of C1 and 10 ******" ;
Complex temp;
temp.real = this -> real / a;
temp.imag = this -> imag / a;

return temp;
}
}; // Struct ends here

Complex operator+(int,Complex);
Complex operator-(int,Complex);
Complex operator*(int,Complex);
Complex operator/(int,Complex);

int main()
{
Complex c1; // object created
int real, imagine;

c1.display();

cout << "\n\nEnter real no for c1 :\n";


cin >> real ;
cout << "Enter imaginary no for c1\n";
cin >> imagine ;

c1.setReal(real);
c1.setImag(imagine);
cout << "\nValues of C1" ;
c1.display();

// parameterised

Complex c2(20,30); // object created


cout << "\nValues of C2" ;
c2.display();

// Add

Complex c3; //object Created


c3 = c1 + c2 ; //c3 = c1.operator+(c2); ..member func
c3.display();

Complex c4; //obejct created


c4 = c1 + 10 ; //c4 = c1.operator+(10); ..member func
c4.display();

Complex c5;
cout << "\n****** Addition of 10 and c1 ******";
c5 = 10 + c1 ; // operator+(10,c1); ...non member
c5.display();

//Sub

Complex c6; //object Created


c6 = c1 - c2 ; //c6 = c1.operator-(c2);
c6.display();

Complex c7; //obejct created


c7 = c1 - 10 ; //c7 = c1.operator-(10);
c7.display();

Complex c8;
cout << "\n****** Substraction of 10 and c1 ******";
c8 = 10 - c1 ; //c8 = operator-(10,c1);
c8.display();

//Mul

Complex c9; //object Created


c9 = c1 * c2 ; //c9 = c1.operator*(c2);
c9.display();

Complex c10; //obejct created


c10 = c1 * 10 ; //c10 = c1.operator*(10);
c10.display();

Complex c11;
cout << "\n****** Multiplication of 10 and c1 ******";
c11 = 10 * c1 ; //c11 = operator*(10,c1);
c11.display();

//Div

Complex c12; //object Created


c12 = c1 / c2 ; //c12 = c1.operator/(c2);
c12.display();

Complex c13; //obejct created


c13 = c1 / 10 ; //c13 = c1.operator/(10);
c13.display();

Complex c14;
cout << "\n****** Division of 10 and c1 ******";
c14 = 10 / c1 ; //c14 = operator/(10,c1);
c14.display();

return 0;
}

// Outside main

// Add

Complex operator+ ( int a ,Complex c1) // Non Member Function


{
Complex temp;

int real , imag ;

real = a + c1.getReal(); //getter


imag = a + c1.getImag();

temp.setReal(real); //setter
temp.setImag(imag);

return temp;

// Sub

Complex operator- ( int a ,Complex c1) // Non Member Function


{
Complex temp;

int real , imag ;

real = a - c1.getReal(); //getter


imag = a - c1.getImag();

temp.setReal(real); //setter
temp.setImag(imag);

return temp;

// Mul

Complex operator* ( int a ,Complex c1) // Non Member Function


{
Complex temp;

int real , imag ;

real = a * c1.getReal(); //getter


imag = a * c1.getImag();

temp.setReal(real); //setter
temp.setImag(imag);

return temp;

}
// Div

Complex operator/ ( int a ,Complex c1) // Non Member Function


{
Complex temp;

int real , imag ;

real = a / c1.getReal(); //getter


imag = a / c1.getImag();

temp.setReal(real); //setter
temp.setImag(imag);

return temp;

}
// 1.Complex:
#include<iostream>
using namespace std;
#include<string.h>

struct Complex
{
int real,imag;

Complex()// default constructor


{
cout << "\n\n**Default constructor**\n\n";
this->real=0;
this->imag=0;
}
Complex(int re, int im)//parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this-> real =re;
this-> imag =im;
}

void setReal(int rel)


{
this->real = rel;
}

void setImag(int img)


{
this->imag = img;
}

int getReal()
{
return this -> real;
}

int getImag()
{
return this -> imag;
}

void display() // void display(complex * this)


{
cout <<"\n"<< this -> real << "+" << this -> imag << "i\n";
}

// Greater

int operator > ( Complex c2) // Member Function


{
int flag = 0 ;
if(this->real > c2.real)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator > ( int a) // Member Function


{
int flag = 0 ;
if(this->real > a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

// Smaller

int operator < ( Complex c2) // Member Function


{
int flag = 0 ;
if(this->real < c2.real)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator < ( int a) // Member Function


{
int flag = 0 ;
if(this->real < a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

// Equal to Equal to

int operator == ( Complex c2) // Member Function


{
int flag = 0 ;
if(this->real == c2.real)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator == ( int a) // Member Function


{
int flag = 0 ;
if(this->real == a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

// Not Equal to

int operator != ( Complex c2) // Member Function


{
int flag = 0 ;
if(this->real != c2.real)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator != ( int a) // Member Function


{
int flag = 0 ;
if(this->real != a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}
}; // Struct ends here

int operator > (int , Complex);


int operator < (int , Complex);
int operator == (int , Complex);
int operator != (int , Complex);

int main()
{
Complex c1; // object created
int real, imagine;

c1.display();

cout << "\n\nEnter real no for c1 :\n";


cin >> real ;
cout << "Enter imaginary no for c1\n";
cin >> imagine ;

c1.setReal(real);
c1.setImag(imagine);
cout << "\nValues of C1" ;
c1.display();

// parameterised

Complex c2(20,30); // object created


cout << "\nValues of C2" ;
c2.display();

// Greater
// A}
int c3 = c1 > c2; //c3 = c1.operator>(c2);
if( c3 == 1)
{
cout << "\nc1 is greater" ;
}
else
{
cout << "\nc2 is greater" ;
}

// B}

int c4 = c1 > 10; //c4 = c1.operator>(10);


if( c4 == 1)
{
cout << "\nc1 is greater" ;
}
else
{
cout << "\n10 is greater" ;
}

//C}
int c11 = 10 > c1 ;

// Smaller
// A}
int c5 = c1 < c2; //c5 = c1.operator>(c2);
if( c5 == 1)
{
cout << "\nc1 is smaller" ;
}
else
{
cout << "\nc2 is smaller" ;
}

// B)

int c6 = c1 < 10; //c6 = c1.operator>(10);


if( c6 == 1)
{
cout << "\nc1 is smaller" ;
}
else
{
cout << "\n10 is smaller" ;
}

//C}

int c12 = 10 > c1 ;

// Equal to Equal to
// A}

int c7 = c1 == c2; //c7 = c1.operator==(c2);


if( c7 == 1)
{
cout << "\nc1 is equal to c2" ;
}
else
{
cout << "\nc1 is not equal to c2" ;
}

// B}

int c8 = c1 == 10; //c8 = c1.operator==(10);


if( c8 == 1)
{
cout << "\nc1 is equal to c2" ;
}
else
{
cout << "\nc1 is not equal to c2" ;
}

//C}
int c13 = 10 > c1 ;

// Not Equal to
// A}

int c9 = c1 != c2; //c9 = c1.operator!=(c2);


if( c9 == 1)
{
cout << "\nc1 is greater" ;
}
else
{
cout << "\nc2 is greater" ;
}

// B}

int c10 = c1 != 10; //c10 = c1.operator>(10);


if( c10 == 1)
{
cout << "\nc1 is greater" ;
}
else
{
cout << "\n10 is greater" ;
}

//C}

int c14 = 10 > c1 ;

return 0;

// Non Member Function

int operator > (int a , Complex c2)


{
int c11 = a > c2.real;
if(c11 == 1 )
{
cout << "\nIn c11 : a is Greater\n";
}
else
{
cout << "\nIn c11 : c2 is Greater\n";
}
}

int operator < (int a , Complex c2)


{
int c12 = a < c2.real;
if(c12 == 1 )
{
cout << "\nIn c12 : a is Greater\n";
}
else
{
cout << "\nIn c12 : c2 is Greater\n";
}
}

int operator == (int a , Complex c2)


{
int c13 = a == c2.real;
if(c13 == 1 )
{
cout << "\nIn c13 : a is Greater\n";
}
else
{
cout << "\nIn c13 : c2 is Greater\n";
}
}

int operator != (int a , Complex c2)


{
int c14 = a != c2.real;
if(c14 == 1 )
{
cout << "\nIn c14 : a is Greater\n";
}
else
{
cout << "\nIn c14 : c2 is Greater\n";
}
}
// 2.Distance:
#include<iostream>
using namespace std;
#include<string.h>
struct Distance
{
int feet,inch;

Distance() // Default constructor


{
cout << "\n\n**Default constructor**\n\n";
this -> feet = 0;
this -> inch = 0;
}
Distance(int f, int i) // Parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> feet = f;
this -> inch = i;
}

//setter function

void setFeet(int fe)


{
this->feet = fe;
}

void setInch (int in)


{
this->inch = in;
}

//getter function

int getFeet()
{
return this -> feet;
}

int getInch()
{
return this -> inch;
}

//display function

void display() //void display(distance * ptr)


{
cout << "\nDistance is feet :" << this->feet;
cout << "\nDistance is inch :" << this->inch;
}

// Greater
int operator > ( Distance d2) // Member Function
{
int flag = 0 ;
if(this->feet > d2.feet)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator > ( int a) // Member Function


{
int flag = 0 ;
if(this->feet > a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

// Smaller

int operator < ( Distance d2) // Member Function


{
int flag = 0 ;
if(this->feet < d2.feet)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator < ( int a) // Member Function


{
int flag = 0 ;
if(this->feet < a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

// Equal to Equal to

int operator == ( Distance d2) // Member Function


{
int flag = 0 ;
if(this->feet == d2.feet)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator == ( int a) // Member Function


{
int flag = 0 ;
if(this->feet == a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

// Not Equal to

int operator != ( Distance d2) // Member Function


{
int flag = 0 ;
if(this->feet != d2.feet)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

int operator != ( int a) // Member Function


{
int flag = 0 ;
if(this->feet != a)
{
flag = 1;
}
else
{
flag = 0;
}
return flag ;
}

}; //Struct ends here

int operator > (int , Distance);


int operator < (int , Distance);
int operator == (int , Distance);
int operator != (int , Distance);

int main()
{
Distance d1; //created d1 object
d1.display(); //display function call

int fe,in;

cout << "\n\n\nEnter Feet :\n" ;


cin >> fe;
cout << "\n\nEnter Inch : \n";
cin >> in;

d1.setFeet(fe); //d1.feet=12;
d1.setInch(in); //d1.inch=9;
cout << "\nValues of D1" ;
d1.display(); //display(&d1);

Distance d2(12,3); //created d1 object


cout << "\nValues of D2" ;
d2.display(); //display function call

// Greater
// A}
int d3 = d1 > d2; //d3 = d1.operator>(d2);
if( d3 == 1)
{
cout << "\nd1 is greater" ;
}
else
{
cout << "\nd2 is greater" ;
}

// B}

int d4 = d1 > 10; //d4 = d1.operator>(10);


if( d4 == 1)
{
cout << "\nd1 is greater" ;
}
else
{
cout << "\n10 is greater" ;
}

//C}

int d11 = 10 > d1 ;

// Smaller
// A}
int d5 = d1 < d2; //d5 = d1.operator>(d2);
if( d5 == 1)
{
cout << "\nd1 is smaller" ;
}
else
{
cout << "\nd2 is smaller" ;
}

// B)

int d6 = d1 < 10; //d6 = d1.operator>(10);


if( d6 == 1)
{
cout << "\nd1 is smaller" ;
}
else
{
cout << "\n10 is smaller" ;
}

//C}

int d12 = 10 > d1 ;

// Equal to Equal to
// A}

int d7 = d1 == d2; //d7 = d1.operator==(d2);


if( d7 == 1)
{
cout << "\nd1 is equal to d2" ;
}
else
{
cout << "\nd1 is not equal to d2" ;
}

// B}

int d8 = d1 == 10; //d8 = d1.operator==(10);


if( d8 == 1)
{
cout << "\nd1 is equal to d2" ;
}
else
{
cout << "\nd1 is not equal to d2" ;
}

//C}

int d13 = 10 > d1 ;

// Not Equal to
// A}

int d9 = d1 != d2; //d9 = d1.operator!=(d2);


if( d9 == 1)
{
cout << "\nd1 is greater" ;
}
else
{
cout << "\nd2 is greater" ;
}

// B}

int d10 = d1 != 10; //d10 = d1.operator>();


if( d10 == 1)
{
cout << "\nd1 is greater" ;
}
else
{
cout << "\n10 is greater" ;
}

//C}

int d14 = 10 > d1 ;


return 0;
}

// Non Member Function

int operator > (int a , Distance d2)


{
int d11 = a > d2.feet;
if(d11 == 1 )
{
cout << "\nIn d11 : a is Greater";
}
else
{
cout << "\nIn d11 : d2 is Greater";
}
}
int operator < (int a , Distance d2)
{
int d12 = a < d2.feet;
if(d12 == 1 )
{
cout << "\nIn d11 : a is Greater";
}
else
{
cout << "\nIn d11 : d2 is Greater";
}
}

int operator == (int a , Distance d2)


{
int d13 = a == d2.feet;
if(d13 == 1 )
{
cout << "\nIn d11 : a is Greater";
}
else
{
cout << "\nIn d11 : d2 is Greater";
}
}

int operator != (int a , Distance d2)


{
int d14 = a != d2.feet;
if(d14 == 1 )
{
cout << "\nIn d11 : a is Greater";
}
else
{
cout << "\nIn d11 : d2 is Greater";
}
}

You might also like