本週終於完成了第一階段的學習任務(動態庫實現+測試環境的搭建)。又陷入了另外一個死循環。說到底仍是由於過於偏執(以前寫過有關C++IDE的搭建的說明。詳情可參見 https://my.oschina.net/u/3435444/blog/1476572),可不知爲何sublime text 3 的 格式化代碼控件(coolformat)沒法正常運行。糾結了三天後,終於大徹大悟。凡事不要依賴於別人,索性迴歸原始。使用VC++ 6.0 做爲 開發IDE。繞了一大圈又回到原點,也許這就是生活吧!閒話少量,書接標題。類繼承和對象實例化。自己沒有太多的難度,只是從一個java開發者的角度按照C++的模式,實現相關內容。代碼以下:java
class Box code:ios
Box.cpp學習
#include <iostream> #include "Box.h" using namespace std; Box::Box(){ cout << "class box struct method is running\n"; name=(char *)malloc(sizeof(10)); } Box::~Box(){ cout << "class box destruct method is running\n"; }
Box.h測試
class Box{ public : Box(); ~Box(); protected: double length; char* name; } ;
class SmallBox code:spa
SmallBox.cpp.net
#include <iostream> #include "SmallBox.h" using namespace std; SmallBox::SmallBox(){ cout << "class SmallBox struct method is running\n"; } SmallBox::~SmallBox(){ cout << "class SmallBox destruct method is running\n"; } double SmallBox::getLength(void){ return length; } void SmallBox::setLength(double len){ length=len; }
SmallBox.hcode
#include "Box.h" class SmallBox:Box{ public: SmallBox(); ~SmallBox(); double getLength(void); void setLength(double); } ;
test.cpporm
#include <iostream> #include "SmallBox.h" using namespace std; int main(){ cout << "********************************\n"; cout << "main method is running !\n"; SmallBox *obj =new SmallBox(); obj->setLength(10.1); cout << "SmallBox length is "<< obj->getLength() << "\n"; delete(obj); cout << "********************************\n"; return 0; }
總結:對象
已上內容經過SmallBox 繼承Box ,在main方法中,初始化SmallBox 並調用 相關方法的demo。例子很簡單,關鍵是實現思路。若有不妥之處還請賜教。blog