1. 程式人生 > >學習C++的道路 博主會持續更新的

學習C++的道路 博主會持續更新的

變量 拆分 return radius eve 接口 一個人 main stream

2018.9.3 今天開始正式上課了 也是好好開始學習C++的日子了

今天買了兩本書 C++項目開發 與 C接口----等我的書回來一定要好好的學習一波

博主用的編譯軟件是VS2017 ----- 地址 : https://visualstudio.microsoft.com/zh-hans/vs/whatsnew/

今天:學習了利用繼承改寫組合關系 我把今天寫的筆記總結了下來 如下(有寫的不對的地方,大家不要見怪 提出來我們共同解決):

技術分享圖片
  1 /*
  2     8:繼承使用的兩種情況
  3     (1)、類之間有自然的繼承關系,一個類另一個類的特例。
4 fx:一個學生是一個人 5 (2)、實現代碼復用,一個類需要使用另一個類中的成員時 6 9:繼承的好處 7 (1)、代碼復用 code reuse 8 (2)、使代碼容易修改 9 10:兩個類之間的交互關系 10 (1)、組合類 ------》 一個類中有另一個類的對象 has-a的關系 11 ====== 組合改繼承 那麽 我們可以一步一步去拆分進而去得到 12 (2)、繼承 ------》 一個雷是另一個類的特例 is-a的關系 13
14 學到了學到了 輸出運算符重載 15 ostream & operator << (ostream & os, Point & apoint){ 16 os << "Point:X:Y: "<<apoint.x<<","<<apoint.y<<"\n"; 17 return os; 18 //其中 Point是具體的某一個類 這個類名是可以進行更改的 19 } 20 21 */ 22
#include <iostream> 23 using namespace std; 24 class Point { 25 26 }; 27 28 class Circle { 29 private: 30 Point center; 31 float radius; 32 }; 33 34 class Cylinder { 35 private: 36 Circle c; 37 float height; 38 }; 39 40 //上邊是用組合來寫的 41 //接下來給出用繼承來寫 42 class Point1{ 43 private: 44 float x, y; 45 }; 46 47 class Circle1:Point1 { 48 private: 49 float radious; 50 }; 51 52 class Cylinder : Circle1 { 53 private: 54 float height; //自己擴展 + 父類的成員 + 爺爺的成員 55 }; 56 57 //接下來書寫組合改繼承的具體代碼 註意:與上邊要分開 否則一起運行時會報錯的 58 59 #include <iostream> 60 #include <cmath> 61 using namespace std; 62 63 class Point { 64 friend ostream & operator << (ostream & , Point & ); 65 protected: 66 double x, y; 67 public: 68 Point():x(0),y(0){} 69 Point(double _x, double _y) { 70 x = _x; 71 y = _y; 72 } 73 }; 74 75 ostream & operator << (ostream & os, Point & apoint) { 76 os << "Point:X:Y " << apoint.x << "." << apoint.y << "\n"; 77 return os; 78 } 79 80 //圓繼承點 81 class Circle : public Point{ 82 friend ostream & operator << (ostream &, Circle &); 83 protected: 84 double radius; 85 public: 86 Circle():Point(),radius(0){} 87 Circle(double r,double xval,double yval):Point(xval,yval),radius(r){} 88 89 double area() { 90 return (3.14159 * radius * radius); 91 } 92 }; 93 94 ostream & operator << (ostream & os, Circle & aCircle) { 95 os << "Cicle:radius:" << aCircle.radius; 96 os << aCircle.x << "\n"; 97 os << aCircle.y << "\n"; 98 return os; 99 } 100 101 class Cylinder :public Circle { 102 friend ostream & operator << (ostream &, Cylinder &); 103 protected: 104 double height; 105 public: 106 Cylinder() :Circle() { height = 0.0; } 107 Cylinder(double hv, double rv, double xv, double yv) :Circle(rv, xv, yv) { 108 height = hv; 109 } 110 111 double area() { 112 return (2 * Circle::area() + 2 * 3.14159 * radius * height); 113 } 114 }; 115 116 ostream & operator << (ostream & os, Cylinder & acylinder) { 117 os << "cylinder dimensions"; 118 os << "x:" << acylinder.x; 119 os << " y:" << acylinder.y; 120 os << " radius: " << acylinder.radius; 121 os << " height: " << acylinder.height << endl; 122 return os; 123 } 124 125 int main(int argc, const char * argv[]) { 126 Point p(2, 3); 127 Circle c(7, 6, 5); 128 Cylinder cyl(10, 11, 12, 13); //半徑不同 調用的函數變量的值不同 129 130 cout << p; 131 cout << c; 132 cout << "area circle:" << c.area() << endl; 133 134 cout << cyl; 135 cout << "area cylinder:" << cyl.area() << endl; 136 137 cout << "area cylinder base is " << cyl.Circle::area() << endl; 138 139 return 0; 140 }
繼承與組合

學習C++的道路 博主會持續更新的