第十二周项目一(4)

来源:互联网 发布:马克思主义史学 知乎 编辑:程序博客网 时间:2024/05/03 12:29

代码

#include <iostream>using namespace std;const double pi=3.1415926;float area(float r=6.5);//指定r的默认值为6.5float volume(float h,float r=6.5); //指定r的默认值为6.5int main( ){    cout<<area()<<endl; //相当于area(6.5);    cout<<area(7.5)<<endl; //形参得到的值为7.5,而不是6.5    cout<<volume(45.6)<<endl; //相当于volume(45.6,6.5)    cout<<volume(34.2,10.4)<<endl; //h的值为34.2,r的值为10.4    return 0;}float area(float r){    return pi*r*r;}float volume(float h,float r){    return pi*r*r*h;}


总结:去掉4 行“=6.5”。我的原因是8行调用时出错,r无输入值。

            \main.cpp|8|error: too few arguments to function 'float area(float)'|
            \main.cpp|4|note: declared here|
           ||=== Build finished: 1 errors, 0 warnings (0 minutes, 2 seconds) ===|

          14行'|改为r=6,5,我以为错在不能赋值。

           \main.cpp|4|error: expected ',' or '...' before numeric constant|
            \main.cpp||In function 'int main()':|
              \main.cpp|8|error: too few arguments to function 'float area(float)'|
                \main.cpp|4|note: declared here|
                  ||=== Build finished: 2 errors, 0 warnings (0 minutes, 1 seconds) ===|

        将第5行改为“float h=1,float r”,我以为10行会出错,r无值。

        \main.cpp|4|error: expected ',' or '...' before numeric constant|
         \main.cpp|5|error: default argument missing for parameter 2 of 'float volume(float, float)'|
          \main.cpp||In function 'int main()':|
           \main.cpp|8|error: too few arguments to function 'float area(float)'|
            \main.cpp|4|note: declared here|
              ||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|

       将第5行改为“float volume(float h=0,float r=6.5)”,不会有影响

     

总结:通过这个练习,对默认参数的用法及作用有了更深刻的认识

0 0