Question

use an example to declare a generic class at least one generic method

use an example to declare a generic class at least one generic method

Homework Answers

Answer #1

#include <iostream>
using namespace std;

//declaration of generic class

template <class T> class addition
{
public:
//generic function

void add(T a,T b)
{
T ans;
ans=a+b;
cout<<"Addition of "<<a<<" and "<<b<<" is "<<ans<<endl;
}
  
};

int main()
{

//declaring class that receives integer data
addition<int> obj;

//declaring class that receives float data
addition<float> obj2;
obj.add(10,20);//class receives integer data as input and outputs integer value as answer
obj2.add(15.5,10.2);//same class here receives float data as input and output float value as answer
return 0;
}

Know the answer?
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
Similar Questions
java Give an example of a class that contains at least one class variable and at...
java Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
Suppose that you want to write a method that searches for an item in a generic...
Suppose that you want to write a method that searches for an item in a generic DLL and returns the pointer to the target (if search succeeds) or null (if search fails). One strategy to improve the performance of the search algorithm is, whenever you have a successful search, you move the target one step towards the front of the list. You can implement helper methods. Example: 7 <--> 5 <--> 0 <--> 11 Search for 0: SUCCESS! 7 <-->...
Define and discuss the concept of a keystone species. Use at least one example species. Be...
Define and discuss the concept of a keystone species. Use at least one example species. Be sure to discuss the conservation implications of keystone species as they pertain to “trophic cascades.”
4) Class Input Method Definition - C++ Show code for an input method of a Time...
4) Class Input Method Definition - C++ Show code for an input method of a Time class. (A time-of-day has both an hour and a minute, commonly separated by a : symbol.) (Remember that you told me on the ekzam(misspelled on purpose) that input methods should never prompt the user to stay as generic and reusable as possible to be used across multiple applications without complications. Also remember that the data an input method gathers is from the user --...
Declare four string variables. Declare each one separately. Do not initialize the variables. Then write the...
Declare four string variables. Declare each one separately. Do not initialize the variables. Then write the above question so that all variables are declared at once
Part 1 I need to make an example class that I make up on my own....
Part 1 I need to make an example class that I make up on my own. It should have some variables, at least one function and at least one procedure in it. And then I should create two instances of the class and demonstrate how to use the class by calling some of the procedures and functions. What I make my class about is up to me. Part 2 Do a simple example of ByVal vs ByRef. I can do...
Ms. Jonna is told that 30% of students have at least one class that uses a...
Ms. Jonna is told that 30% of students have at least one class that uses a free online textbook (OER),70% of students are concerned about the cost of books, and 17% of students have at least one class that uses a free online book AND are concerned about the cost of books. a) create a contingency table with the above information b) Determine the likehood that a student is concerned about the cost of books given that they have at...
Write a C++ header (.h) file to specify a class GiftCard, with at least 3 meaningful...
Write a C++ header (.h) file to specify a class GiftCard, with at least 3 meaningful private data members and 3 public methods, including two constructors. At least one numeric data member and one calculator method to make this class meaningful / useful.
Complete a medications form with each medication including their generic /brand name , category class, expected...
Complete a medications form with each medication including their generic /brand name , category class, expected Pharmacological Action, Complications, Contraindications/Precautions, Interactions, Medication Administration, Evaluation of Medication Effectiveness, Therapeutic Use, Nursing Interventions, and Client Education. Propofol warfarin Digoxin
Provide at least one example for each of Homans' Propositions.
Provide at least one example for each of Homans' Propositions.