Answer to Question #193415 in C++ for Mr.kettavan

Question #193415
Design a class template with overloaded operator + to perform a = b + c.
1
Expert's answer
2021-05-15T17:53:47-0400
#include <iostream>
using namespace std;
 
template <class T>
class MyClass
{
public:
	MyClass(T val) { value = val; }
	T operator+ (const MyClass& RHS)
	{
		MyClass temp(this->GetValue() + RHS.value);
		return temp.GetValue();
	}
	T GetValue() { return value; }
private:
	T value;
};
 
int main()
{
	MyClass<double> f1(2.25);
	MyClass<double> f2(1.5);
	MyClass<double> f3 = f1 + f2;
	cout << "Double: " << f3.GetValue() << endl;
 
	MyClass<int> i1(6);
	MyClass<int> i2(3);
	MyClass<int> i3 = i1 + i2;
	cout << "Int: " << i3.GetValue() << endl;
	system("pause");
	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

Assignment Expert
17.05.21, 06:55

Dear KAVIN MUKILAN please post a new task

KAVIN MUKILAN
17.05.21, 06:09

Design a class template with overloaded operator / to perform a = b / c.

Leave a comment

LATEST TUTORIALS
New on Blog