我的第一个关于类的c++程序
#include"iostream"
using namespace std;
class CounterType
{
private:
int count;
public:
CounterType();
CounterType(int count_1);
int back();
void increase();
void decrease();
void output(ostream& outs);
};
CounterType::CounterType():count(0)
{
}
CounterType::CounterType(int count_1)
{
if(count_1<0)
{
cout<<"Illegal values for count"<<endl;
exit(1);
}
count=count_1;
}
int CounterType::back()
{
return count;
}
void CounterType::increase()
{
count++;
}
void CounterType::decrease()
{
if(count<=0)
{
cout<<"Illegal values for count"<<endl;
exit(1);
}
count--;
}
void CounterType::output(ostream& outs)
{
if(count<0)
{
cout<<"Illegal values for count"<<endl;
exit(1);
}
outs<<"Count is "<<count<<endl;
}
int main()
{
int n,m;
cout<<"Please enter the number you want the count take: ";
cin>>n;
CounterType count_1(n),count_2;
count_1.decrease();
m=count_1.back();
cout<<"Count is: "<<m<<endl;
count_1.output(cout);
cout<<"--------------------------------------------------------------------"<<endl;
count_2.increase();
m=count_2.back();
if(m<0)
{
cout<<"Illegal values for count"<<endl;
exit(1);
}
cout<<"Count is:"<<m<<endl;
count_2.output(cout);
return 0;
}