Question

c++

Q.2. a) Create a C++ class template called MathCalc, to perform arithmetic operations between two numbers. These arithmetic o

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi There,

I have implemented the template class and respective functions.

I have assumed that class T and class U specify the datatypes of input variables of each function and type U is also the return type of each function.

Please comment below if you need more information or the code needs some changes.

Output:

Code:


#include <iostream>
using namespace std;
// type T and type U represent the datatype of both the variables.
// type U also represents the return type of the operation.
template <class T, class U>
class MathCalc
{
public:
U Sum(T augend, U addend)
{
return (U)(augend + addend); // sum = augend + addend
}

U Subtract(T subtrahend, U minuend)
{
return (U)(minuend - subtrahend);// diffrence = minuend - subtrahend
}

U Multiply(T multiplicand, U multiplier)
{
return (U)(multiplier * multiplicand);// product = multiplier * multiplicand
}

U Divide(T dividend, U divisor)
{
return (U)(dividend / divisor); // quotient = dividend / divisor
}
};

int main()
{
MathCalc<float, float> calc;
cout << "Enter two numbers of type float: ";
float f1, f2;

cin >> f1 >> f2;
try
{
cout << "Sum: " << calc.Sum(f1, f2) << endl;
cout << "Subtract: " << calc.Subtract(f1, f2) << endl;
cout << "Multiply: " << calc.Multiply(f1, f2) << endl;
cout << "Divide: " << calc.Divide(f1, f2) << endl;
}
catch (exception e)
{
cout << "Exception occured " << e.what() << endl;
}
  
int i1;
float f3;
cout << endl;
cout << "Enter two numbers of type integer and float: ";
MathCalc<int, float> calc2;
cin >> i1 >> f3;
try
{
cout << "Sum: " << calc2.Sum(i1, f3) << endl;
cout << "Subtract: " << calc2.Subtract(i1, f3) << endl;
cout << "Multiply: " << calc2.Multiply(i1, f3) << endl;
cout << "Divide: " << calc2.Divide(i1, f3) << endl;
}
catch (exception e)
{
cout << "Exception occured " << e.what() << endl;
}
  

return 0;
}

Add a comment
Know the answer?
Add Answer to:
c++ Q.2. a) Create a C++ class template called MathCalc, to perform arithmetic operations between two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q.1. Create a C++ class template called ArrayCalc, to perform a series of mathematical operations on...

    Q.1. Create a C++ class template called ArrayCalc, to perform a series of mathematical operations on an array. These mathematical operations are calculating the minimum, maximum, sum and average values of the array. The ArrayCalc class template accepts one type parameter, which can be defined as: template <class T>. This allows ArrayCalc to carry out operations for different array data types. ArrayCalc has the following public methods, described here in pseudo code: i. void FindMin(inArrayData[], inArraySize, SoutMinVal); (This function accepts...

  • 1.Write a Python program to perform simple arithmetic operations (add, subtract, multiply and divide) on 2...

    1.Write a Python program to perform simple arithmetic operations (add, subtract, multiply and divide) on 2 numbers and display the output on console     ( use methods in your program) Range of numbers : 1 to 100 (Do not use 0)

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • Using C#, create a class in a file MathOP.cs. MathOP.cs needs to include the following methods MathAdd: to add two numb...

    Using C#, create a class in a file MathOP.cs. MathOP.cs needs to include the following methods MathAdd: to add two numbers MathSub: to subtract two numbers Next, create a new class MathOP2 (MathOP2.cs) that Inherits from MathOP.cs. MathOP2.cs needs to include the following methods MathMult: to multiply two numbers MathDiv: to divide two numbers Allinone: accepts two parameters and set 4 class variables: v_add, v_subtract, v_multiply, and v_divide and show how these values are retrieved. Finally, create a program TestMathOP.cs...

  • Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method...

    Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method and divide method, both methods accepts two parameters and return a value. Create a test program with documentation to test all operators (at least 4) The TestMathOP should do the following      Enter the First number >> 5.5      Enter the Second Number >> 7.5      The sum of the numbers is 13      The subtract of the two numbers is -2.00      Do...

  • C++ visual studio program...Write your own version of a class template that will create a dynamic...

    C++ visual studio program...Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • Write another program called calculator that inherits from class average and also has methods that has:...

    Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial”...

  • Write another program called calculator that inherits from class average and also has methods that has:...

    Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial”...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT