The objective of this assignment is to learn how to define a class with data and
ID: 3689538 • Letter: T
Question
The objective of this assignment is to learn how to define a class with data and methods, along with multiple constructors. Define a class called Counter. An object of this class is used to count things. The class can be used to either count nonnegative whole numbers or nonnegative floating point numbers. The class should include two instance variables; one long and one double.The class should include a three Constructors; a default Constructor with no parameters which initializes both variables to 0, and one Constructor each for initializing each instance variable. The class should include two overloaded methods; add (2) and subtract (2). Each overloaded method is responsible for the count of each variable. Be sure that no method allows the value of the counter to become negative.The class should include a toString() method, which can be used to display the two instance variables.The class should include a static method reset(). which will set each of the two variables to 0. Write a program to test your class definition. Each method in the class should contain documentation as to its function. Your assignment will be graded based upon all tasks being performed and handed in with all required documentation. two instance variables constructors [x3] add methods [x2] subtract methods [x2] toString method static reset method Proper compilation and source code documentation of all classes and methodsExplanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
class Counter{
public:
long num_long;
double num_double;
Counter(){
num_long=0;
num_double=0;
}
Counter(long num){
num_long=num;
}
Counter(double num){
num_double=num;
}
void add(long num){
num_long += num;
}
void add(double num){
num_double += num;
}
void subtract(long num){
num_long -= num;
}
void subtract(double num){
num_double -= num;
}
long getLong(){
return num_long;
}
double getDouble(){
return num_double;
}
void tostring(Counter& v){
cout<<v.getLong();
cout<<v.getDouble();
}
void reset(){
num_long=0;
num_double=0;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.