22.C++- 繼承與組合,protected訪問級別
阿新 • • 發佈:2018-09-19
urn bject href clu tps pac protected https namespace
#include <iostream> #include <string> #include <sstream> using namespace std; class Object { protected: string mName; string mInfo; public: Object() { mName="Object"; mInfo=""; } string name() { return mName; } string info() { return mInfo; } }; class Point : public Object { protected: int x; //坐標 int y; public: Point(int x=0,int y=0) { ostringstream s; this->x=x; this->y=y; s<<"P("<<x<<","<<y<<")"; //坐標信息 mName="Point"; mInfo=s.str(); } }; class Line : public Object { private: Point mP1; Point mP2; public: Line(Point p1,Point p2) { ostringstream s; mP1 =p1; mP2 =p2; s<<"Line from " <<p1.info() <<" to "<<p2.info(); //線的信息 mName ="Line"; mInfo =s.str(); } }; int main() { Point p1(2,3); Point p2(6,3); Line L(p1,p2);
cout<<p1.name()<<":"<<endl; cout<<p1.info()<<endl; cout<<L.name()<<":"<<endl; cout<<L.info()<<endl; return 0; }
文章來源:https://www.cnblogs.com/lifexy/p/8698104.html
22.C++- 繼承與組合,protected訪問級別