注册 登录
编程论坛 C++教室

函数的缺省参数实例

十八太保 发布于 2011-07-28 23:13, 320 次点击
#include <iostream>
using namespace std;
class A
{
public:
    void set(int=30,int=15);
    void count(bool=false);
private:
    int w;
    int h;
};
int main()
{
  A a;
  a.set();
  a.count();
  a.set(100,200);
  a.count(true);

}
void A::set(int width,int height)
{
    w=width;
    h=height;
}
void A::count(bool val)
{
    if(val==true)
    {
        cout<<"val的值为真时:"<<w*h<<endl;   
    }
    else
    {
        cout<<"val的值为假时:"<<w*h/2<<endl;   
    }
}
0 回复
1