Answer to Question #218902 in C++ for Nikita

Question #218902

Define a class Pairs with two integer data members, f and s, where f represents

the first value in the ordered pair and s represents the second value in an ordered

pair. Write a program to test all the overloaded operators in your class definition.


Overload the stream extraction operator >> and the stream insertion operator << as

friend functions so that objects of class Pairs are to be input and output in the

form (5,6) (5,-4) (-5,4) or (-5,-6).


Overload binary operator + as a friend function to add pairs according to the rule

(a,b) + (c,d) = (a + c, b + d)


Overload operator – as a friend function in the same way, i.e. according to the rule

(a,b) - (c,d) = (a - c, b - d)


Overload operator * as a friend function on Pairs and int according to the rule

(a,b) * c = (a * c, b * c)

Overload the +, - and * operators for objects of class Pairs, as member functions. Also, define the class Pairs as an ADT that uses separate files for the interface and the implementation.


1
Expert's answer
2021-07-20T02:32:37-0400
#include <iostream>


using namespace std;
//Define and implement class Pairs
class Pairs
{
private:
	int f;//first member
	int s;//second member
public:
	//Constrcutor default
	Pairs()
	{
		this->f = 0;
		this->s = 0;
	}
	//Paramitrazed operator
	Pairs(int a, int b)
	{
		this->f = a;
		this->s = b;
	}
	//define friend function overload operator
	friend ostream& operator<<(ostream& cout, const Pairs &a);
	friend istream& operator>>(istream& cin, Pairs& a);
	friend Pairs operator+(const Pairs& a, const Pairs& b);
	friend Pairs operator-(const Pairs& a, const Pairs& b);
	friend Pairs operator*(const Pairs& a, const Pairs& b);
	//member function
	Pairs operator+(const Pairs &a)
	{
		this->f += a.f;
		this->s += a.s;
		return *this;
	}
	Pairs operator-(const Pairs& a)
	{
		this->f -= a.f;
		this->s -= a.s;
		return *this;
	}
	Pairs operator*(const Pairs& a)
	{
		this->f *= a.f;
		this->s *= a.s;
		return *this;
	}
	//interfeys
	int getFirst()const
	{
		return this->f;
	}
	int getSecond()const
	{
		return this->s;
	}
	int setFirst(int _f)
	{
		this->f = _f;
	}
	int setSecond(int _s)
	{
		this->s = _s;
	}
};
//output Pairs
ostream& operator<<(ostream& cout, const Pairs& a)
{
	cout.flush();
	cout << "(" << a.f << "," << a.s << ")  ";
	return cout;
}
//Input Pairs
istream& operator>>(istream& cin, Pairs& a)
{
	cin.clear();
	cin >> a.f >> a.s;
	return cin;
}
Pairs operator+(const Pairs& a, const Pairs& b)
{
	return Pairs(a.f + b.f, a.s + b.s);
}
Pairs operator-(const Pairs& a, const Pairs& b)
{
	return Pairs(a.f - b.f, a.s - b.s);
}
Pairs operator*(const Pairs& a, const Pairs& b)
{
	return Pairs(a.f * b.f, a.s * b.s);
}


int main()
{
	bool quit = false;
	while (!quit)
	{
		cout << "menu\n";
		cout << "\t1 -operator+\n";
		cout << "\t2 -operator-\n";
		cout << "\t3 -operator*\n";
		cout << "\t4 -exit\n";
		int cmd;
		cin >> cmd;
		switch (cmd)
		{
		case 1:
		{
			cout << "Enter member Pairs a: ";
			Pairs a, b;
			cin >> a;
			cout << "Enter member Pairs b: ";
			cin >> b;
			Pairs c;
			cout << a << "+" << b;
			c = a + b;
			cout<< c << endl;
			break;
		}
		case 2:
		{
			cout << "Enter member Pairs a: ";
			Pairs a, b;
			cin >> a;
			cout << "Enter member Pairs b: ";
			cin >> b;
			cout << a << "-" << b;
			Pairs c;
			c = a - b;
		    cout<< c << endl;
			break;
		}
		case 3:
		{
			cout << "Enter member Pairs a: ";
			Pairs a, b;
			cin >> a;
			cout << "Enter member Pairs b: ";
			cin >> b;
			cout << a << "*" << b;
			Pairs c;
			c = a * b;
			cout<< c << endl;
			break;
		}
		case 4:
		{
			cout << "Good Bya\n";
			quit = 1;
			break;
		}
		default:
			break;
		}
	}
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS