第11周阅读程序(1)

来源:互联网 发布:凸优化求解python库 编辑:程序博客网 时间:2024/04/28 13:48

问题及代码:

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:zwj.cpp *作    者:张晴晴 *完成日期:2016年5月10日 *版 本 号:v1.0 * *问题描述:阅读程序,写出运行结果 *输入描述: *程序输出: */#include<iostream>using namespace std;class Data{public :    Data (int i):x(i){cout<<"A";}    ~Data(){cout<<"B";}private :    int x;};class Base{public :    Base(int i ):b1(i){cout<<"C";}    ~Base(){cout<<"D";}private :    int b1;};class Derived:public Base{public:     Derived(int i,int j):Base(i),d1(j){cout<<"E";}     ~Derived(){cout<<"F";}private:        Data d1;};int main(){    Derived obj(1,2);    return 0;}

运行结果:

过程分析:主函数中声明了Derived型的一个对象 obj(1,2),进行构造函数,分别进行Base(1),d1(2)的构造函数,先进行Base(1)的构造输出C,在进行d1的构造输出A,(d1是类Date中的成员函数), Derived(int i,int j):Base(i),d1(j){cout<<"E";}按步骤输出CA后输出E,进行析构函数,先析构Derived(),再析构d1(2),Base(1),析构函数的过程中依次输出FBD,所以最后输出结果的顺序为CAEFBD。

0 0