Question

Create a template function printResult which prints a value of type T void printResult(T val); C++...

Create a template function printResult which prints a value of type T
void printResult(T val);
C++

Write a program to create two Mathematic type (template below) objects a(10, 5) and b(3.4, 5.5) which prints using the template function printResult the result of

a.addition(), a.subtraction(), a.multiplciation(), a.division()

b.addition(), b.subtraction(), b.multiplication(), b.division()

(Template)

template <typename T>

class Mathematics{

private:

T val1, val2;

public:

Mathematics(T v1, T v2)

{

val1 = v1;

val2 = v2;

}

T addition()

{

return val1 + val2;

}

T subtraction()

{

return val1 - val2;

}

T multiplication()

{

return val1*val2;

}

T division()

{

return val1/val2;

}

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

The required code is slightly modified as per the requirements of the question in case of any doubts you can ask me in comments

Main.cpp

#include <iostream>

using namespace std;
template<class T>
class calc
{
T a;
T b;
public:
calc(T x,T y)
{
a = x;
b = y;
}
T add();
T subtraction();
T multiply();
T division();
void printResult(T val);
};
template<class T>
T calc<T>::add()
{
return (a+b);
}
template<class T>
T calc<T>::subtraction()
{
return (a-b);
}
template<class T>
T calc<T>::multiply()
{
return (a*b);

}
template<class T>
T calc<T>::division()
{
  
return (a/b);

}

template<class T>
void calc<T>::printResult(T val)
{
cout <<val<<endl;
}

int main()
{
calc<int>a(10,5);
calc<double>b(3.4,5.5);

// a
cout<<"Addition : ";a.printResult(a.add());
cout<<"Subtraction : ";a.printResult(a.subtraction());
cout<<"Multiplication : ";a.printResult(a.multiply());
cout<<"Division : ";a.printResult(a.division());

cout<<endl;
// b
cout<<"Addition : ";b.printResult(b.add());
cout<<"Subtraction : ";b.printResult(b.subtraction());
cout<<"Multiplication : ";b.printResult(b.multiply());
cout<<"Division : ";b.printResult(b.division());
  
return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Create a template function printResult which prints a value of type T void printResult(T val); C++...
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
  • c++ Q.2. a) Create a C++ class template called MathCalc, to perform arithmetic operations between two...

    c++ Q.2. a) Create a C++ class template called MathCalc, to perform arithmetic operations between two numbers. These arithmetic operations are addition, subtraction, multiplication and division. The MathCalc class template accepts two type parameters, which can be defined as: template <class T, class U>. This allows MathCalc to carry out arithmetic operations between two different data types. MathCalc has the following public methods, described here in pseudocode: i. Ret Sum(augend, addend); Ret is the return value of the method. ii....

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • c++ language 1) Create a function that prints a greeting when called with a name such...

    c++ language 1) Create a function that prints a greeting when called with a name such as greeting ("Elona") 2) Create a function that squares a double number and returns the result as a double. 3) Create a function that takes 5 int numbers and returns the average as a float 4) Create a single function that gives a greeting, calculates the average of 5 grades but does not return a value. Hint: Use return type void and a string...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

    PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

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

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

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