第十二周 阅读程序 虚基类 中的继承

/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:d.cpp
*作    者:张旺华
*完成日期:2015年6月1日
*版 本 号:v1.0
*/
#include <iostream>
using namespace std;
class Base
{
public:
    Base(char i) { cout<<"Base constructor. --"<<i<<endl; }
};
class Derived1:virtual public Base
{
public:
    Derived1(char i,char j):Base(i)
    {
        cout<<"Derived1 constructor. --"<<j<<endl;
    }
};
class Derived2:virtual public Base
{
public:
    Derived2(char i,char j):Base(i)
    {
        cout<<"Derived2 constructor. --"<<j<<endl;
    }
};
class MyDerived:public Derived1,public Derived2
{
public:
    MyDerived(char i,char j,char k,char l,char m,char n,char x): Derived2(i,j), Derived1(k,l), Base(m), d(n)
    {
        cout<<"MyDerived constructor. --"<<x<<endl;
    }
private:
    Base d;
};
int main()
{
    MyDerived obj('A','B','C','D','E','F','G');
    return 0;
}

运行结果:

知识点运用及学习心得:

通过这个程序,我们可以了解在执行虚基类继承的时候,首先是构造函数,构造函数是最先执行最底层的构造函数也就是base类,在逐步上升。

时间: 2024-08-26 03:27:17

第十二周 阅读程序 虚基类 中的继承的相关文章

第十二周阅读程序3:虚基类的构造函数

问题及代码: #include <iostream> using namespace std; class Base { public: Base(char i) { cout<<"Base constructor. --"<<i<<endl; } }; class Derived1:virtual public Base //虚基类 { public: Derived1(char i,char j):Base(i) { cout<

第十二周阅读程序4:虚拟基类-同名数据成员

问题及代码: (1)程序执行后,运行结果是多少? (2)将程序中有注释的两条语句修改为注释的内容,即将"class B:public A {};"修改为"class B:virtual public A{};","class C:public A {}; "修改为"class C:virtual public A{};",重新编译运行程序,程序的运行结果又是多少? #include<iostream> using

第十二周 阅读项目 (4)虚基类多重继承数据理解

<span style="font-size:18px;">/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年6月1日 *版 本 号:v1.0 */ #include<iostream> using namespace std; class A { public: int n; }; class B:virtual public A

第十二周 阅读项目 (5)

阅读下面类的定义,请说出在测试函数中不同情况的调用产生的结果 [cpp] view plaincopyprint? #include <iostream> using namespace std; class A { protected: int a,b; public: A(int aa, int bb):a(aa), b(bb) {} void printA() { cout<<"a: "<<a<<"\tb: "&

C++ 虚基类 派生与继承

在学习设计模式时我就有一个疑问,关联和继承除了用法上的区别,好像在内存上并没有什么区别,继承也是父类作为了子类的元素(内存上),关联也是这样.而且关联好像更占内存一些.这就是设计模式里问题了“依赖倒转原则”. 继承分为public继承,protect继承,private继承 public:父类中的public,protected成员到了派生类中属性不变. protected:父类中的public,protected成员到了派生类中,都变为protected成员. private:父类中的publ

第十五周阅读程序1:二进制读写

问题及代码: 阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a; ofstream outfile("f1.dat&quo

第十六周阅读程序:异常处理和命名空间

(1) #include <iostream > using namespace std; int a[10]= {1,2, 3, 4, 5, 6, 7, 8, 9, 10}; int fun( int i); int main() { int i ,s=0; for( i=0; i<=10; i++) { try { s=s+fun(i); } catch(int) { cout<<"数组下标越界!"<<endl; } } cout<&

第十二周 阅读项目 多重继承中数据关系

<span style="font-size:18px;">/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年6月1日 *版 本 号:v1.0 */ #include<iostream> using namespace std; class A { public: int n; }; class B:public A {}; //

第十二周项目2:摩托车继承自行车和机动车

问题及代码: #include <iostream> #include <conio.h> #include <windows.h> using namespace std; enum vehicleStaus {rest, running}; //车辆状态:泊车.行进 class vehicle //车辆类 { protected: int maxSpeed; //最大车速 int currentSpeed; //当前速度 int weight; //车重 vehic