Hi all,
This program works well. The the object of data type of class used to count something.
However, the compiler generated the error when I broke this program into 3 parts:
- The interface file, Problem5.h, contains the class definition.
- The implementation file, Problem5.cpp, contains the class's member functions, #include "Problem5.h" was written.
- The application file used to test above class, #include "Problem5.h" was written.
* When I compiled them, it was fine. However, when I ran them, it seemed to get trouble with the linker.
The weird thing is, the program works perfectly when I broke the program into 2 parts, the file contains the definiton of class and its member functions' definition, called Problem5.h; and the file contains the main() function.
Everyone can help me out please. Here is the code:
Code:
#include <iostream>
using namespace std;
class CounterType
{
public:
CounterType();//Default Constructor
CounterType(int number); //Constructor
void increase();
void decrease();
int get();
void output(ostream& numberout);
private:
int count;
void check();
};
// <---- interface file
int main()
{
CounterType myclass;
CounterType fortest(100);
int countvalue,i;
for (i=0;i<10;i++)
{
myclass.increase();
if ((i==3)||(i==7))
myclass.decrease();
}
countvalue=fortest.get();
cout<<"The first object that counts from 1-->10, except 3&7 :";
myclass.output(cout);
cout<<endl<<endl;
cout<<"The second object that is initilized with 100: "<<countvalue<<endl<<endl;
return 0;
}
//<---- application (drive) file
CounterType::CounterType() : count(0)
{
}
CounterType::CounterType(int number)
{
count=number;
check();
}
void CounterType::check()
{
if (count<0)
{
cout<<"Count contains negative number. No negative number is allowed!\n\n";
exit(1);
}
}
void CounterType::increase()
{
count++;
check();
}
void CounterType::decrease()
{
if (count > 0)
count--;
check();
}
int CounterType::get()
{
return count;
}
void CounterType::output(ostream& numberout)
{
numberout << count;
}
//<----- implementation file