实验6

一、

//base.h
#ifndef _BASE_H
#define _BASE_H
#include<iostream>
using namespace std;
class base
{
public:
    base(int a, int b) {
        m = a;
        n = b;
    };
    void show() { cout << m + n << endl; }
    int getm() { return m; }
    int getn() { return n; }
private:
    int m;
    int n;
};
#endif
//A.h
#ifndef _A_H
#define _A_h
#include "base.h"
#include <iostream>
using namespace std;
class A :
    public base
{
public:
    A(int m, int n):base(m,n) {
        show();
        cout << m - n << endl;
    }
    
};
#endif
//B.h
#ifndef _B_H
#define _B_H
#include "base.h"
#include <iostream>
using namespace std;
class B :
    public base
{
public:
    B(int m, int n) :base(m, n) {
        show();
        cout << m * n << endl;
    }
    

};
#endif
//C.h
#ifndef _C_H
#define _C_h
#include "base.h"
#include <iostream>
using namespace std;
class C :
    public base
{
public:
    C(int m, int n) :base(m, n) {
        show();
        cout << m / n << endl;
    }


};
#endif
//main.cpp
#include "stdafx.h"
#include <iostream>
#include "A.h"
#include "B.h"
#include "C.h"
#include "base.h"
using namespace std;

int main()
{
    A a(6,3);
    B b(6,3);
    C c(6,3);

    return 0;
}

二、

//vehicle.h
#ifndef _VEHICLE_H
#define _VEHICLE_H
#include<iostream>
using namespace std;
class vehicle
{
public:
    vehicle() {
        maxspeed = 1; weight = 1;
    };
    void run() { cout << "run" << endl; };
    void stop() {
        cout << "stop" << endl;
    };
    ~vehicle() { cout << "析构1" << endl;}
private:
    int maxspeed, weight;
};
class bicycle:
    virtual public vehicle 
{
public:
    bicycle() { height = 10; }
    ~bicycle() { cout << "析构2" << endl; }
private:
    int height;
};
class motocar :
    virtual public vehicle
{
public:
    motocar() { seatnum = 40; }
    ~motocar() { cout << "析构3" << endl; }
private:
    int seatnum;
};
class motocycle :
    public motocar, public bicycle
{
public:
    motocycle() { cout << "motocyle的构造函数" << endl; }
    ~motocycle() { cout << "析构4" << endl; }
};
#endif

 

//main.cpp
#include "stdafx.h"
#include <iostream>
#include "vehicle.h"
using namespace std;


int main()
{
    motocycle a;
    a.run();
    a.stop();

    return 0;
}

这题中不是应该先调用motocar再调用bicycle吗?为什么析构函数却是先执行2再执行3?

posted @ 2018-06-03 19:07  KrnFx  阅读(369)  评论(1编辑  收藏  举报