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

实验6

时间:2018-06-08 00:47:27      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:operator   cycle   opera   文件   on()   派生   实验   汽车   code   

第一题


#include<iostream> 
using namespace std;
class Base1{     //基类base1--加法运算 
    public:
        Base1(double a,double b):m(a),n(b){cout<<"和為:"<<m+n<<endl;} //声明 
        private:
            double m,n;
};
class A:public Base1{  //派生类A--既要继承基类的加法,又要开拓减法 
    public:
        A(double a,double b):Base1(a,b),m(a),n(b){}; //对基类的继承 
        void jian(){  //成员函数--减法运算 
            cout<<"减为:"<<m-n<<endl; 
        }
    private:
    double m,n; 
    
};
class B:public Base1{
    public:
        B(double a,double b):Base1(a,b),m(a),n(b){};
        void mul(){ //成员函数--乘法 
            cout<<"乘为:"<<m*n<<endl; 
        }
    private:
        double m,n;
};
class C:public Base1{
    public:
        C(double a,double b):Base1(a,b),m(a),n(b){};
        void div(){  //成员函数--除法 
            cout<<"除为:"<<m/n<<endl; 
        }
    private:
        double m,n; 
};
int main(){
    double a,b;
    cout<<"输入m,n:"<<endl;
    cin>>a>>b;
    cout<<endl;
    cout<<"启动芯片A:"<<endl;
    A a1(a,b);
    a1.jian();  //通过外在对象访问成员函数 
    cout<<"启动芯片B:"<<endl;
    B b1(a,b);
    b1.mul();
    cout<<"启动芯片C:"<<endl;
    C c1(a,b);
    c1.div();
    return 0;
}

技术分享图片
第二题
技术分享图片

#include<iostream> 
using namespace std;
class vehicle{
    public:
        vehicle(int m,int w):maxspeed(m),weight(w){
            cout<<"最大速度:"<<maxspeed<<"  重量:"<<weight<<endl; 
        }
        void run(){
            cout<<"车启动了"<<endl; 
        }
        void stop(){
            cout<<"车停下了"<<endl; 
        }
    private:
        int maxspeed,weight;
};
class bicycle:virtual public vehicle{
    public:
        bicycle(int m,int w,int h):vehicle(m,w),height(h){
        cout<<"  高度:"<<height<<endl; 
        } 
    private:
        int maxspeed,weight,height;
};
class motorcar:virtual public vehicle{
    public:
        motorcar(int m,int w,int s):vehicle(m,w),seatnum(s){
        cout<<"  座位数:"<<seatnum<<endl; 
    }
    private:
        int maxspeed,weight,seatnum;
};
class motorcycle:public bicycle,public motorcar{
    public:
        motorcycle(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){}//(1) 
        private:
        int maxspeed,weight,height,seatnum;
};
int main(){
    cout<<"自行车"<<endl; 
    bicycle b(10,50,15);
    b.run();
    b.stop();
    cout<<"汽车"<<endl; 
    motorcar mr(30,300,6);
    mr.run();
    mr.stop();
    cout<<"摩托车"<<endl;
    motorcycle me(50,150,26,3);
    me.run();
    me.stop();
    return 0;
}
//(1)处之前写成 vehicle(m,w),height(h),seatnum(s)就不对。 

技术分享图片
第三题
(1)

 using namespace std;
 int g,h;
 class Fraction{
    public:
        Fraction():top(0),bottom(1){};
        Fraction(int t,int b):top(t),bottom(b){};
        Fraction(int t):top(t),bottom(1){};
        Fraction(Fraction &f0);
        void show(){
        cout<<top<<"/"<<bottom<<endl;};
        void operator+(Fraction &f0);
        void operator-(Fraction &f0);
        void operator*(Fraction &f0);
        void operator/(Fraction &f0);
    private:
         int top;
        int bottom;
 };
    void Fraction::operator+(Fraction &f0){
    g=top;h=bottom;     
    top=top*f0.bottom+f0.top*bottom;
    bottom=bottom*f0.bottom;
    show();
}
void Fraction::operator-(Fraction &f0){
    top=g;bottom=h; 
    top=top*f0.bottom-f0.top*bottom;
    bottom=bottom*f0.bottom;
    show();
}
void Fraction::operator*(Fraction &f0){
    top=g;bottom=h; 
    top=top*f0.top;
    bottom=bottom*f0.bottom;
    show();
}
void Fraction::operator/(Fraction &f0){
    top=g;bottom=h; 
    top=top*f0.bottom;
    bottom=bottom*f0.top;
    show();
}
int main(){
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    a.show();
    b.show();
    c.show();
    b.operator+(c);
    b.operator-(c);
    b.operator*(c);
    b.operator/(c);
    return 0;
}

技术分享图片

用多文件一直没成功过。。。

实验6

标签:operator   cycle   opera   文件   on()   派生   实验   汽车   code   

原文地址:https://www.cnblogs.com/lixiaoyu-Judy/p/9152435.html

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