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...

  • Write a C# console application that has one method to perform the four basic arithmetic operations...

    Write a C# console application that has one method to perform the four basic arithmetic operations of add, subtract, multiply, and divide. In the main program, first print a line on the console showing your name. Call the method you created above with each of the four arithmetic operations. You have to pass the method two integer numbers as well as a flag for which operation you want the method to perform. The method performs the operation on the numbers...

  • (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...

  • 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...

  • 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...

  • 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...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • 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...

  • help with C++ code The aim of this is to practice writing template classes. You must...

    help with C++ code The aim of this is to practice writing template classes. You must use SafeArray template class and implement a SafeMatrix template class that will allow you to work with two dimensional arrays of any type. The boundaries are checked. You must be able to create instances of SafeMatrix template class like, SafeMatrix<int> m(2,3); The above statement will create a 2 by 3 dimensional array of integers. You must be able to access and set values using...

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

    Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...

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