codecamp

我的第一个关于类的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;
}

A c++ program
another c++ program
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }