Question

In: Computer Science

c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...

c++

Write the implementation (.cpp file) of the Counter class of the previous exercise.
The full specification of the class is:

  • A data member counter of type int.
  • An data member named counterID of type int.
  • A static int data member named nCounters which is initialized to 0.
  • A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID.
  • A function called increment that accepts no parameters and returns no value. increment adds one to the instance variable counter.
  • A function called decrement that accepts no parameters and returns no value. decrement subtracts one from the counter.
  • A function called getValue that accepts no parameters and returns an int. It returns the value of the instance variable counter.
  • A function named getCounterID that accepts no parameters and returns an int. getCounterID returns the value of the data member counterID.

The previous exercise:

Write the header file (.h file) of a class Counter containing:

  • A data member counter of type int.
  • A data member named counterID of type int.
  • A static int data member named nCounters.
  • A constructor that takes an int argument.
  • A function called increment that accepts no parameters and returns no value.
  • A function called decrement that accepts no parameters and returns no value.
  • A function called getValue that accepts no parameters and returns an int.
  • A function named getCounterID that accepts no parameters and returns an int.

Solutions

Expert Solution

C++ Program:

File: Counter.cpp

#include "Counter.h"

//Static field initialization
int Counter::nCounters = 0;

//Constructor
Counter::Counter(int val)
{
counter = val;
nCounters = nCounters + 1;
counterID = nCounters;
}

//Increment
void Counter::increment()
{
counter = counter + 1;
}

//decrement
void Counter::decrement()
{
counter = counter - 1;
}

//Returns the counter value
int Counter::getValue()
{
return counter;
}

//Returns the counterId value
int Counter::getCounterID()
{
return counterID;
}

File: Counter.h

#ifndef COUNTER_H_INCLUDED
#define COUNTER_H_INCLUDED

class Counter
{
private:
int counter;
int counterID;
static int nCounters;

public:
Counter(int);//Constructor
void increment();
void decrement();
int getValue();
int getCounterID();
};

#endif // COUNTER_H_INCLUDED

File: main.cpp

#include <iostream>
#include "Counter.h"

using namespace std;

int main()
{
//Counter object
Counter cObj(10), cObj1(50);

cout << "\nCounter Id: " << cObj.getCounterID();
cout << "\nCounter Value: " << cObj.getValue();

cout << "\nIncrementing counter five times\n";
//Increment
cObj.increment();
cObj.increment();
cObj.increment();
cObj.increment();
cObj.increment();

cout << "\nDecrementing counter\n";
//Decrement
cObj.decrement();
cout << "\nCounter Value: " << cObj.getValue();

cout << "\n\n\nCounter Id: " << cObj1.getCounterID();
cout << "\nCounter Value: " << cObj1.getValue();

cout << "\n\n";

return 0;
}
__________________________________________________________________________________________

Sample Run:


Related Solutions

Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
Write a class that extends the LeggedMammal class from the previous laboratory exercise.
C++ code on Visual Studio Code:Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators....
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think of the personType as the base class. Lab10.cpp is provided below: #include <iostream> #include <string> using namespace std; // Base class personType class personType { public: void print()const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); string getFirstName()const; string getLastName()const; personType(string first = "", string last = ""); //Constructor //Sets firstName and lastName...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you ever wonder how an object gets instantiated (created)? What really happens when you coded Box b1; or Date d1; or Coord c1; I know you have lost sleep over this. So here is the answer………. When an object is created, a constructor is run that creates the data items, gets a copy of the methods, and may or may not initialize the data items....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT