美文网首页上嵌学习笔记
C++ ----带默认形参的函数,inline(),模板函数,动

C++ ----带默认形参的函数,inline(),模板函数,动

作者: Hassan_chao | 来源:发表于2017-02-10 17:27 被阅读20次

    带有默认值形参的函数

    #include <iostream>
    #include <string.h>
    using namespace std;
    struct Student
    {
        int iId;
        char caName[32];
        float fScore;
        char caPwd[32];
        void info()
        {
            cout << iId << ' ' << caName
                 << ' ' << fScore
                 << ' ' << caPwd << endl;
        }
    };
    
    //函数重载时,可能会存在歧义
    void initStu(Student &stu, int id
                 , const char *pName)
    {
    
    }
    //带有默认值的形参
    //一般将带有默认值的形参写在形参列表最后
    void initStu(Student &stu, int id
                 , const char *pName
                 , float score = 60
                 , const char *pPwd="123456")
    {
        stu.iId = id;
        strcpy(stu.caName, pName);
        stu.fScore = score;
        strcpy(stu.caPwd, pPwd);
    }
    int main(void)
    {
        Student stu;
        //initStu(stu, 10001, "张三", 89, "abc@123");
        initStu(stu, 10001, "张三", 89);
        //initStu(stu, 10001, "张三");
        //initStu(stu, 10001, "张三", 89, "abc@123");
        stu.info();
        return 0;
    }
    

    inline 函数

    和宏定义的区别,宏定义原样替换,inline(),先进行运算,后替换

    将函数的具体实现,替换到出现函数调用的地方;没有函数调用的消耗;

    实参若存在表达式,会先将表达式进行计算,在进行相应的操作

    使用场合,若某段代码被频繁调用,并且该段代码逻辑结构比较简单,可用将该段代码定义为inline()函数

    逻辑结构简单:没有循环,没有嵌套,没有多重条件结构

    #include <iostream>
    using namespace std;
    #define Test(M, N) M*N //3+4*5+6
    void test(int m, int n)
    {
        cout << m*n << endl;
    }
    //将函数的具体实现替换到出现函数调用的地方
    //没有函数调用的消耗
    
    //实参若存在表达式,会先进行表达式计算
    //再进行相应操作
    
    //若某段代码被频繁的调用,并且该段代码逻辑
    //结构比较简单,那么可以将该段代码定义为
    //inline函数
    
    //码逻辑结构比较简单:没有循环,没有嵌套,
    //没有多重条件结构
    inline void fun(int m, int n)
    {
        cout << m*n << endl;
    }
    int main(void)
    {
        cout << Test(3+4, 5+6) << endl;
        test(3+4, 5+6);
        fun(3+4, 5+6);
        return 0;
    }
    

    函数模板

    template

    #include <iostream>
    using namespace std;
    template <typename U, typename M>
    int  myMax(U a, M b)
    {
        int ret = 0;
        if (a > b)
        {
            ret = 1;
        }
        else
        {
            ret = 0;
        }
    
        return ret;
    }
    int main(void)
    {
    #if 0
        myMax(12, 45);  //-->myMax(int, int); //模板函数
        myMax(12, 4.5); //-->myMax(int, double);
        myMax('a', 3.4);//-->myMax(char, double);
    #endif
        int ret = 0;
        ret = myMax('a', 3.4);
        cout << ret << endl;
        return 0;
    }
    

    qsort() 数组排序

    字符串 数据类型

    string

    //字符串操作
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    {
        //string 是类似于结构体的一种类型,含有各式各样的成员函数
    
        //定义,复制
        string str = "hello,world";
        string str1("hello,world");
        string str2 = str1;
        string str3;
        str3 = str;
        cout << str <<endl;
        cout << str1 <<endl;
        cout << str2 <<endl;
        cout << str3 <<endl;
      
        //拼接
        //字符串拼接时,第一个或者第二个操作数,必须是string对象
        string str4 = str+"I like it";
        cout << str4 <<endl;
        //错误拼接
        //string str5 = "orange"+"I like it";
    
        //大小,不包含'\0'的长度
        cout <<str.length()<<endl;
        return 0;
    }
    

    new 动态申请内存-----运算符

    delete 释放空间-------运算符

    //实例1  
    #include <iostream>
    using namespace std;
    struct Student
    {
        int iId;
        char caName[32];
        float fScore;
        void info()
        {
            cout << iId << ' ' << caName 
                 << ' ' << fScore << endl;
        }
    };
    int main(void)
    {
        //char *p = (char *)malloc(32);
        //int *p = new int; //申请存放int类型数据的空间:4字节
        //使用87初始化空间
        
        //new和delete是运算符    
        int *p = new int(87);
        cout << *p << endl;
        delete p;
    
        char *pc = new char[1];
        delete []pc;
    
        int *pArr = new int[7]; //4*7 字节
        delete []pArr;
        Student *pStu = new Student;//sizeof(Student)字节
        delete pStu;
        return 0;
    }
    
    //实例2
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    typedef int (*PARR)[4];
    typedef int (*PARR2)[5][6];
    int main(void)
    {
        //int arr[3][4];
        int (*pa)[4] = (PARR)malloc(sizeof(int)*3*4);   
        int (*pa2)[4] = new int[3][4];
        delete []pa;
        delete []pa2;
        //fun(int a[3][4]) -->fun(int (*p)[4])
        //int arr[4][5][6];
        int (*pp)[5][6] = (PARR2)malloc(sizeof(int)*4*5*6);
        PARR2 pp2 = new int[4][5][6];
        delete []pp;
        delete []pp2;
        //fun(int a[4][5][6]) -->fun(int (*p)[5][6])
        return 0;
    }
    

    类的使用

    class的使用方法

    //类的使用形式
    #include <iostream>
    using namespace std;
    
    class TDD
      {
        public:
            //函数的声明,和传参
            //变量的声明
            void exer();
            int exer2();
      private:
        //函数的声明,和传参
        //变量声明
      };
    void TDD::exer()
      { 
      }
    int TDD::exer2()
      {
      }
    int main(void)
      {
        TDD tdd;
            
        return 0;
      }
    

    相关文章

      网友评论

        本文标题:C++ ----带默认形参的函数,inline(),模板函数,动

        本文链接:https://www.haomeiwen.com/subject/pvtnittx.html