OPERATOR OVERLOADING

 

PROGRAM 1:

DEFINE A CLASS MAT OF SIZE M*N, DEFINE +, -, AND * OPERATOR
OF MAT OBJECTS. WRITE A CPP PROGRAM TO TEST ABOVE CLASS
FOR ABOVE OPERATIONS ON THE MAT OBJECTS.



#include <iostream>
#include <iomanip>

class MAT
{
	int m,n, mat[100][100];
public:
	void operator+(MAT);
	void operator-(MAT);
	void operator*(MAT);
	void getdata();
	void display();
};

void MAT::getdata()
{
	std::cout<<"Enter m: ";
	std::cin>>m;
	std::cout<<"Enter n: ";
	std::cin>>n;
	mat[m][n];
	for(int i=0; i<m; i++)
	{
		for(int j=0; j<n;j++){
			std::cout<<"Array["<<i<<"]["<<j<<"]: ";
			std::cin>>mat[i][j];}
	}
}

void MAT::display()
{
	for(int i=0; i<m; i++)
	{
		for(int j=0; j<n; j++)
		{
			std::cout<<mat[i][j]<<" ";
		}
		std::cout<<std::endl;
	}
	std::cout<<std::endl;
}

void MAT::operator+(MAT m1)
{
	if(m==m1.m && n==m1.n)
	{
		for(int i=0; i<m; i++)
		{
			for(int j=0; j<n; j++)
			{
				std::cout<<std::setw(3)<<mat[i][j]+m1.mat[i][j];
			}
			std::cout<<std::endl;
		}
	}
	else
	{
		std::cout<<"Addition not possible"<<std::endl;
	}
}

void MAT::operator*(MAT m2)
{
	int sum=0;
	if(n==m2.m)
	{
		for(int i=0; i<m; i++)
		{
			for(int j=0; j<m2.n; j++)
			{
				for (int k=0; k<m2.m; k++)
				{
					sum+=mat[i][k]*m2.mat[k][j];
				}
				std::cout<<std::setw(4)<<sum;
				sum=0;
			}
			std::cout<<std::endl;
		}
	}
	else
	{
		std::cout<<"Multiplication not possible"<<std::endl;
	}
}

void MAT::operator-(MAT m3)
{
	if (m==m3.m && n==m3.n)
	{
		for(int i=0; i<m; i++)
		{
			for(int j=0; j<n; j++)
			{
				std::cout<<std::setw(3)<<mat[j][j]-m3.mat[i][j];
			}
		std::cout<<std::endl;
		}
	}
	else
	{
		std::cout<<"Subtraction not possible"<<std::endl;
	}
}

int main()
{
	MAT ob1, ob2, add ,sub, mul;

	std::cout<<"Enter element in 1st array: "<<std::endl;
	ob1.getdata();
	std::cout<<"Enter element in 2nd array: "<<std::endl;
	ob2.getdata();
	std::cout<<"Array 1: "<<std::endl;
	ob1.display();
	std::cout<<"Array 2: "<<std::endl;
	ob2.display();
	std::cout<<"Addition: "<<std::endl;
	ob1+ob2;
	std::cout<<"Subtraction: "<<std::endl;
	ob1-ob2;
	std::cout<<"Multiplication: "<<std::endl;
	ob1*ob2;
	
}
// < IS FOR LESS THAN; > FOR GREATER THAN; [ FOR LEFT THIRD BRACKET; ] FOR RIGHT; 

INPUT- OUTPUT:

Enter element in 1st array: 
Enter m: 3
Enter n: 3
Array[0][0]: 1
Array[0][1]: 2
Array[0][2]: 3
Array[1][0]: 4
Array[1][1]: 5
Array[1][2]: 6
Array[2][0]: 7
Array[2][1]: 8
Array[2][2]: 9
Enter element in 2nd array: 
Enter m: 3
Enter n: 3
Array[0][0]: 9
Array[0][1]: 8
Array[0][2]: 7
Array[1][0]: 6
Array[1][1]: 5
Array[1][2]: 4
Array[2][0]: 3
Array[2][1]: 2
Array[2][2]: 1
Array 1: 
1 2 3 
4 5 6 
7 8 9 

Array 2: 
9 8 7 
6 5 4 
3 2 1 

Addition: 
 10 10 10
 10 10 10
 10 10 10
Subtraction: 
 -8 -3  2
 -5  0  5
 -2  3  8
Multiplication: 
 30  24  18
 84  69  54
138 114  90

PROGRAM 2:

WRITE A PROGRAM CLASS NAME CLOCK WITH DATA MEMBERS, HOURS, MINUTES, AND SECONDS. WRITE A PROGRAM TO ADD TWO INPUT TIME USING OPERATOR OVERLOADING.

C++ PROGRAM:



#include <iostream>
class clockk
{
	int m,s,h;
public:
	clockk()
	{	}
	void getdata()
	{
		std::cout<<"Enter values: "<<std::endl<<"Hour: ";
		std::cin>>h;
		std::cout<<"minutes: ";
		std::cin>>m;
		std::cout<<"seconds: ";
		std::cin>>s;
	}
	clockk operator+ (clockk b)
	{
		clockk temp;
		temp.h = h+b.h;
		temp.m=m+b.m;
		temp.s=s+b.s;
		return (temp);
	}
	void display(void)
	{
		if(s>=60)
		{
			m++;
			s-=60;
		}
		if(m>=60)
		{
			h++;
			m-=60;
		}
		std::cout<<h<<"h "<<m<<"m "<<s<<"s "<<std::endl;
	}
};

int main()
{


	clockk t1, t2, t3;
	std::cout<<"Enter first input: "<<std::endl;
	t1.getdata();
	std::cout<<"Enter second input: "<<std::endl;
	t2.getdata();
	t3=t1+t2;
	std::cout<<"T1= "; t1.display();
	std::cout<<"T2= "; t2.display();
	std::cout<<"Addition Result: ";
	t3.display();
}

INPUT-OUTPUT:


Enter first input: 
Enter values: 
Hour: 3
minutes: 30
seconds: 00
Enter second input: 
Enter values: 
Hour: 3
minutes: 30
seconds: 00
INITIALIZE
T1= 3h 30m 0s 
T2= 3h 30m 0s 
Addition Result: 7h 0m 0s 

PROGRAM 3:

USING OVERLOADING '+' OPERATOR TO CONCATENATE TWO STRING OBJECT.


#include <iostream>
#include <cstring>

class string{
public:
	char s[20];
	int size;

	void getstring(char str[])
	{
		strcpy(s,str);
	}

	void operator+(string);
};

void string::operator+(string ob)
{
strcat(s,ob.s);
std::cout<<"\nConcatenated String is: "<<s;	
}
int main()
{
	string ob1,ob2;
	char string1[10],string2[10];
	std::cout<<"Enter First string: ";
	std::cin>>string1;
	ob1.getstring(string1);
	std::cout<<"\nEnter second string: ";
	std::cin>>string2;
	ob2.getstring(string2);
	ob1+ob2;
}

INPUT-OUTPUT:

Enter First string: Shout
Enter second string: coders
Concatenated String is: Shoutcoders

THIS ARTICLE UPDATED EVERY MONTH. BOOKMARK IT FOR LATEST INFORMATION.

Comments