電商專業(yè)學(xué)習(xí)嵌入式軟件開(kāi)發(fā)第五十六天

  • C++第二天

今天晚上系統(tǒng)網(wǎng)絡(luò)考試,考得真是一塌糊涂,鐵定需要補(bǔ)考了。第一階段的項(xiàng)目至今為止還有兩處函數(shù)調(diào)用出問(wèn)題沒(méi)有解決,而且是最重要的兩處,這兩處解決不了這個(gè)項(xiàng)目只能勉強(qiáng)算完成三分之一,這兩天一直在查代碼,改代碼,就是沒(méi)有進(jìn)展。今天C++講的內(nèi)容不算多,但是現(xiàn)在的感覺(jué)又像是剛開(kāi)始學(xué)C基礎(chǔ)似的,老師讓寫個(gè)小練習(xí)都寫不出來(lái),極度缺乏代碼量。

fun:函數(shù)重載

#include <iostream>
using namespace std;

//g++ fun.cpp -o fun.o
//objdump -t fun.o

//函數(shù)重載
// int biggerii(int a, int b){}
int bigger(int a, int b)
{
    cout << "int,int" << endl;  
    return (a > b ? a:b);
}
//float biggerff(float a, float b){}
float bigger(float a, float b)
{
    cout << "float,float" << endl;  
    return (a > b ? a:b);
}
//double biggerdd(double a, double b){}
double bigger(double a, double b)
{
    cout << "double,double" << endl;    
    return (a > b ? a:b);
}

int main(void)
{
    cout << bigger(3, 4) << endl;
//  cout << biggerii(3, 4) << endl;
    cout << bigger(3.6, 3.67) << endl;
//cout << biggerdd(3.6, 3.67) << endl;
    cout << bigger(3.6f, 3.67f) << endl;
//  cout << biggerff(3.6f, 3.67f) << endl;

    return 0;
}

inline:類似于宏定義

#include <iostream>
using namespace std;

#define fun(x, y) x*y
//假如一段代碼邏輯結(jié)構(gòu)比較簡(jiǎn)單但是被頻繁的調(diào)用,那么可以將該段代碼定義為inline函數(shù)
//邏輯結(jié)構(gòu)比較簡(jiǎn)單:沒(méi)有多重條件判斷,沒(méi)有循環(huán)結(jié)構(gòu),沒(méi)有嵌套調(diào)用,沒(méi)有遞歸調(diào)用等
inline int test(int x, int y)
{
    return x*y;
}

int main(void)
{
    cout << fun(2+3, 4+5) << endl;
    //2+3*4+5
    cout << test(2+3, 4+5) << endl;

    return 0;
}

可以使用默認(rèn)值也可以重新賦值

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

struct Student
{
    int iId;
    char caName[32];
    float fScore;
    char caPwd[32];
};
void show(const Student &stu)
{
    cout << stu.iId << ' ' << stu.caName << ' ' << stu.fScore << ' ' << stu.caPwd << endl;
}
#if 0
Student *makeStu(const char *pwd = "123", const char *name){}
#endif
//makeStu(, "lisi"); // x
//makeStu("", "lisi");  // x
//makeStu("123", "lisi");  //
//makeStu("lisi");  // x
//一般將帶有默認(rèn)值的形參寫在形參列表最后
Student *makeStu(const char *name, const char *pwd = "abcdefg")
{
    static int s_id = 1000;
    Student *pstu = (Student *)malloc(sizeof(Student));
    pstu->iId = s_id++;
    strcpy(pstu->caName,name);
    pstu->fScore = 0.0f;
    strcpy(pstu->caPwd, pwd);
    return pstu;
}

int main(void)
{
    Student *stu = makeStu("zhangsan", "123456");
    show(*stu);

//  Student *s2 = makeStu("lisi");
//  show(*s2);

    return 0;
}

多態(tài):靜態(tài)多態(tài)---函數(shù)重載實(shí)現(xiàn)

#include <iostream>
using namespace std;

//g++ swap.cpp -o swap.o
//objdump -t swap.o

//多態(tài):靜態(tài)多態(tài)---函數(shù)重載實(shí)現(xiàn)
//編譯之后就可以確定到底調(diào)用哪個(gè)函數(shù),將此過(guò)程稱之為靜態(tài)綁定

typedef struct Student
{
    int iId;
    char caName[32];
    float fScore;
}STU;
void show(STU &stu)
{
    cout << "id:" << stu.iId << " name:" << stu.caName << " score:" << stu.fScore << endl;
}

void myswap(int &a, int &b)
{
    int tmp = a;
    a = b;
    b = tmp;
    cout << "myswap(int)" << endl;
}

void myswap(double &a, double &b)
{
    double tmp = a;
    a = b;
    b = tmp;
    cout << "myswap(double)" << endl;
}

void myswap(STU &s1, STU &s2)
{
    STU tmp = s1;
    s1 = s2;
    s2 = tmp;
    cout << "myswap(STU)" << endl;
}

int main(void)
{
    int a = 90;
    int b = 178;
    cout << "a=" << a << " b=" << b << endl;
    myswap(a, b);
    cout << "a=" << a << " b=" << b << endl;
    cout << "...................\n";
    double d1 = 8.9;
    double d2 = 6.7;
    cout << "d1=" << d1 << " d2=" << d2 << endl;
    myswap(d1, d2);
    cout << "d1=" << d1 << " d2=" << d2 << endl;
    cout << "...................\n";
    STU s1 = {1001, "zhangsan", 89};
    STU s2 = {1002, "lisi", 99};
    show(s1);
    show(s2);
    myswap(s1, s2);
    show(s1);
    show(s2);

    return 0;
}

template:模板函數(shù)

#include <iostream>
using namespace std;

typedef struct Student
{
    int iId;
    char caName[32];
    float fScore;
}STU;
void show(STU &stu)
{
    cout << "id:" << stu.iId << " name:" << stu.caName << " score:" << stu.fScore << endl;
}

//函數(shù)模板
template<typename U>
void myswap(U &a, U &b)
{
    U tmp = a;
    a = b;
    b = tmp;
}
#if 0
void myswap(int &a, int &b)
{
    int tmp = a;
    a = b;
    b = tmp;
}
#endif
#if 0
void myswap(double &a, double &b)
{
    double tmp = a;
    a = b;
    b = tmp;
}
#endif
#if 0
void myswap(STU &a, STU &b)
{
    STU tmp = a;
    a = b;
    b = tmp;
}
#endif
int main(void)
{
    int a = 90;
    int b = 178;
    cout << "a=" << a << " b=" << b << endl;
    myswap(a, b);
    cout << "a=" << a << " b=" << b << endl;
    cout << "...................\n";
    double d1 = 8.9;
    double d2 = 6.7;
    cout << "d1=" << d1 << " d2=" << d2 << endl;
    //函數(shù)調(diào)用時(shí),函數(shù)模板根據(jù)傳入的具體實(shí)參,自動(dòng)生成具體類型的模板函數(shù)。
    myswap(d1, d2);
    cout << "d1=" << d1 << " d2=" << d2 << endl;
    cout << "...................\n";
    STU s1 = {1001, "zhangsan", 89};
    STU s2 = {1002, "lisi", 99};
    show(s1);
    show(s2);
    myswap(s1, s2);
    show(s1);
    show(s2);

    return 0;
}

new:開(kāi)辟空間

#include <iostream>
using namespace std;

int main(void)
{
//  int *p = (int *)malloc(sizeof(int));
      //int*p2=(int*)malloc(5*sizeof(int));
    //malloc(2*3*sizeof(int));
    //new/delete 是關(guān)鍵字
    int *p = new int;
    delete p;//釋放空間
    
    int *p2 = new int[5];
    delete []p2;

    int (*p3)[3] = new int[2][3];
    delete []p3;
    
    int (*p4)[4][6] = new int[2][4][6];
    delete []p4;
    //sizeof(p4) = 4;//32位機(jī)
    return 0;
}

string:拼接、比較

#include <iostream>
#include <string>   //std::string
using namespace std;

int main(void)
{
    string str = "Hello";
    cout << str << endl;

    //char arr1[32] = "Hello";
    //char arr2[32] = {'\0'};
    //arr2 = arr1; ?
    string str2 = str;
    cout << str2 << endl;
    //string這種類型對(duì)‘+’進(jìn)行了重新實(shí)現(xiàn)
    //拼接時(shí):第一個(gè)或者第二個(gè)必須是string對(duì)象
    string str3 = str + "world";
    //string str3 = "world" + "aaa" + str;  // X
    str3 = str3 + "abc" + "123";
    cout << str3 << endl;

    if (str == str1)
//  if (str >= str1)
//  if (str <= str1)

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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