11-阅读程序2
2013-05-12 09:52
197 查看
/* * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 文件名称:11-阅读程序2 * 作 者:孙红蕾 * 完成日期:2013 年 5 月 10 日 * 版 本 号:v1.0 * * 输入描述: * 问题描述:先分析程序的执行结果,在上机时运行程序进行对照,再通过单步执行跟踪程序 * 的运行,达到理解基类、派生类中构造函数、析构函数执行过程的目的。 * 程序输出: * 算法设计:略 */ #include <iostream> using namespace std; class Part //部件类 { public: Part(); Part(int i); ~Part(); private: int val; }; class Whole: public Part { public: Whole(); Whole(int,int,int,int); ~Whole(); private: Part one; Part two; int data; }; Part::Part() { val=0; cout<<"The default constructor of part was called "<<val<<endl; } Part::Part(int i) { val=i; cout<<"The constructor of part was called "<<val<<endl; } Part::~Part() { cout<<"The destructor of part was called "<<val<<endl; } Whole::Whole() { data=0; cout<<"The default constructor of whole was called "<<data<<endl; } Whole::Whole(int p, int i,int j,int k):Part(p), two(i),one(j),data(k) { cout<<"The constructor of whole was called "<<data<<endl; } Whole::~Whole() { cout<<"The destructor of whole was called "<<data<<endl; } int main() { Whole w1; Whole w2(1,2,3,4); return 0; } /* 如果用vs2008,将上面的main()函数删除,换作下面的两个函数 void f() { Whole w1; Whole w2(1,2,3,4); } int main() { f(); system("pause"); return 0; } */
运行图片:
相关文章推荐
- 11-阅读程序1
- 第14周项目6-阅读程序(10,11)
- 第十四章项目6-阅读程序(4、5、6、7、8、9、10、11)
- 第十四周项目六 阅读程序(10、11)
- 第十六周阅读程序4
- 大一第二学期第5周【程序阅读(2)】
- 第五周 课后实践--阅读程序(3,4,5,6)
- 阅读程序
- 第15周上机实践项目2——阅读程序(2)
- vxworks下的串口测试程序 分类: vxWorks 2014-04-22 14:15 657人阅读 评论(0) 收藏
- 第16周阅读程序——2(2)
- 第十五周上机项目2--(2) 阅读程序
- 第十五周项目二-阅读程序(2)
- 第十五周项目二 阅读程序(1)
- 第六周阅读程序四
- 飛飛(七十)阅读程序 好玩好玩
- 第十五周项目二——阅读程序
- 第十三周阅读程序
- H面试程序(11): 判断字符串是否包含子串问题
- 第六周程序阅读(2)