类的继承与组合

类的继承与组合

对象(Object)是类(Class)的一个实例(Instance)。如果将对象比作房子,那么 类就是房子的设计图纸。所以面向对象设计的重点是类的设计,而不是对象的设计。 对于 C++程序而言,设计孤立的类是比较容易的,难的是正确设计基类及其派生类。

本章仅仅论述“继承”(Inheritance)和“组合”(Composition)的概念。 注意,当前面向对象技术的应用热点是 COM 和 CORBA,这些内容超出了 C++教材 的范畴,请阅读 COM 和 CORBA 相关论著。

 

 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 int main(int argc, char** argv) {
 6     //max()函数原型声明语句
 7     float max(float,float);
 8     
 9     //变量声明语句
10     float a,b,Max;
11 
12     //输入参数并计算
13     cout<<"a=";
14     cin>>a;
15     cout<<"b=";
16     cin>>b;
17     Max=max(a,b);     //调用max()函数 
18     cout<<"max("<<a<<","<<b<<")="<<Max<<endl;
19 
20 
21     return 0;
22 }
23 //定义max()函数
24 float max(float x,float y)     //max()返回值类型为浮点型
25 {
26     float z;
27     z=(x>y)?x:y;
28     return(z);
29 }
30 
31     

 

posted @ 2018-08-02 13:06  borter  阅读(117)  评论(0编辑  收藏  举报