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.
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a numeric object (Integer, Double or any class that extends java.lang.number) Add a method standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation as type double. Use a for each loop where appropriate. Hard code a couple of test arrays into your Demo file. You must use at least 2 different types such as Double and Integer. Your call will...
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 --...
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...
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
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.
Provide at least one example for each of Homans' Propositions.
Provide at least one example for each of Homans' Propositions.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT