• C++对C的改进(2)


    本文地址:http://www.cnblogs.com/archimedes/p/cpp-change2.html,转载请注明源地址

    区别一:原型声明的区别

    原型声明的概念:
    函数要先定义再使用,如果先使用后定义则必须使用原型声明

    #include <iostream>
    using namespace std;
    int main()
    {
       float add(float x,float y);//原型声明
       float a,b,c;
       cout << "Please enter a,b:";
       cin >> a >> b;
       c = add(a, b);
       cout << "sum=“ << c << endl;
       return 0;
    }
    float add(float x,float y)
    {
       float z;
       z = x+y;
       return z;
    }

    注意:

    ①声明语句必须加分号!

    ②位置任意,只是作用域不同

    ③声明的原因就是告诉编译环境函数参数的个数,类型和顺序

    ④C和C++中,任何类型的函数先使用后定义都需原型声明!

    区别:原型为空的含义不同

    void  fun();
    void  fun(void);

    C++:认为两种形式都无参

    C:认为第一个可能有多个参数第二个无参

    区别二:局部变量定义的位置

    区别三:域解析::扩大全局变量的可见范围

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    int sum = 5050;
    int main()
    {
        int arr[3], i;
        cout<<"input 3 num:"<<endl;
        for(i=0; i<3; i++)
            cin>>arr[i];
        int sum = 0;
        for(i=0; i<3; i++)
            sum += arr[i];
        for(i=0; i<3; i++)
            cout<<setw(4)<<arr[i]<<endl;
         cout << "局部sum=" << sum      << endl;
        ::sum += sum;
        cout << "全局sum=";
        cout << ::sum <<endl;
        system("PAUSE");
        return 0;
    }

    区别四:带默认参数的函数

    含义:形参有初值的函数

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    void fun(int i, int j = 5, int k = 10);
    int main()
    {
        fun(20);
        fun(20, 30);
        fun(20, 30, 40);
        system("PAUSE");
        return 0;
    }
    
    void fun(int i, int j, int k)
    {
        cout<<"i="<<i
            <<" j="<<j
            <<" k="<<k
            <<endl;
    }

    注意:

    1、有默认参数值的参数必须在参数表的最右端;
    int f(int a,int b=0,int c); //×

    2、必须在函数调用前将默认值通知编译系统;

    3、声明和定义同时给出默认值,有些编译器报错,有些不会。最好只在函数声明时给出默认值;

    4、有默认值的形参,调用时给出了实参,则实参值优先

    #include <iostream>
    using namespace std;
    int max(int a, int b, int c=6);
    int main()
    {
        int result = 0;
        result = max(3,4,5);
        cout<<result<<endl;
        system("PAUSE");
        return 0;
    }
    int max(int a, int b, int c)
    {
        int t;
        t = a > b ? a : b;
        c = c > t ? c : t;
        return c;
    }

    区别五:内联函数

    1、调用方式

    2、定义方法:在函数最左端加inline

    #include <iostream>
    using namespace std;
    inline int max(int a, int b, int c);
    int main()
    {
        int i=10,j=20,k=30,m;
        m=max(i,j,k);
        cout<<"max="<<m<<endl;
        system("PAUSE");
        return 0;
    }
    int max(int a, int b, int c)
    {
        int t;
        t = a > b ? a : b;
        c = c > t ? c : t;
        return c;
    }

    注意:

    可在定义和声明函数时同时写inline,也可在一处写inline

    只将规模很小且使用频繁的函数定义成内联函数

    内联函数中不能包含复杂的控制语句

    对函数作inline声明是建议性的,并非一经指定为inline就一定当内联函数处理

    总之规模小,使用频繁的函数适合作为inline

    类内定义的成员函数都将理解为inline,前面无需加inline

    类内声明,类外定义的函数默认并非inline

    区别五:函数重载

    重载的前提:发生在同一个作用域中的才是重载
    因为C++中函数中局部声明的名字将屏蔽在全局作用域内声明的名字!

    #include <iostream>
    using namespace std;
    
    int square(int x)
    {
        return x*x;
    }
    float square(float x)
    {
        return x*x;
    }
    double square(double x=1.5)
    {
        return x*x;
    }
    int main()
    {
        cout<<"square()"
            <<square()<<endl;
        cout<<"square(10)"
            <<square(10)<<endl;
        cout<<"square(2.5f)"
            <<square(2.5f)<<endl;
        cout<<"square(1.1)"
            <<square(1.1)<<endl;
        system("PAUSE");
        return 0;
    }

    注意:

    ①重载函数的参数个数,参数类型,参数顺序3者中必须至少有一种不同,返回值不同不作为重载依据

    ②重载函数的功能应该相近

    ③main()函数不能重载

    ④参数类型最好保证一致,不一致会自动转换但转换不成功会报错

    ⑤重载与有默认值的函数一起用,可能产生二义性
    #include <iostream>
    using namespace std;
    
    int fun(int x, int y=10)
    {
        return x*y;
    }
    int fun(int x)
    {
        return x;
    }
    int main()
    {
        fun(5);//error:对重载函数的调用不明确
        
        system("PAUSE");
        return 0;
    }
  • 相关阅读:
    gitment Error:validation failed错误解决办法
    Hexo博客yilia主题添加Gitment评论系统
    用DateTime的ParseExact方法解析特殊的日期时间
    C#中的日期处理函数
    SQL,Linq,Lambda之间的转换练习
    Windows Azure Platform 系列文章目录
    Linq查询表达式
    EF框架的三种工作方式
    jQuery UI 实现图片循环显示,常用于网站首页banner广告切换
    jQuery UI Datepicker
  • 原文地址:https://www.cnblogs.com/wuyudong/p/cpp-change2.html
Copyright © 2020-2023  润新知