大母牛论坛,小邋遢简谱,2012年奥运会吉祥物
目录
在c++中,类之间可以有直接的关联关系,包括组合关系和继承关系。
组合关系描述的是类之间整体与部分的关系,具有以下特点
/*描述class的组合关系*/ #include <iostream> #include <string> using namespace std; class memory { public: memory() { cout << "memory()" << endl; } ~memory() { cout << "~memory()" << endl; } }; class disk { public: disk() { cout << "disk()" << endl; } ~disk() { cout << "~disk()" << endl; } }; class cpu { public: cpu() { cout << "cpu()" << endl; } ~cpu() { cout << "~cpu()" << endl; } }; class mainboard { public: mainboard() { cout << "mainboard()" << endl; } ~mainboard() { cout << "~mainboard()" << endl; } }; class computer { private: /*必须是其他类的对象,不能是指针,否则无法构成组合关系*/ memory mmem; disk mdisk; cpu mcpu; mainboard mmainboard; public: computer() { cout << "computer()" << endl; } void power() { cout << "power()" << endl; } void reset() { cout << "reset()" << endl; } ~computer() { cout << "~computer()" << endl; } }; int main() { computer c; return 0; }
继承关系描述的是类之间的父子关系,父类为基类,子类为派生类。
#include <iostream> #include <string> using namespace std; class hpbook : public computer //computer是组合关系示例代码中实现的类 { string mos; public: hpbook() { mos = "windows 8"; } void install(string os) { mos = os; } void os() { cout << mos << endl; } }; class macbook : public computer { public: void os() { cout << "mac os" << endl; } }; int main() { hpbook hp; hp.power(); hp.install("ubuntu 16.04 lts"); hp.os(); cout << endl; macbook mac; hp.power(); mac.os(); cout << endl; return 0; }
建议:作为类设计的一般原则,能用组合关系的,就不要用继承关系,前提是组合关系可以实现所需功能和较好的架构设计。
#include <iostream> #include <string> using namespace std; class parent { protected: int mv; public: parent() { mv = 100; } int value() { return mv; } }; class child : public parent { public: int addvalue(int v) { mv = mv + v; } }; int main() { parent p; child c; // p.mv = 1000; // error // c.mv = 10000; // error c.addvalue(50); cout << "c.mv = " << c.value() << endl; return 0; }
上面的demo简单地展示了protecded成员变量的特性和使用方式,下面再看一个复杂一些的综合示例,uml类图如下所示,
polint和line都继承自object,同时line还组合使用了point。
#include <iostream> #include <sstream> #include <string> using namespace std; class object { protected: string mname; string minfo; public: object() { mname = "object"; minfo = "null"; } string name() { return mname; } string info() { return minfo; } }; class point : public object { private: int mx; int my; public: point(int x = 0, int y = 0) { ostringstream s; mx = x; my = y; mname = "point"; s << "p(" << mx << ", " << my << ")"; minfo = s.str(); } int x() { return mx; } int y() { return my; } }; class line : public object { private: point mstart; point mend; public: line(point start, point end) { ostringstream s; mstart = start; mend = end; mname = "line"; s << "line from " << mstart.info() << " to " << mend.info(); minfo = s.str(); } point &begin() { return mstart; } point &end() { return mend; } }; int main() { object o; cout << o.name() << endl; cout << o.info() << endl; cout << endl; point p(1, 2); cout << p.name() << endl; cout << p.info() << endl; cout << endl; point start(3, 4); point end(5, 6); line l(start, end); cout << l.name() << endl; cout << l.info() << endl; return 0; }
类似于成员变量有三种访问级别,c++也支持三种不同的继承方式
虽然c++支持三种不同的继承方式,但private和protected继承带来的复杂性远大于实用性,因此在工程中一般推荐使用public继承。
实际上,c++的派生语言(如java、c#)都只支持public继承这一种方式,也变相说明了这一点。
父类和子类都可以定义构造函数,其中子类构造函数必须对继承而来的成员变量进行初始化,初始化的方法有两种:
继承中的构造与析构顺序,在“对象的构造与析构(二)”中已经讲过,不再赘述。
#include <iostream> #include <string> using namespace std; class parent { protected: string ps; int mv; public: parent() { cout << "parent()" << endl; ps = "default"; mv = 0; } parent(string s, int v) { cout << "parent(string s, int v) : " << s << ", " << v << endl; ps = s; mv = v; } ~parent() { cout << "~parent() : " << ps << ", " << mv << endl; } }; class child : public parent { private: string cs; public: /* * 进入child()前,隐式调用parent(),初始化父类成员; * 进入child()后,使用赋值方式,初始化父类成员. */ child() { cout << "child()" << endl; ps = "parent default"; mv = 100; cs = "default"; } /* * 进入child()前,使用初始化列表,显式调用parent(string s, int v),初始化父类成员. */ child(string s) : parent(s, 200) { cout << "child(string s) : " << s << endl; cs = s; } ~child() { cout << "~child() : " << cs << endl; } }; int main() { child c1; child c2("child"); cout << endl; return 0; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论