C++ ----帶默認形參的函數(shù),inline(),模板函數(shù),動態(tài)內(nèi)存,和類

帶有默認值形參的函數(shù)

#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;
    }
};

//函數(shù)重載時,可能會存在歧義
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 函數(shù)

和宏定義的區(qū)別,宏定義原樣替換,inline(),先進行運算,后替換

將函數(shù)的具體實現(xiàn),替換到出現(xiàn)函數(shù)調(diào)用的地方;沒有函數(shù)調(diào)用的消耗;

實參若存在表達式,會先將表達式進行計算,在進行相應的操作

使用場合,若某段代碼被頻繁調(diào)用,并且該段代碼邏輯結(jié)構(gòu)比較簡單,可用將該段代碼定義為inline()函數(shù)

邏輯結(jié)構(gòu)簡單:沒有循環(huán),沒有嵌套,沒有多重條件結(jié)構(gòu)

#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;
}
//將函數(shù)的具體實現(xiàn)替換到出現(xiàn)函數(shù)調(diào)用的地方
//沒有函數(shù)調(diào)用的消耗

//實參若存在表達式,會先進行表達式計算
//再進行相應操作

//若某段代碼被頻繁的調(diào)用,并且該段代碼邏輯
//結(jié)構(gòu)比較簡單,那么可以將該段代碼定義為
//inline函數(shù)

//碼邏輯結(jié)構(gòu)比較簡單:沒有循環(huán),沒有嵌套,
//沒有多重條件結(jié)構(gòu)
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;
}

函數(shù)模板

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); //模板函數(shù)
    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() 數(shù)組排序

字符串 數(shù)據(jù)類型

string

//字符串操作
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    //string 是類似于結(jié)構(gòu)體的一種類型,含有各式各樣的成員函數(shù)

    //定義,復制
    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;
  
    //拼接
    //字符串拼接時,第一個或者第二個操作數(shù),必須是string對象
    string str4 = str+"I like it";
    cout << str4 <<endl;
    //錯誤拼接
    //string str5 = "orange"+"I like it";

    //大小,不包含'\0'的長度
    cout <<str.length()<<endl;
    return 0;
}

new 動態(tài)申請內(nèi)存-----運算符

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類型數(shù)據(jù)的空間:4字節(jié)
    //使用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 字節(jié)
    delete []pArr;
    Student *pStu = new Student;//sizeof(Student)字節(jié)
    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:
        //函數(shù)的聲明,和傳參
        //變量的聲明
        void exer();
        int exer2();
  private:
    //函數(shù)的聲明,和傳參
    //變量聲明
  };
void TDD::exer()
  { 
  }
int TDD::exer2()
  {
  }
int main(void)
  {
    TDD tdd;
        
    return 0;
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy閱讀 9,676評論 1 51
  • 題目類型 a.C++與C差異(1-18) 1.C和C++中struct有什么區(qū)別? C沒有Protection行為...
    阿面a閱讀 7,891評論 0 10
  • 1. 讓自己習慣C++ 條款01:視C++為一個語言聯(lián)邦 為了更好的理解C++,我們將C++分解為四個主要次語言:...
    Mr希靈閱讀 2,994評論 0 13
  • 第一天 一.內(nèi)聯(lián)函數(shù)(inline) 函數(shù)調(diào)用的時候需要建立棧內(nèi)存環(huán)境,進行參數(shù)傳遞,并產(chǎn)生程序執(zhí)行轉(zhuǎn)移,這些工作...
    陳果123閱讀 1,221評論 0 1
  • 上午聽了以琳姐夫妻倆的分享,分享的真好,聽的讓人非常享受的感覺。我也要成為這樣的人。 方向?qū)α?,心有所向,法有萬種...
    陸敏Amy閱讀 221評論 0 0

友情鏈接更多精彩內(nèi)容