Question #47608

Write a class ‘Counter’ which has a data member currentVal which hold the current value of the counter. Add a constructor which takes the intial value of the counter. Then add increment/decrement functions to this class which increment/decrement the value of the currentVal. Add a printValue function which prints the current value.
Then write a main function which will create a object of the Counter class with 5 as the initial value. Then call increment and decrement functions and print the values after each call.
1

Expert's answer

2015-01-19T03:45:27-0500

Answer on Question# 47608- <engineering> - <other>

Program:


#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
class Counter
{
private:
int currentVal;
public:
Counter(int val=0) {currentVal=val;}
void Increment(){currentVal++;}
void Decrement(){currentVal--;}
void printValue(){cout<<"Value="<<currentval<<endl;}
};
int main()
{
Counter myCounter(5);
cout<<"After constructor (5): ";
myCounter.printValue();
myCounter.Increment();
cout<<"After increment: "; myCounter.printValue();
myCounter.Decrement();
cout<<"After decrement: "; myCounter.printValue();
getch();
return 1;
}


Result of executing of program:


After constructor <5>: Value=5
After increment: Value=6
After decrement: Value=5


https://www.assignmentexpert.com/</currentval<<endl></string></conio.h></iostream>


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS