码迷,mamicode.com
首页 > 其他好文 > 详细

实验六

时间:2018-06-08 00:58:12      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:bsp   成员   实验   失败   http   技术分享   cin   The   sub   

第一题

//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对值的操作不是很懂,在问过舍友后也还是不怎么懂,希望有更简单清晰的告解。

 

实验六

标签:bsp   成员   实验   失败   http   技术分享   cin   The   sub   

原文地址:https://www.cnblogs.com/cppnewcomer/p/9153174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!