参数的缺省值

参数的缺省值

有一些参数的值在每次函数调用时都相同,书写这样的语句会使人厌烦。C++语言采 用参数的缺省值使书写变得简洁(在编译时,缺省值由编译器自动插入)。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6         //定义结构类型
 7     struct human {
 8        char name[10];
 9        int sex;
10        int age;
11        };
12 
13     //声明结构变量和结构指针,并初始化
14     struct human x={"WangPing",1,30},*p=&x;
15  
16     //利用结构指针显示结构中的数据
17     cout<<"(*p).name="<<(*p).name<<endl;
18     cout<<"(*p).sex="<<(*p).sex<<endl;
19     cout<<"(*p).age="<<(*p).age<<endl;
20     cout<<"-------------------------"<<endl;
21 
22     //利用new运算符为p分配内存
23     p=new human;
24 
25     //从键盘上为p指向的结构对象赋值
26     cout<<"p->name=";
27     cin>>p->name;
28     cout<<"p->sex=";
29     cin>>p->sex;
30     cout<<"p->age=";
31     cin>>p->age;
32     cout<<"-------------------------"<<endl;
33 
34     //显示p所指结构对象的值
35     cout<<"p->name="<<p->name<<endl;
36     cout<<"p->sex="<<p->sex<<endl;
37     cout<<"p->age="<<p->age<<endl;
38     cout<<"-------------------------"<<endl;
39 
40     //显示结构变量的值
41     cout<<"x.name="<<x.name<<endl;
42     cout<<"x.sex="<<x.sex<<endl;
43     cout<<"x.age="<<x.age<<endl;
44 
45     //释放p指向的内存
46     delete p;  
47     return 0;
48 }

 

posted @ 2018-08-02 12:49  borter  阅读(665)  评论(0编辑  收藏  举报