Answered step by step
Verified Expert Solution
Question
1 Approved Answer
VisualStudio C++ Make sure it runs and add comments to answer the questions Lab Manual - Templates Objective Implement template functions Implement template classes Implement
VisualStudio C++ Make sure it runs and add comments to answer the questions
Lab Manual - Templates
Objective
- Implement template functions
- Implement template classes
- Implement templates with multiple types
Non-Type Parameters for Templates
Besides the template arguments that are preceded by the class keyword, which represent types, templates can also have regular typed parameters, similar to those found in functions.
To try this out consider the following class template:
template
class Sequence {
T memblock [N];
public:
void setmember (int x, T value);
T getmember (int x);
};
Sequence is a class that stores a Sequence of elements, but here N is an integer. The member function setmember sets the member at position x in the memblock with value and getmember returns the value at index x.
Exercise 1:
You are required to do the following:
- Implement the Sequence class. (do not use inline definitions)
- Copy the following main and add it to your program, again watch it run.
int main ()
{
Sequencemyints;
Sequencemyfloats;
myints.setmember (0,100);
myfloats.setmember (3,3.1416);
cout << myints.getmember(0) << ' ';
cout << myfloats.getmember(3) << ' ';
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started