Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 intCounter; Counter doubleCounter(1.0, 0.1);

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 #include #include #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 class Counter { private: E value; E increment;

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(, increment: ) * * Do not terminate string with a * \ * The value and increment should be formatted as n.d, * i.e. with one digit of precision. */ virtual string toString() const { ostringstream oss; oss << "Counter(" << value << ", increment: " << increment << ")"; return oss.str(); } }; template ostream& operator <<(ostream& os, const Counter& o) { os << o.toString(); return os; }

#endif

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

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions

Question

List the major types of bonds. What is a mortgage-backed security?

Answered: 1 week ago

Question

2. Do the same for your favorite female film character.

Answered: 1 week ago

Question

9. Explain the relationship between identity and communication.

Answered: 1 week ago

Question

a. How do you think these stereotypes developed?

Answered: 1 week ago

Question

a. How many different groups were represented?

Answered: 1 week ago