您的位置:首页 > 其它

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;
}
*/


运行图片:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: