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.
What is exception propagation? Give an example of a class that contains at least two methods,...
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
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...
Create a generic class with a type parameter that simulates drawing an item at random out...
Create a generic class with a type parameter that simulates drawing an item at random out of a box. For example, the box might contain Strings representing names written on slips of paper, or the box might contain Integers representing the possible numbers for a lottery draw. Include the following methods: add() – that allows the user to add an object of the specified type to the box isEmpty() – to determine if the box is empty drawItem() – to...
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 <-->...
1. How does one declare a class variable in C#? 2. Write a C# script for...
1. How does one declare a class variable in C#? 2. Write a C# script for a game object to call the function foo(), every 10 seconds after the game starts. 3. Write a c# fragement to change an object's texture, when the is store in the variable objectvariable and the texture is stored in the variable mytexture.
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.”
how to create a class called Cheetah that: inherits from the Animal class. makes use of...
how to create a class called Cheetah that: inherits from the Animal class. makes use of at least one static field which needs to have a static setter and getter. Contains a toString ()method. Has an array as one of it's field. .Create an application class, and within it create a Cheetah object and print it out with the main method
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT