6.1 默认函数参数

Posted: March 25, 2011 in C++

/*函数参数可以预先设定一个默认值, 如果返回时无返回值, 将会使用默认参数, 否则仍旧使用返回值*/

#include <iostream>
int Area(int len,int hig=20,int wid=50);

using namespace std;

void main()
{
               int area, length=3, hight=2, width=5;

               area=Area(length,hight,width);
               cout<<“Area is “<<area<<endl<<endl;

               area=Area(length,hight);//使用默认参数50
               cout<<“Area is “<<area<<endl<<endl;

               area=Area(length);//使用默认参数20和50
               cout<<“Area is “<<area<<endl<<endl;
}

int Area(int len,int hig,int wid)
{
               return (len*hig*wid);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s