Program to define a class - PP5

Posted on 2005-05-10
Last Modified: 2006-11-18
Defines and tests class CounterType, which is used to count things.
CounterType contains both a default constructor and a constructor that
sets the count to a specified value, plus methods to increment, decrement,
return, and output the count.  The count is always nonnegative.

This is the program:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

class CounterType
  //Initializes the count to 0.

  CounterType(int initCount);
  //Precondition: initCount holds the initial value for the count.
  //  If initCount > 0,initializes the count to initCount.
  //  If initCount <= 0,initializes the count to 0.

  void increment();
  //  The count is one more than it was.

  void decrement();
  //  If the count was positive, it is now one less than it was.
  //  If the count was 0, it is still 0

  int getCount();
  void output(ostream& outStream);
  //Precondition: outStream is ready to write to
  //Postcondition: count has been written to outStream

  int count;


  count = 0;

CounterType::CounterType(int initCount)
  if (initCount >= 0)
    count = initCount;
    count = 0;

void CounterType::increment()

void CounterType::decrement()
  if (count > 0)

int CounterType::getCount()
  return count;

void CounterType::output(ostream& outStream)
  outStream << count;

I dont know what is missing on this program that's why i cant run this program? Can someone help me. Thanks

Question by:cuong5985
    LVL 20

    Accepted Solution

    You need a main() method to be able to run the program
    LVL 10

    Assisted Solution

    by:Eduard Ghergu
    1. Create a .h(pp) file where you'll put the above source code;
    2. Create a .cpp file where you have to have a main function;
    3. Include the .h(pp) file;
    4. Create a variable(an object) having the type CounterType;
    5. Call the methods in the way you want.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now