1. 程式人生 > >實驗六

實驗六

運行 pan ace pla bubuko esp eat 位數 code

第一題

//bass.h
//基類 
//point
class point{
    public:
        void setm( double numbm)
        {
            m=numbm;
        }
        void setn( double numbn)
        {
            n=numbn;
        }
        double displayadd()
        {
            cout<<"m+n="<<m+n;
        }
    protected:
        
double m; double n; }; //派生類A A芯片:計算兩位整數的加法(m+n)、計算兩位整數的減法(m-n) class A: public point{ public: double displaysub() { cout<<"m-n="<<m-n; } } ; //派生類B B 芯片:計算兩位整數的加法(m+n)、計算兩位整數的乘法(m*n) class B: public point{ public: double
displaymul() { cout<<"m*n="<<m*n; } }; //派生類C C 芯片:計算兩位整數的加法(m+n)、計算兩位整數的除法(m/n) class C: public point{ public: double displaydiv() { cout<<"m/n="<<m/n; } }; //main #include<iostream> #include"bass.h"
using namespace std; int main() { double m, n; cout<<"m= "; cin>>m; cout<<"n= "; cin>>n; cout<<endl; //A cout<<"A芯片"<<endl; A calculateA; calculateA.setm(m); calculateA.setn(n); calculateA.displayadd(); cout<<endl; calculateA.displaysub(); cout<<endl<<endl; //B cout<<"B芯片"<<endl; B calculateB; calculateB.setm(m); calculateB.setn(n); calculateB.displayadd(); cout<<endl; calculateB.displaymul(); cout<<endl<<endl; //C cout<<"C芯片"<<endl; C calculateC; calculateC.setm(m); calculateC.setn(n); calculateC.displayadd(); cout<<endl; calculateC.displaydiv(); return 0; }

技術分享圖片

第二題

//vehicle.h
class vehicle{
    public:
        vehicle(int setmaxs, int setwei)
        {
            maxspeed = setmaxs;
            weight = setwei;
        }
        
        void run();
        void stop();
    protected:
        int    maxspeed;
        int weight;
};

//bicycle類  新增數據成員高度(height)
class bicycle : virtual public vehicle{
    public:
        bicycle(int setmaxs, int setwei, int sethei):vehicle(setmaxs, setwei), height(sethei){
        }
    protected:
        int height;
};

//motorcar類    新增數據成員座位數(seatnum)
class motorcar : virtual public vehicle{
    public:
        motorcar(int setmaxs, int setwei, int setsea):vehicle(setmaxs, setwei), seatnum(setsea){
        }
    protected:
        int seatnum;
};

//從 bicycle 和 motorcar 派生出摩托車(motorcycle)類
class motorcycle : virtual public bicycle, virtual public motorcar{
    public:
        motorcycle(int setmaxs, int setwei, int sethei, int setsea):
        motorcar(setmaxs, setwei, setsea),
        bicycle(setmaxs, setwei, sethei),
        vehicle(setmaxs, setwei) {}
};

//show.cpp
#include<iostream>
#include"vehicle.h"
using namespace std;
void vehicle::run()
{
    cout<<"run"<<endl;
}
void vehicle::stop()
{
    cout<<"stop"<<endl;
}

//main
#include<iostream>
#include"show.cpp"
using namespace std;
int main()
{
motorcycle motor(1,1,1,1);
motor.run();
motor.stop();
return 0;
}

技術分享圖片

運行後很糟糕的失敗了,這種錯誤我還檢查不出來

由多個類派生出來的motorcycle對值的操作不是很懂,在問過舍友後也還是不怎麽懂,希望有更簡單清晰的告解。

實驗六