Answered step by step
Verified Expert Solution
Link Copied!

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:
 
  1. Implement the Sequence class. (do not use inline definitions)
  2. Copy the following main and add it to your program, again watch it run.
 
 
int main () 
{
 Sequence  myints;
 Sequence  myfloats;
 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions