自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

適合具備 C 語言基礎(chǔ)的 C++ 教程之一

開發(fā) 后端
C 語言通常被認(rèn)為是一種面向過程的語言,因?yàn)槠浔旧淼奶匦愿菀拙帉懨嫦蜻^程的代碼,當(dāng)然也不排除使用 C 語言編寫面向過程的代碼,比如 Linux 的源代碼以及現(xiàn)在很火的國產(chǎn)物聯(lián)網(wǎng)操作系統(tǒng) RT-Thread,其內(nèi)核的實(shí)現(xiàn)方式都是使用 C 語言實(shí)現(xiàn)的面向?qū)ο蟮拇a。

 [[381386]]

引言

C 語言通常被認(rèn)為是一種面向過程的語言,因?yàn)槠浔旧淼奶匦愿菀拙帉懨嫦蜻^程的代碼,當(dāng)然也不排除使用 C 語言編寫面向過程的代碼,比如 Linux 的源代碼以及現(xiàn)在很火的國產(chǎn)物聯(lián)網(wǎng)操作系統(tǒng) RT-Thread,其內(nèi)核的實(shí)現(xiàn)方式都是使用 C 語言實(shí)現(xiàn)的面向?qū)ο蟮拇a。相比于 C 語言來說,C++ 更能夠?qū)崿F(xiàn)面向?qū)ο蟮某绦蛟O(shè)計(jì),其具有的特性也要比 C 語言要多的多。下面假設(shè)有這樣一個(gè)需求。

現(xiàn)要描述兩個(gè)人的信息,姓名,職業(yè),年齡,并輸出。

我們首先先使用 C 語言的設(shè)計(jì)思路實(shí)現(xiàn)這個(gè)功能。

C語言描述

如果使用 C 語言來描述上面這個(gè)問題,大部分都會(huì)想到使用結(jié)構(gòu)體來完成這個(gè)要求,寫出的程序也就如下所示:

  1. #include <stdio.h> 
  2.  
  3. struct person 
  4.     char *name
  5.     int age; 
  6.     char *work
  7. }; 
  8.  
  9. int main(int argc, char** aggv) 
  10.     struct person persons[] = { 
  11.         {"wenzi",24,"programer"}, 
  12.         {"jiao", 22,"teacher"}, 
  13.     }; 
  14.  
  15.     char i; 
  16.     for (i = 0; i < 2; i++) 
  17.     { 
  18.         printf("name is:%s,age is:%d,work is:%s\n",persons[i].name,persons[i].age,persons[i].work); 
  19.     } 

上述這是比較初級(jí)的寫法,如果對(duì) C 語言了解的更多一點(diǎn)的人在寫這段程序的時(shí)候,會(huì)使用函數(shù)指針的方式將代碼寫的更加巧妙,代碼如下所示:

  1. #include <stdio.h> 
  2.  
  3. struct person 
  4.     char *name
  5.     int age; 
  6.     char *work
  7.  
  8.     void (*printInfo)(struct person *per); 
  9. }; 
  10.  
  11. void printInfo(struct person *per) 
  12.     printf("The people's name is:%s,age is:%d,work is:%s\n",per->name,per->age,per->work); 
  13.  
  14.  
  15. int main(int argc, char** argv) 
  16.     struct person per[2]; 
  17.  
  18.     per[0] = {"wenzi",18,"programer",printInfo}; 
  19.     per[1] = {"jiaojiao",18,"teacher",printInfo}; 
  20.  
  21.     per[0].printInfo(&per[0]); 
  22.     per[1].printInfo(&per[1]); 

使用了函數(shù)指針的方式來書寫這個(gè)程序,程序也變得更加簡介了,主函數(shù)里也少了 for循環(huán)。

C++ 的引入

那除此之外,還有更好的書寫方式么,這個(gè)時(shí)候就要引入 C++ 的特性了,上述代碼中在執(zhí)行函數(shù)時(shí)都傳入了參數(shù),那要如何做才能將上述中的參數(shù)也省略去呢,且看如下的代碼:

  1. #include <stdio.h> 
  2.  
  3. struct person 
  4.     char *name
  5.     int age; 
  6.     char *work
  7.  
  8.     void prinfInfo(void) 
  9.     { 
  10.          printf("The people's name is:%s,age is:%d,work is:%s\n",name,age,work);        
  11.     } 
  12. }; 
  13.  
  14. int main(int argc, char** argv) 
  15.     struct person persons[] = { 
  16.         {"wenzi", 18,"program"}, 
  17.         {"jiao", 18, "teacher"}, 
  18.     }; 
  19.  
  20.     persons[0].prinfInfo(); 
  21.     persons[1].prinfInfo(); 
  22.  
  23.     return 0; 

上述代碼中使用了 C++ 的特性,在結(jié)構(gòu)體中定義了函數(shù),然后也就可以直接調(diào)用函數(shù)了,跟上面 C 語言的代碼相比較,它沒了實(shí)參,而且代碼看起來也比 C 語言更加簡潔了。

實(shí)際在 C++ 中它具有自己獨(dú)有的一套機(jī)制來實(shí)現(xiàn)上述的代碼,也就是即將說明的 class,有了 class 之后,我們就可以這樣書寫代碼:

  1. #include <stdio.h> 
  2.  
  3. class person 
  4. public
  5.     char * name
  6.     int age; 
  7.     char * work
  8.  
  9.     void printInfo(void) 
  10.     { 
  11.         printf("The people's name is:%s,age is:%d,work is:%s\n",name,age,work);  
  12.     } 
  13.  
  14. int main(int argc, char** argv) 
  15.     person persons[] = { 
  16.         {"wenzi", 18,"program"}, 
  17.         {"jiao", 18, "teacher"}, 
  18.     }; 
  19.  
  20.     persons[0].prinfInfo(); 
  21.     persons[1].prinfInfo(); 
  22.  
  23.     return 0; 

上述就是關(guān)于 C++ 的一個(gè)簡單的引入過程。

C++ 數(shù)據(jù)訪問控制

但是為了能夠改變類里的數(shù)據(jù),但是又要使得這個(gè)改變不要越界,避免胡亂地改變,我們可以這樣來定義這個(gè)類:

  1. #include <stdio.h> 
  2. #include <iostream> 
  3.  
  4. class Person 
  5. private: 
  6.     char *name
  7.     int age; 
  8.     char *work
  9.  
  10. public
  11.     void PrintInfo(void) 
  12.     { 
  13.         cout << "name is:" << name << "age = "<< age << "work is:"<< work <<endl; 
  14.     } 
  15. }; 

這樣定義一個(gè)類之后,類里面的數(shù)據(jù)成員就變成了私有的,不能夠在外部進(jìn)行訪問,比如下面這樣子就是錯(cuò)誤的:

  1. int main(int argc, char ** argv) 
  2.     Person per; 
  3.     per.age = 10; // error 

上述這樣進(jìn)行數(shù)據(jù)的訪問就是錯(cuò)誤的,那么要如何進(jìn)行訪問呢,我們可以定義這樣一個(gè)成員函數(shù)進(jìn)行數(shù)據(jù)的讀寫,比如下面的代碼所示:

  1. #include <stdio.h> 
  2. #include <iostream> 
  3.  
  4. using namespace std; 
  5.  
  6. class Person 
  7. private: 
  8.     char *name
  9.     int age; 
  10.     char *work
  11.  
  12. public
  13.     void PrintInfo(void) 
  14.     { 
  15.         cout << "name is:" << name << ",age = "<< age << ",work is:"<< work <<endl; 
  16.     } 
  17.  
  18.     void setName(char *n) 
  19.     { 
  20.         name = n; 
  21.     } 
  22.  
  23.     int setAge(int a) 
  24.     { 
  25.         if (a < 0 || a > 150) 
  26.         { 
  27.             age = 0; 
  28.             return 0; 
  29.         } 
  30.         age = a; 
  31.     } 
  32. }; 

這樣定義了類之后,就可以訪問私有成員了,比如下面這樣進(jìn)行:

  1. int main(int argc, char **argv) 
  2.     Person per; 
  3.     per.setName("wenzi"); 
  4.     per.setAge(24); 
  5.     per.PrintInfo(); 
  6.  
  7.     return 0; 

上述代碼加入了 private 訪問控制符,通過在類里面定義成員函數(shù)的方式,能夠?qū)λ接谐蓡T進(jìn)行讀寫。

this 指針

再來看上述的代碼,我們可以看到在書寫 setName 和 setAge這兩個(gè)函數(shù)的時(shí)候,形參寫的是 char *n 和 int a,這樣子給人的感覺就不是那么的直觀,如果寫成 char *name 和 char *age 呢,比如成員函數(shù)是像下面這樣子編寫的。

  1. void setName(char *name
  2.     name = name
  3.  
  4. int setAge(int age) 
  5.     if (age < 0 || age > 150) 
  6.     { 
  7.          age = 0; 
  8.          return 0; 
  9.     } 
  10.         age = age; 

上述代碼也很容易看出問題,根據(jù) C 語言的就近原則,name = name沒有任何意義,這個(gè)時(shí)候就需要引入 this 指針。引入 this 指針之后的代碼如下所示:

  1. #include <iostream> 
  2. #include <stdio.h> 
  3.  
  4. using namespace std;  
  5.  
  6. class Person { 
  7. private: 
  8.     char *name
  9.     int age; 
  10.     char *work
  11.  
  12. public
  13.     void setName(char *name
  14.     { 
  15.         this->name = name
  16.     } 
  17.     int setAge(int age) 
  18.     { 
  19.         if (age < 0 || age > 150) 
  20.         { 
  21.             this->age = 0; 
  22.             return -1; 
  23.         } 
  24.         this->age = age; 
  25.         return 0; 
  26.     } 
  27.     void printInfo(void) 
  28.     { 
  29.         cout << "name =" << name << ", age =" << age << endl; 
  30.     } 
  31. }; 
  32.  
  33. int main(int argc, char **argv) 
  34.     Person per; 
  35.     per.setName("wenzi"); 
  36.     per.setAge(25); 
  37.     per.printInfo(); 

在上述代碼中,引入了 this 指針,通過上述代碼也可以非常清楚它的意思,就是代表當(dāng)前實(shí)例化的對(duì)象,能夠指向當(dāng)前實(shí)例化對(duì)象的成員。

程序結(jié)構(gòu)

上述代碼中,成員函數(shù)是在類里面實(shí)現(xiàn)的,這樣使得整個(gè)類看著十分的臃腫,我們可以按照如下的方式進(jìn)行書寫:

  1. #include <stdio.h> 
  2.  
  3. class Person 
  4. private: 
  5.     char *name
  6.     int age; 
  7.     char *work
  8.  
  9. public
  10.     void SetName(char *name); 
  11.     int SetAge(int age;) 
  12.     void PrintInfo(void); 
  13.  
  14. void Person::SetName(char *name
  15.     this->name = name
  16.  
  17. void Person::SetAge(int age) 
  18.     this->age = age; 
  19.  
  20. void Person::PrintInfo(void) 
  21.     cout << "name = " << name << "age = " << age << endl; 

通過在類外面實(shí)現(xiàn)我們的成員函數(shù),看起來要更為簡潔一些,上述就是代碼的實(shí)現(xiàn)形式。

多文件

上述代碼中,我們都是將代碼寫在一個(gè)文件中,這樣當(dāng)代碼量很大的時(shí)候,如果代碼都是在一個(gè)文件里,那么會(huì)使得代碼難以閱讀,這個(gè)時(shí)候,我們就會(huì)將代碼分別放在幾個(gè)文件中來進(jìn)行管理,比如實(shí)現(xiàn)上述相同的功能,我們的代碼結(jié)構(gòu)如下圖所示:

image-20210110120503456

 

其中 main.cpp文件中的內(nèi)容如下所示:

  1. #include <stdio.h> 
  2. #include "person.h" 
  3.  
  4. int main(int argc, char **argv) 
  5.     Person per; 
  6.  
  7.     //per.name = "zhangsan"
  8.     per.setName("zhangsan"); 
  9.     per.setAge(200); 
  10.     per.printInfo(); 
  11.  
  12.     return 0; 
  13. }   

可以看到在上述 main.cpp中包含了 #include "person.h"頭文件,實(shí)際上是在 person.h文件中定義了 person類,person.h文件的內(nèi)容如下:

  1. #ifndef __PERSON_H__ 
  2. #define __PERSON_H__ 
  3.  
  4. class Person { 
  5. private: 
  6.     char *name
  7.     int age; 
  8.     char *work
  9.  
  10. public
  11.     void setName(char *name); 
  12.     int setAge(int age); 
  13.     void printInfo(void); 
  14. }; 
  15.  
  16. #endif 

然后,在 person.cpp中定義了成員函數(shù):

  1. #include <stdio.h> 
  2. #include "person.h" 
  3.  
  4. void Person::setName(char *name
  5. {   
  6.     this->name = name
  7.  
  8. int Person::setAge(int age) 
  9.     if (age < 0 || age > 150) 
  10.     { 
  11.         this->age = 0; 
  12.         return -1; 
  13.     } 
  14.     this->age = age; 
  15.     return 0; 
  16.  
  17. void Person::printInfo(void) 
  18.     printf("name = %s, age = %d, work = %s\n"name, age, work);  

在有了上述三個(gè)文件之后,要如何進(jìn)行編譯呢,這個(gè)時(shí)候就需要寫一個(gè) Makefile文件,接下來簡單介紹一下 Makefile語法。

Makefile

總的來說 Makefile的規(guī)則核心就如下所示:

  1. target ... :prerequisites 
  2. command 
  3. ... 
  4. ... 

target也就是一個(gè)目標(biāo)文件,可以是Object File,也可以是執(zhí)行文件。還可以是一個(gè)標(biāo)簽

prerequisites就是要生成那個(gè)target所需要的文件或者是目標(biāo)

command就是make所要執(zhí)行的命令(任意的Shell)

說了核心的東西,來看我們當(dāng)前所編寫的 Makefile文件,Makefile文件如下所示:

  1. person: main.o person.o 
  2.     g++ -o $@ $^ 
  3.  
  4. %.o : %.cpp 
  5.     g++ -c -o $@ $< 
  6.  
  7. clean: 
  8.     rm -f *.o person     

在這里所要明確的一點(diǎn)是這樣的,就是在 Makefile中,必須使用 Tab 鍵來進(jìn)行縮進(jìn)。然后,需要明確的一個(gè)概念是,要使得代碼能夠執(zhí)行,需要經(jīng)過 編譯 -> 鏈接 -> 執(zhí)行,這三個(gè)過程才能夠運(yùn)行,編譯是把源文件編譯成中間代碼,這個(gè)中間代碼在 UNIX 是 .o 文件,然后再把大量的 .o 文件合成可執(zhí)行文件,這個(gè)過程就是 鏈接,最后,執(zhí)行我們鏈接好的可執(zhí)行文件。

我們來看上述這個(gè) Makefile文件,person是最終的可執(zhí)行文件,然后,要生成這個(gè)可執(zhí)行文件,需要 main.o文件和 person.o文件,然后執(zhí)行這個(gè)操作需要的是第二條命令,g++ -o $@ $^,其中 $@ 表示的是目標(biāo)文件,$^表示的是所有依賴文件。

然后,緊接著看第三條,%.o : %.cpp,這里表示的是通配符,表示的是所有的 .o 文件和所有的 .cpp 文件,意思就是說要生成的所有的 .o 文件依賴于 .cpp 文件,然后,執(zhí)行的命令是 g++ -c -o $@ $<其中表示的是第一個(gè)依賴文件。

最后,我們需要清楚,在編譯過程中,生成了一些中間文件以及可執(zhí)行文件,如果我們想要清除掉當(dāng)前生成的文件,那么只需要執(zhí)行 make clean就可以清除掉生成的 .o文件以及 person文件。

函數(shù)重載

C++ 不允許變量重名,但是對(duì)于函數(shù)來說,可以允許重載,只要函數(shù)的參數(shù)不同即可,這樣就完成了函數(shù)的重載,直接來看一段關(guān)于函數(shù)重載的代碼:

  1. #include <iostream> 
  2.  
  3. using namespace std; 
  4.  
  5. int add(int a, int b) 
  6.     cout<<"add int+int"<<endl; 
  7.     return a+b; 
  8.  
  9. int add(int a, int b, int c) 
  10.     cout<<"add int+int+int"<<endl; 
  11.     return a+b+c; 
  12.  
  13. double add(double a, double b) 
  14.     cout<<"add double+double"<<endl; 
  15.     return a+b; 
  16.  
  17. double add(int a, double b) 
  18.     cout<<"add int+double"<<endl; 
  19.     return (double)a+b; 
  20.  
  21. double add(double b, int a) 
  22.     cout<<"add double+int"<<endl; 
  23.     return (double)a+b; 
  24.  
  25.  
  26. int main(int argc, char **argv) 
  27.     add(1, 2); 
  28.     add(1, 2, 3); 
  29.     add(1.0, 2.0); 
  30.     add(1, 2.0); 
  31.     add(1.0, 2); 
  32.  
  33.     return 0; 

代碼很簡單,就是兩數(shù)相加的一個(gè)運(yùn)算,但是兩數(shù)相加的形參不一樣,有的形參是兩個(gè)整型的相加,還有是一個(gè)整型和浮點(diǎn)數(shù)的相加,因?yàn)?C++ 重載的功能,因此,得以定義多個(gè)函數(shù)名相同但是形參和返回值都不同的函數(shù),從而在主函數(shù)實(shí)現(xiàn)了不同類型數(shù)的相加。

引用和指針

在 C語言中是沒有引用的,在 C++ 中引用的提出也使得之前在 C 語言中必須使用指針的操作,現(xiàn)在可以使用引用完成了,但是引用又不是指針,簡單來說,引用是一個(gè)變量的別名,也就是“綽號(hào)”,對(duì)于這個(gè)別名的操作也就完全等同于被引用變量的操作。為了看是否真的是別名,我們來實(shí)驗(yàn)這樣一段代碼:

  1. #include <iostream> 
  2.  
  3. using namespace std; 
  4.  
  5. int main(int argc,char **argv) 
  6.     int m; 
  7.     m = 10;  
  8.  
  9.     int &n = m; 
  10.  
  11.     int *p = &m; 
  12.     int *p1 = &n; 
  13.  
  14.     cout << "n =" << n << endl; 
  15.     cout << "p =" << p << endl; 
  16.     cout << "p1 =" << p1 << endl; 
  17.  
  18.     return 0;  
  19.  

上述這段代碼中輸出的就是 n 的值,和 m 以及 n 變量的地址,我們來看輸出的內(nèi)容:

image-20210112235421638

 

可以看到代碼中雖然是對(duì) m 進(jìn)行了賦值,但是在輸出 n 的時(shí)候,輸出的是 m 的值,也就是說在這里對(duì)于 n 的操作是完全等同于 m 的,緊接著,我們來證實(shí) n 是否是 m 的別名,那么我們就來看 n 和 m 的地址,可以看到我們輸出的兩個(gè)變量的地址也是完全一致的,這也就證實(shí)了我們的說法。

接下來,看一段指針,引用,常規(guī)形參的一段代碼,代碼如下所示:

  1. #include <iostream> 
  2.  
  3. using namespace std; 
  4.  
  5. int add_one(int a) 
  6.     a = a+1; 
  7.     return a; 
  8.  
  9. int add_one(int *a) 
  10.     *a = *a + 1; 
  11.     return *a; 
  12.  
  13. int add_one_ref(int &b) 
  14.     b = b+1; 
  15.     return b; 
  16.  
  17. int main(int argc, char **argv) 
  18.     int a = 99; 
  19.         int &c = a; 
  20.     cout<<add_one(a)<<endl; 
  21.     cout<<"a = "<<a<<endl; 
  22.  
  23.     cout<<add_one(&a)<<endl; 
  24.     cout<<"a = "<<a<<endl; 
  25.  
  26.     cout<<add_one_ref(a)<<endl; 
  27.     cout<<"a = "<<a<<endl; 
  28.  
  29.         c++; 
  30.  
  31.     cout<<"a = "<<a<<endl; 
  32.     cout<<"c = "<<c<<endl; 
  33.  
  34.     return 0; 

 

根據(jù)上述對(duì)于引用的闡述,我們直接給出運(yùn)行結(jié)果,運(yùn)行結(jié)果如下所示:

image-20210113000240223

具體的計(jì)算過程就不再這里贅述了。

小結(jié)

OK,上述就是關(guān)于 C++ 的一個(gè)簡單的引入的過程以及其涉及到的一部分有別于C語言的語法,本教程將持續(xù)連載,歡迎各位朋友關(guān)注~

本小節(jié)所涉及的代碼可以通過百度云鏈接的方式獲?。烘溄樱篽ttps://pan.baidu.com/s/1RWPXiqiFCVApcfTdaHyDgw

提取碼:j9hd

本文轉(zhuǎn)載自微信公眾號(hào)「wenzi嵌入式軟件」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系wenzi嵌入式軟件公眾號(hào)。

 

 

 

責(zé)任編輯:武曉燕 來源: wenzi嵌入式軟件
相關(guān)推薦

2021-02-20 06:13:18

C 語言C++

2021-02-21 12:09:32

C 語言基礎(chǔ)語法

2021-02-11 08:25:17

C 語言C++ 基礎(chǔ)

2021-02-16 10:57:34

C++ C 語言windows

2021-07-16 07:21:45

C++可調(diào)用對(duì)象std::functi

2010-01-15 17:38:37

C++語言

2010-01-19 14:45:35

C++語言

2020-08-21 13:20:36

C++If ElseLinux

2021-04-25 08:11:57

C語言常量與變量標(biāo)識(shí)符命名規(guī)范

2011-07-14 17:45:06

CC++

2013-12-02 13:59:22

jQueryUI

2011-07-05 13:24:03

C++

2010-01-21 16:24:02

C++語言

2011-07-15 00:47:13

C++多態(tài)

2021-02-06 07:49:48

C語言編程開發(fā)技術(shù)

2011-07-14 16:26:01

2011-01-05 11:12:34

C++

2022-01-14 09:10:56

C++文件Linux

2011-07-13 18:24:18

C++

2010-01-25 15:09:17

C++語言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)