?
Solved

Program to define a class - PP5

Posted on 2005-05-10
4
Medium Priority
?
308 Views
Last Modified: 2006-11-18
Topic:
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
{
public:
  CounterType();
  //Initializes the count to 0.

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

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

  void decrement();
  //Postcondition:
  //  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

private:
  int count;

};



CounterType::CounterType()
{
  count = 0;
}

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

void CounterType::increment()
{
  count++;
}

void CounterType::decrement()
{
  if (count > 0)
    count--;
}

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

0
Comment
Question by:cuong5985
2 Comments
 
LVL 20

Accepted Solution

by:
gatorvip earned 100 total points
ID: 13971333
You need a main() method to be able to run the program
0
 
LVL 10

Assisted Solution

by:Eduard Ghergu
Eduard Ghergu earned 100 total points
ID: 13975239
Hello!
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

840 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