Question
I need to convert the class Counter into a template. // File: main.cpp #include Counter.h #include using namespace std; int main() { Counter intCounter; Counter
I need to convert the class Counter into a template.
// File: main.cpp
#include "Counter.h" #include
using namespace std;
int main() { Counter
cout << "Integer counter:" << endl; for (int count = 1; count <= 5; count++) { cout << intCounter << endl; intCounter.increment(); }
cout << endl << "Double counter:" << endl; for (int count = 1; count <= 5; count++) { cout << doubleCounter << endl; doubleCounter.increment(); }
return 0; }// end main()
/ File: Counter.h
#ifndef COUNTER_H #define COUNTER_H
#include
using namespace std;
/* TODO (1): * * Convert the class to a template class. * * Define all method definitions INSIDE the class, i.e. * make each an inline definition; that is remove the ; and * add a body {} to each one. */ template
public: Counter(const E& initialValue = 1, const E& incrementValue = 1) : value(initialValue), increment(incrementValue) {}
virtual E increment() { return value + increment; } virtual E decrement() { return value - increment; }
/* Return a string formatted as: * Counter(
#endif
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