0% found this document useful (0 votes)
54 views29 pages

Lab Programs Opov All

The document contains 6 code examples demonstrating operator overloading in C++ using member functions and friend functions. The examples overload operators for arithmetic operations, relational operations, and assignment. Specifically, the examples show how to overload operators like +, -, *, /, <, >, ==, = through member functions and friend functions to perform operations on user-defined classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views29 pages

Lab Programs Opov All

The document contains 6 code examples demonstrating operator overloading in C++ using member functions and friend functions. The examples overload operators for arithmetic operations, relational operations, and assignment. Specifically, the examples show how to overload operators like +, -, *, /, <, >, ==, = through member functions and friend functions to perform operations on user-defined classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

1. C++ Program for evaluating the expression ob3=ob1+ob2.

//ob3=ob1+ob2
#include<iostream>
using namespace std;

class Add
{
private:
int a;
public:
void input();
void display();
Add operator +(Add);
};

void Add::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}

void Add::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

Add Add::operator +(Add ob)


{
Add temp;
temp.a=a+ob.a;
return(temp);
}
int main()
{
Add ob1,ob2,ob3;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Object 1: "<<endl;
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.display();
cout<<"\n Result of the expression ob3=ob1+ob2: "<<endl;
ob3=ob1+ob2;
ob3.display();
return 0;
}

2. C++ Program for overloading of all arithmetic operators using member function.

//OVERLOADING OF ALL ARITHMETIC OPERATORS USING MEMBER FUNCTION


#include<iostream>
using namespace std;
#include<stdlib.h>

class Operate
{
private:
int a;
public:
void input();
void display();
Operate operator +(Operate);
Operate operator -(Operate);
Operate operator *(Operate);
Operate operator /(Operate);
Operate operator %(Operate);
};

void Operate::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}

void Operate::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

Operate Operate::operator +(Operate ob)


{
Operate temp;
temp.a=a+ob.a;
return(temp);
}

Operate Operate::operator -(Operate ob)


{
Operate temp;
temp.a=a-ob.a;
return(temp);
}

Operate Operate::operator *(Operate ob)


{
Operate temp;
temp.a=a*ob.a;
return(temp);
}

Operate Operate::operator /(Operate ob)


{
Operate temp;
if(ob.a!=0)
{
temp.a=a/(ob.a);
return(temp);
}
else
{
cout<<"\n Divide by Zero error"<<endl;
exit(1);
}
}

Operate Operate::operator %(Operate ob)


{
Operate temp;
if(ob.a!=0)
{
temp.a=a%(ob.a);
return(temp);
}
else
{
cout<<"\n Divide by Zero error"<<endl;
exit(1);
}
}

int main()
{
Operate ob1,ob2,ob3,ob4,ob5,ob6,ob7;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Object 1: "<<endl;
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.display();
cout<<"\n Sum: "<<endl;
ob3=ob1+ob2;
ob3.display();
cout<<"\n Difference: "<<endl;
ob4=ob1-ob2;
ob4.display();
cout<<"\n Product: "<<endl;
ob5=ob1*ob2;
ob5.display();
cout<<"\n Quotient: "<<endl;
ob6=ob1/ob2;
ob6.display();
cout<<"\n Remainder: "<<endl;
ob7=ob1%ob2;
ob7.display();
return 0;
}

3. C++ Program for overloading of all arithmetic operators using friend function.

//OVERLOADING OF ALL ARITHMETIC OPERATORS USING FRIEND FUNCTION


#include<iostream>
using namespace std;
#include<stdlib.h>
class Operate
{
private:
int a;
public:
void input();
void display();
friend Operate operator +(Operate,Operate);
friend Operate operator -(Operate,Operate);
friend Operate operator *(Operate,Operate);
friend Operate operator /(Operate,Operate);
friend Operate operator %(Operate,Operate);
};

void Operate::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}

void Operate::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

Operate operator +(Operate ob1, Operate ob2)


{
Operate temp;
temp.a=ob1.a+ob2.a;
return(temp);
}

Operate operator -(Operate ob1, Operate ob2)


{
Operate temp;
temp.a=ob1.a-ob2.a;
return(temp);
}

Operate operator *(Operate ob1, Operate ob2)


{
Operate temp;
temp.a=ob1.a*ob2.a;
return(temp);
}

Operate operator /(Operate ob1, Operate ob2)


{
Operate temp;
if(ob2.a!=0)
{
temp.a=(ob1.a)/(ob2.a);
return(temp);
}
else
{
cout<<"\n Divide by Zero error"<<endl;
exit(1);
}
}
Operate operator %(Operate ob1, Operate ob2)
{
Operate temp;
if(ob2.a!=0)
{
temp.a=(ob1.a)%(ob2.a);
return(temp);
}
else
{
cout<<"\n Divide by Zero error"<<endl;
exit(1);
}
}
int main()
{
Operate ob1,ob2,ob3,ob4,ob5,ob6,ob7;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Object 1: "<<endl;
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.display();
cout<<"\n Sum: "<<endl;
ob3=ob1+ob2;
ob3.display();
cout<<"\n Difference: "<<endl;
ob4=ob1-ob2;
ob4.display();
cout<<"\n Product: "<<endl;
ob5=ob1*ob2;
ob5.display();
cout<<"\n Quotient: "<<endl;
ob6=ob1/ob2;
ob6.display();
cout<<"\n Remainder: "<<endl;
ob7=ob1%ob2;
ob7.display();
return 0;
}
4. C++ Program for overloading of relational operators using member function.

//OVERLOADING OF RELATIONAL OPERATORS USING MEMBER FUNCTION


#include<iostream>
using namespace std;

class Relation
{
private:
int a;
public:
void input();
void display();
void operator <(Relation);
void operator >(Relation);
void operator ==(Relation);
void operator !=(Relation);
};

void Relation::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}

void Relation::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

void Relation::operator <(Relation ob)


{
if (a < ob.a)
cout<<"\n"<<a<<" is smaller"<<endl;
else
cout<<"\n"<<ob.a<<" is smaller"<<endl;
}

void Relation::operator >(Relation ob)


{
if (a > ob.a)
cout<<"\n"<<a<<" is larger"<<endl;
else
cout<<"\n"<<ob.a<<" is larger"<<endl;
}

void Relation::operator ==(Relation ob)


{
if (a == ob.a)
cout<<"\n Both are equal"<<endl;
else
cout<<"\n Both are not equal"<<endl;
}

void Relation::operator !=(Relation ob)


{
if (a != ob.a)
cout<<"\n Both are not equal"<<endl;
else
cout<<"\n Both are equal"<<endl;
}

int main()
{
Relation ob1,ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Less Than: "<<endl;
ob1<ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Greater Than: "<<endl;
ob1>ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Equal To: "<<endl;
ob1==ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Not Equal To: "<<endl;
ob1!=ob2;
return 0;
}

5. C++ Program for overloading of relational operators using friend function.

//OVERLOADING OF RELATIONAL OPERATORS USING FRIEND FUNCTION


#include<iostream>
using namespace std;

class Relation
{
private:
int a;
public:
void input();
void display();
friend void operator <(Relation,Relation);
friend void operator >(Relation,Relation);
friend void operator ==(Relation,Relation);
friend void operator !=(Relation,Relation);
};

void Relation::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}

void Relation::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

void operator <(Relation ob1,Relation ob2)


{
if (ob1.a < ob2.a)
cout<<"\n"<<ob1.a<<" is smaller"<<endl;
else
cout<<"\n"<<ob2.a<<" is smaller"<<endl;
}

void operator >(Relation ob1,Relation ob2)


{
if (ob1.a > ob2.a)
cout<<"\n"<<ob1.a<<" is larger"<<endl;
else
cout<<"\n"<<ob2.a<<" is larger"<<endl;
}

void operator ==(Relation ob1,Relation ob2)


{
if (ob1.a == ob2.a)
cout<<"\n Both are equal"<<endl;
else
cout<<"\n Both are not equal"<<endl;
}

void operator !=(Relation ob1,Relation ob2)


{
if (ob1.a != ob2.a)
cout<<"\n Both are not equal"<<endl;
else
cout<<"\n Both are equal"<<endl;
}

int main()
{
Relation ob1,ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Less Than: "<<endl;
ob1<ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Greater Than: "<<endl;
ob1>ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Equal To: "<<endl;
ob1==ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Not Equal To: "<<endl;
ob1!=ob2;
return 0;
}

6. C++ Program for overloading of assignment operator using member function.

//OVERLOADING OF ASSIGNMENT OPERATOR USING MEMBER FUNCTION


#include<iostream>
using namespace std;
class Assign
{
private:
int a;
int b;
public:
void input();
void display();
void operator =(Assign);
};
void Assign::input()
{
cout<<"\n Enter the values of a and b: "<<endl;
cin>>a>>b;
}
void Assign::display()
{
cout<<"\n The value of a: "<<a<<endl;
cout<<"\n The value of b: "<<b<<endl;
}
void Assign::operator =(Assign ob)
{
a=ob.a;
b=ob.b;
}
int main()
{
Assign ob1,ob2;
cout<<"\n Object 1: "<<endl;
ob1.input();
cout<<"\n Object 2: "<<endl;
ob2.input();
cout<<"\n Before Assignment: "<<endl;
cout<<"\n Object 1: "<<endl;
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.display();
cout<<"\n After Assignment: "<<endl;
ob1=ob2;
cout<<"\n Object 1: "<<endl;
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.display();
return 0;
}
7. C++ program for evaluating the expression ob3=ob1+10.

//ob3=ob1+10
#include<iostream>
using namespace std;

class num
{
private:
int a;
public:
void input();
void display();
num operator +(int);
};

void num::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}

void num::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

num num::operator +(int p)


{
num temp;
temp.a=a+p;
return(temp);
}
int main()
{
num ob1,ob3;
cout<<"\n Object 1: "<<endl;
ob1.input();
ob1.display();
ob3=ob1+10;
cout<<"\n Result of the expression ob3=ob1+10: "<<endl;
ob3.display();
return 0;
}

8. C++ program for evaluating the expression ob3=5+ob1.

//ob3=5+ob1
#include<iostream>
using namespace std;

class num
{
private:
int a;
public:
void input();
void display();
friend num operator +(int,num);
};

void num::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}
void num::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

num operator +(int p, num ob)


{
num temp;
temp.a=p+ob.a;
return(temp);
}

int main()
{
num ob1,ob3;
cout<<"\n Object 1: "<<endl;
ob1.input();
ob1.display();
ob3=5+ob1;
cout<<"\n Result of the expression ob3=5+ob1: "<<endl;
ob3.display();
return 0;
}

9. C++ program for evaluating the expression ob3=ob1+ob2*5.

//ob3=ob1+ob2*5
#include<iostream>
using namespace std;

class num
{
private:
int a;
public:
void input();
void display();
num operator *(int);
num operator +(num);
};

void num::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}

void num::display()
{
cout<<"\n The value of a: "<<a<<endl;
}

num num::operator *(int p)


{
num temp;
temp.a=a*p;
return(temp);
}

num num::operator +(num ob)


{
num temp;
temp.a=a+ob.a;
return(temp);
}
int main()
{
num ob1,ob2,ob3;
cout<<"\n Object 1: "<<endl;
ob1.input();
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.input();
ob2.display();
ob3=ob1+ob2*5;
cout<<"\n Result of the expression ob3=ob1+ob2*5: "<<endl;
ob3.display();
return 0;
}

10. C++ Program for evaluating the expression ob3=ob1*ob2+5.

//ob3=ob1*ob2+5
#include<iostream>
using namespace std;
class num
{
private:
int a;
public:
void input();
void display();
num operator *(num);
num operator +(int);
};
void num::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}
void num::display()
{
cout<<"\n The value of a: "<<a<<endl;
}
num num::operator *(num ob)
{
num temp;
temp.a=a*ob.a;
return(temp);
}

num num::operator +(int p)


{
num temp;
temp.a=a+p;
return(temp);
}

int main()
{
num ob1,ob2,ob3;
cout<<"\n Object 1: "<<endl;
ob1.input();
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.input();
ob2.display();
ob3=ob1*ob2+5;
cout<<"\n Result of the expression ob3=ob1*ob2+5: "<<endl;
ob3.display();
return 0;
}
11. C++ program for evaluating the expression ob3=ob1-5*ob2.

//ob3=ob1-5*ob2
#include<iostream>
using namespace std;

class num
{
private:
int a;
public:
void input();
void display();
friend num operator *(int,num);
num operator -(num);
};
void num::input()
{
cout<<"\n Enter the value of a: "<<endl;
cin>>a;
}
void num::display()
{
cout<<"\n The value of a: "<<a<<endl;
}
num operator *(int p, num ob)
{
num temp;
temp.a=p*ob.a;
return(temp);
}
num num::operator -(num ob)
{
num temp;
temp.a=a-ob.a;
return(temp);
}

int main()
{
num ob1,ob2,ob3;
cout<<"\n Object 1: "<<endl;
ob1.input();
ob1.display();
cout<<"\n Object 2: "<<endl;
ob2.input();
ob2.display();
ob3=ob1-5*ob2;
cout<<"\n Result of the expression ob1-5*ob2: "<<endl;
ob3.display();
return 0;
}

12. C++ Program to overload the operators for addition, subtraction, multiplication and equality
of two objects of Matrix class.

//OPERATOR OVERLOADING FOR MATRICES


#include<iostream>
using namespace std;

class Matrix
{
private:
int row,col;
int A[3][3];
public:
void input();
void display();
Matrix operator +(Matrix);
Matrix operator -(Matrix);
Matrix operator *(Matrix);
Matrix operator ==(Matrix);
};

void Matrix::input()
{
cout<<"\n Enter the number of rows and columns for the matrix: "<<endl;
cin>>row>>col;
cout<<"\n Enter the matrix elements: "<<endl;
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
cin>>A[i][j];
}

void Matrix::display()
{
cout<<"\n The matrix is: "<<endl;
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
}

Matrix Matrix::operator +(Matrix ob)


{
Matrix temp;
if((row != ob.row) || (col != ob.col))
{
cout<<"\n The matrices are not of the same order";
exit(1);
}
else
{
temp.row=ob.row;
temp.col=ob.col;
for(int i=0; i<ob.row; i++)
for(int j=0; j<ob.col; j++)
temp.A[i][j]=A[i][j]+ob.A[i][j];
}
return(temp);
}

Matrix Matrix::operator -(Matrix ob)


{
Matrix temp;
if((row != ob.row) || (col != ob.col))
{
cout<<"\n The matrices are not of the same order";
exit(1);
}
else
{
temp.row=ob.row;
temp.col=ob.col;
for(int i=0; i<ob.row; i++)
for(int j=0; j<ob.col; j++)
temp.A[i][j]=A[i][j]-ob.A[i][j];
}
return(temp);
}

Matrix Matrix::operator *(Matrix ob)


{
Matrix temp;
if(col != ob.row)
{
cout<<"\n The matrices are not of the same order";
exit(1);
}
else
{
temp.row=ob.row;
temp.col=ob.col;
for(int i=0; i<row; i++)
{
for(int j=0; j<ob.col; j++)
{
temp.A[i][j]=0;
for(int k=0; k<col; k++)
temp.A[i][j] = temp.A[i][j] + (A[i][k]*ob.A[k][j]);
}
}
}
return(temp);
}

Matrix Matrix::operator ==(Matrix ob)


{
int count=0;
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
if (A[i][j] == ob.A[i][j])
count++;
if(count==row*col)
cout<<"\n The two matrices are equal"<<endl;
else
cout<<"\n The two matrices are not equal"<<endl;
}
int main()
{
Matrix ob1,ob2,ob3,ob4,ob5;
cout<<"\n 1st Matrix: "<<endl;
ob1.input();
cout<<"\n 2nd Matrix: "<<endl;
ob2.input();
cout<<"\n 1st Matrix: "<<endl;
ob1.display();
cout<<"\n 2nd Matrix: "<<endl;
ob2.display();
cout<<"\n Matrix Sum: "<<endl;
ob3=ob1+ob2;
ob3.display();
cout<<"\n Matrix Difference: "<<endl;
ob4=ob1-ob2;
ob4.display();
cout<<"\n Matrix Product: "<<endl;
ob5=ob1*ob2;
ob5.display();
cout<<"\n Matrix Equality: "<<endl;
ob1==ob2;
return 0;
}

13. C++ Program to overload the operators for addition, subtraction and multiplication of two
objects of Complex class.

//OPERATOR OVERLOADING FOR COMPLEX NUMBERS


#include<iostream>
using namespace std;
class Complex
{
private:
float real;
float img;
public:
void input();
void display();
Complex operator +(Complex);
Complex operator -(Complex);
Complex operator *(Complex);
};
void Complex::input()
{
cout<<"\n Enter the real and imaginary parts of the complex number: "<<endl;
cin>>real>>img;
}
void Complex::display()
{
cout<<"\n The complex number is: "<<real<<"+"<<img<<"i"<<endl;

}
Complex Complex::operator +(Complex ob)
{
Complex temp;
temp.real=real+ob.real;
temp.img=img+ob.img;
return(temp);
}
Complex Complex::operator -(Complex ob)
{
Complex temp;
temp.real=real-ob.real;
temp.img=img-ob.img;
return(temp);
}
Complex Complex::operator *(Complex ob)
{
Complex temp;
temp.real=real*ob.real-img*ob.img;
temp.img=real*ob.img+img*ob.real;
return(temp);
}

int main()
{
Complex ob1,ob2,ob3,ob4,ob5;
cout<<"\n 1st Complex Number: "<<endl;
ob1.input();
cout<<"\n 2nd Complex Number: "<<endl;
ob2.input();
cout<<"\n 1st Complex Number: "<<endl;
ob1.display();
cout<<"\n 2nd Complex Number: "<<endl;
ob2.display();
cout<<"\n Sum of two complex numbers: "<<endl;
ob3=ob1+ob2;
ob3.display();
cout<<"\n Difference of two complex numbers: "<<endl;
ob4=ob1-ob2;
ob4.display();
cout<<"\n Product of two complex numbers: "<<endl;
ob5=ob1*ob2;
ob5.display();
return 0;
}

You might also like