若在逻辑上 A 是 B 的“一部分”(a part of)

若在逻辑上 A 是 B 的“一部分”(a part of) ,则不允许 B 从 A 派生, 而是要用 A 和其它东西组合出 B。 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 
 6 //定义名为ex_class的类模板
 7 template <class T>  class ex_class
 8 {
 9     T value;
10 public:
11     ex_class(T v) { value=v; }
12     void set_value(T v) { value=v; }
13     T get_value(void) {return value;}
14 };
15 
16 int main(int argc, char** argv) {
17         //测试int类型数据
18     ex_class <int> a(5),b(10);
19     cout<<"a.value:"<<a.get_value()<<endl;
20     cout<<"b.value:"<<b.get_value()<<endl;
21 
22     //测试char类型数据
23     ex_class <char> ch('A');
24     cout<<"ch.value:"<<ch.get_value()<<endl;
25     ch.set_value('a');
26     cout<<"ch.value:"<<ch.get_value()<<endl;
27 
28     //测试double类型数据
29     ex_class <double> x(5.5);
30     cout<<"x.value:"<<x.get_value()<<endl;
31     x.set_value(7.5);
32     cout<<"x.value:"<<x.get_value()<<endl;
33     return 0;
34 }
原文地址:https://www.cnblogs.com/borter/p/9417878.html