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

实验6

时间:2018-06-05 21:18:55      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:code   技术分享   muti   const   void   src   inpu   tom   cti   

1

#include<iostream>
using namespace std;
class chip{
    protected:
        int m;
        int n;
        double result;
    public:
        chip(int a,int b,int c=0):m(a),n(b),result(c){}
        void addition(){
            cout<<m<<"+"<<n<<"=";
            result=m+n;
            cout<<result<<endl;
        }
};
class chipA:public chip{
    public:
        chipA(int a,int b,int c=0):chip(a,b,c){}
        void subraction(){
            cout<<m<<"-"<<n<<"=";
            result=m-n;
            cout<<result<<endl;
        }
};
class chipB:public chip{
    public:
        chipB(int a,int b,int c=0):chip(a,b,c){}
        void mutilp(){
            cout<<m<<"*"<<n<<"="; 
            result=m*n;
            cout<<result<<endl; 
        }

};
class chipC:public chip{
    public:
        chipC(int a,int b,int c=0):chip(a,b,c){}
        void division(){
            cout<<m<<"/"<<n<<"=";
            result=(double)m/(double)n;
            cout<<result<<endl;
        }

};
int main(){
    chipA chip1(1,2);
    chip1.addition();
    chip1.subraction();
    chipB chip2(3,4);
    chip2.mutilp();
    chipC chip3(5,6);
    chip3.division();
    return 0; 
    
}

技术分享图片

2

#include<iostream>
using namespace std;
class vehicle{
    protected:
        int maxspeed;
        int weight;
    public:
        vehicle(int a,int b):maxspeed(a),weight(b){ }
        void run(){
            cout<<"run"<<endl;
        }
        void stop(){
            cout<<"stop"<<endl;
        }
        ~vehicle(){}
};
class bicycle:virtual public vehicle{
    protected:
        int height;
    public:
        bicycle(int a,int b,int c):vehicle(a,b),height(c){}
        void run(){
            cout<<"run"<<endl<<"height:"<<height<<endl;
        }
        void stop(){
            cout<<"stop"<<endl;
        ~bicycle(){}
};
class motorcar:virtual public vehicle{
    protected:
        int seatnum;
    public:
        motorcar(int a,int b,int c):vehicle(a,b),seatnum(c){}
        void run(){
            cout<<"run"<<endl<<"seatnum:"<<seatnum<<endl;
        }
        void stop(){
            cout<<"stop"<<endl;
        ~motorcar(){}
}; 
class motorcycle:public bicycle,public motorcar{
    public:
        motorcycle(int a,int b,int c,int d):vehicle(a,b),bicycle(a,b,c),motorcar(a,b,d){}
        void run(){
            cout<<"run"<<endl;
            cout<<"maxspeed:"<<maxspeed<<endl;
            cout<<"height:"<<height<<endl;
            cout<<"seatnum:"<<seatnum<<endl;
        }
        void stop(){
            cout<<"stop"<<endl;
        ~motorcycle(){}
};

int main(){
vehicle a(100,10);
a.vehicle::run();
a.vehicle::stop();
bicycle b(100,10,10);
b.bicycle::run();
b.bicycle::stop();
motorcar c(100,10,10);
c.motorcar::run();
c.motorcar::stop();
motorcycle d(100,10,10,10);
d.motorcycle::run();
d.motorcycle::stop
    return 0;
}

 运行失败。。不知道为什么

3

fraction.h

#ifndef FRACTION_H
#define FRACTION_H
class fraction
{
public:
    fraction(int a, int b);
    fraction(int a);
    fraction();
    ~fraction();
    fraction operator+(const fraction &f1);
    fraction operator-(const fraction &f1);
    fraction operator*(const fraction &f1);
    fraction operator/(const fraction &f1);
    void compare(fraction &f1);
    void input();
    void output();

protected:
    int top;
    int bottom;
};
#endif

 fraction.cpp

#include"fraction.h"
#include<iostream>
#include<cmath>
using namespace std;
Fraction Fraction::operator+(const Fraction &f1){
    return Fraction(top*f1.bottom+f1.top*bottom,f1.bottom*bottom);
}
Fraction Fraction::operator-(const Fraction &f1){
    return Fraction(top*f1.bottom-f1.top*bottom,bottom*f1.bottom);
}
Fraction Fraction::operator*(const Fraction &f1){
    return Fraction(top*f1.top,bottom*f1.bottom);  
}
Fraction Fraction::operator/(const Fraction &f1){
    return Fraction(top*f1.bottom,bottom*f1.top);
}

void Fraction::output(){
    int t,x,y;
    if(abs(top)>abs(bottom))
        t=abs(bottom);
    else
        t=abs(top);
    for(int i=1;i<t;i++){
        if(top%i==0&&bottom%i==0){
            top/=i;
            bottom/=i;
            i=1;
        }
    }
    if(bottom<0){
        top*=-1;
        bottom*=-1;
    }
    cout<<top<<"/"<<bottom<<endl; 
}

后面不会了。。

实验6

标签:code   技术分享   muti   const   void   src   inpu   tom   cti   

原文地址:https://www.cnblogs.com/artistqun/p/9138862.html

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