Question

C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define...

C++ program

please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it.

Define a class counterType to implement a counter. Your class must have a private data member counter of type int. Define a constructor that accepts a parameter of type int and initializes the counter data member. Add functions to:

  • Set counter to the integer value specified by the user.
  • Initialize counter to 0.
  • Return the value of counter with a function named getCounter.
  • Increment and decrement counter by one.
  • Print the value of counter using the print function.
    • Example output: Counter = 0.

The value of counter must be nonnegative.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// CounterType.h

#ifndef COUNTERTYPE_H
#define COUNTERTYPE_H
class CounterType
{
public:
CounterType();
void setCount(int counter);
int getCount();
void increase();
void decrease();
void print();
private:
// Declaring variables
int counter;
};
#endif

============================

// counterTypeImp.cpp


#include <iostream>
using namespace std;
#include "CounterType.h"
CounterType::CounterType()
{
this->counter=0;
}
void CounterType::setCount(int count)
{
this->counter = count;
}
int CounterType::getCount()
{
return counter;
}
void CounterType::increase()
{
counter++;
}
void CounterType::decrease()
{
counter--;
}
void CounterType::print()
{
cout << "Counter :" << counter << endl;

}

===============================

// main.cpp

#include <iostream>
using namespace std;
#include "CounterType.h"
int main()
{
int counter;
CounterType ct;
while(true)
{
cout << "Enter the Count value :";
cin >> counter;
if(counter<0)
{
cout<<"** Invalid.Must be positive **"
}
else
break;
}

ct.setCount(counter);
cout << "--- Calling increase() function 5 times ---" << endl;
for (int i = 1; i <= 5; i++)
{
ct.increase();
}

cout << "The Counter = " << ct.getCount() << endl;
cout << "--- Calling decrease() function 7 times ---" << endl;
for (int i = 1; i <= 7; i++)
{
ct.decrease();
}
ct.print();
return 0;
}

==============================

Output:

==========================Thank You

Add a comment
Know the answer?
Add Answer to:
C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the implementation (.cpp file) // i cant figure out what it wants where the is a ? the...

    Write the interface (.h file) of a class Accumulator containing:A data member named sum of type integer.A constructor accepting an integer parameter.A function named getSum that accepts no parameters and returns an integer.A function named add that accepts an integer parameter and returns no value.class Accumulator{int sum;Accumulator(int);int getSum ();void add (int);};Write the implementation (.cpp file) of the Accumulator class of the previous exercise. The full specification of the class is:An data member named sum of type integer.A constructor that accepts...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

  • Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification...

    Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is: A data member named amount of type  double . An data member named capacity of type  double . A constructor that accepts a parameter of type  double . The value of the parameter is used to initialize the value of capacity. The constructor also sets amount to zero. A function named addGas that accepts a parameter of type  double . The value of the...

  • This is the code I wrote for myprogramminglab It works for 8 out of 9 inputs they use and I...

    Write a full class definition for a class named GasTank , and containing the following members:A data member named amount of type double.A constructor that no parameters. The constructor initializes the data member amount to 0.A function named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter.A function named useGas that accepts a parameter of type double . The value of the amount data member...

  • C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...

    C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :- -You may need getters and setters also. 1. Declare and define an animal base class: animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double). animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set...

  • Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and...

    Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and a driver program called invoiceDriver.cpp. The class Invoice is used in a hardware store to represent an invoice for an item sold at the store. An invoice class should include the following: A part number of type string A part description of type string A quantity of the item being purchased of type int A price per item of type int A class constructor...

  • Code the following Program in C++ Define a class named Payment that contains a member variable...

    Code the following Program in C++ Define a class named Payment that contains a member variable of type float that stores the amount of the payment and appropriate accessor and mutator methods.    Also create a member function named paymentDetails that outputs an English sentence that describes the amount of the payment. Next define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails function to indicate that the payment is in cash. Include appropriate constructor(s)....

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT