《c++對象內存模型》讀書筆記
這本書大二第一次接觸,剛開始由於功力不夠不能很好的消化這本書的內功,多讀幾遍就會對自己的語言思想有很大的提升。這本書出版很久了,但一直沒被淘汰。記錄自己的學習筆記,溫故知新。
C語言不是面向對象的計算機編程語言,它主要由基本數據類型,struct結構體,和函數,數據塊和函數是沒有關聯的,函數就相當於服務指令,你想加工什麼數據,就通過調用函數傳人數據參數,從而獲得該函數的服務。這樣的話當工程很大的時候就就會定義很多的函數很多的結構體,這些結構體和函數根據去功能通過.h .m文件進行劃分,成百上千的函數結構體會讓頭文件的引用變得非常的複雜,從而降低開發效率,和程序設計的難度。
C++是面向對象編程的計算機高級語言,封裝,繼承,多態。數據塊和處理數據塊的功能函數就關聯起來,以前用文件的形式來劃分數據塊和函數變成的用類的形式。提高開發效率,用面向對象的程序設計思想來設計程序使得更加的清晰。
對於計算機語言,與其說高級語言牛叉到不如說是編譯器牛叉,從本質上來講,計算機高級語言寫出來的源代碼都逃不過編譯器的掌心,它們都會被編譯成對應的CPU指令,對於動態語言無非就是加上一個runtime,既然語言都最後會變成CPU指令,那麼計算機語言之間都可以一一對應的關係,那麼C++語言編寫的程序也無非就是函數的調用,參數的傳遞,但在語法特性上又存在各自的差異。這就說明了編譯器對C++語言做了很多的事。
計算機語言是標準,對應的編譯器是標準的實現,編譯器越是智能越是強大,它爲你做的東西就多,這就爲什麼java,c#,swift不需要頭文件,因爲開發環境編譯器能動態的檢測到你設計的變量和函數,swift甚至不需要main函數,swift是面向對象的語言,如果編譯器檢測到了函數調用,它就會用這個函數作爲main函數作爲程序的入口點。
對於不同的書,不同的讀者有不同的看法,以下爲個人學習記錄,不足之處還望見諒。
問題:
1.問什麼會有self變量?
2.成員函數和虛函數的區別?
3.爲什麼虛函數能實現動態調用和多態?
1.c和c++對比
- typedef struct point3d {
- float a;
- float b;
- float c;
- }point3d;
- void printPoint3d(const point3d *pd){
- printf("a= %g,b= %g,c = %g",pd->a,pd->b,pd->c);
- }
- point3d p;
- p.a=4;
- p.b=5;
- p.c=6;
- printPoint3d(&p);
在c語言中我們要完成一個功能函數的調用首先要創建需要的參數,把參數傳人到函數中。
- class point3d{
- private:
- float a;
- float b;
- float c;
- public:
- void printPoint3d(){
- printf("a= %g,b= %g,c = %g",a,b,c);
- }
- };
使用時:非虛函數的成員函數在編譯時就已經確定它的調用地址,而虛函數的調用是在運行時綁定。
- point3d *p=new point3d();
- p->printPoint3d();
在c++中我們想要對數據塊進行操作只需要調用和它包裝工能函數就行了,成員變量(data member)可以映射爲struct數據塊,成員函數 (data function)可以映射爲打印功能函數。
2.this的由來
- #include <stdio.h>
- class point3d{
- private:
- float a;
- float b;
- float c;
- public:
- void printPoint3d();
- };
實現文件:
- #include "Point3d.h"
- void point3d::printPoint3d(){
- this->a =1;
- this->b =2;
- this->c =3;
- printf("a= %g,b= %g,c = %g\n",this->a,this->b,this->c);
- }
- #include <iostream>
- #include "Point3d.h"
- int main(int argc, const char * argv[]) {
- point3d *point = new point3d();
- point->printPoint3d();
- return 0;
- }
運行結果:
a= 1,b= 2,c = 3
Program ended with exit code: 0
從上面的例子中我們可以看到成員變量中可以通過this指針使用成員變量。
下面我們來一步一步的分解改例子:
該類經過編譯之後就得到:
3.虛函數
- class point3d{
- private:
- float a;
- float b;
- float c;
- public:
- virtual void printPoint3d();
- };
- point->printPoint3d();
4.多態
- class point3d{
- public:
- float a;
- float b;
- float c;
- public:
- virtual void printPoint3d();
- };
class point3d.m
- void point3d::printPoint3d(){
- this->a =1;
- this->b =2;
- this->c =3;
- printf("a= %g,b= %g,c = %g\n",this->a,this->b,this->c);
- }
class point4d.h
- #include <stdio.h>
- #include "Point3d.h"
- class Point4d:public point3d{
- private:
- float d;
- public:
- void printPoint3d();
- };
class point4d.m
- void Point4d::printPoint3d(){
- this->a=1;
- this->b=2;
- this->c=3;
- this->d=4;
- printf("point 4d print :a= %g,b= %g,c = %g ,d= %g\n",this->a,this->b,this->c,this->d);
- }
使用:
- #include <iostream>
- #include "Point3d.h"
- #include "Point4d.h"
- int main(int argc, const char * argv[]) {
- point3d *point = new point3d();
- point->printPoint3d();
- point = new Point4d();
- point->printPoint3d();
- return 0;
- }
運行結果:
- a= 1,b= 2,c = 3
- point 4d print :a= 1,b= 2,c = 3 ,d= 4
- Program ended with exit code: 0