标签:
代码:
/* *Copyright (c) 2016, 烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp; *作 者:岳成艳; *完成日期:2015年5月6号; *版 本 号:vc++6.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; }运行测试:
#include <iostream> using namespace std; class A { public: A(){a=0;} A (int i){a=i;} void print(){cout<<a<<" ";} private: int a; }; class B: public A { public: B(){b=0;} B(int i, int j, int k): A(i),aa(j){b=k;} //思考:这3处出现的print,有何区别 void print() //(1) { A::print(); //(2) aa.print(); //(3) cout<<b<<endl; } private: int b; A aa; }; int main() { B test[2]; test[0]=B(1,4,7); test[1]=B(2,5,8); for(int i=0; i<2; i++) test[i].print(); return 0; }
标签:
原文地址:http://blog.csdn.net/haiyanycy/article/details/51334837