实验6

#include<iostream>
using namespace std;

class Base1{
	public:
		Base1(int a,int b)
		{
			cout<<"x+y="<<a+b<<endl;
		}
};
	
class A:public Base1{
	public:
		A(int c,int d):Base1(c,d)
		{
			cout<<"x-y="<<c-d<<endl;
		}
};
	
class B:public Base1{
	public:
		B(int e,int f):Base1(e,f)
		{
			cout<<"x*y="<<e*f<<endl;
		}
};
	
class C:public Base1{
	public:
		C(int g,int h):Base1(g,h)
		{
			cout<<"x/y="<<g/h<<endl;
		}
};

int main()
{
    int x,y;
    cin>>x>>y;
    A(x,y);
    B(x,y);
    C(x,y);
	return 0;
}

  

 

#include<iostream>
using namespace std;

class vehicle{
	public:
	    vehicle(int maxspeed,int weidht);
	    ~vehicle();
		void run();
		void stop();
	private:
		int maxspeed;
		int weight;
}; 
void vehicle::run(){cout << "run1" <<endl;}
void vehicle::stop(){cout << "stop1" <<endl;}
vehicle::vehicle(int m,int w):maxspeed(m),weight(w){
    cout << "constructing vehicle..." << endl;
}
vehicle::~vehicle(){
    cout << "destructing vehicle..." << endl;
}

class bicycle:virtual public vehicle{
	public:
		bicycle(int height,int maxspeed1,int weight1);
        ~bicycle();
    private:
		int height;
};
bicycle::bicycle(int h,int m,int w):height(h),vehicle(m,w){
    cout << "constructing bicycle..." << endl;
}
bicycle::~bicycle(){
    cout << "destructing bicycle..." << endl;
}

class motorcar:virtual public vehicle{
	public:
		motorcar(int seatnum,int maxspeed2,int weight2);
        ~motorcar();
    private:
		int seatnum;
};
motorcar::motorcar(int s,int m,int w):seatnum(s),vehicle(m,w){
    cout << "constructing motorcar..." << endl;
}
motorcar::~motorcar(){
    cout << "destructing motorcar..." << endl;
}

class motorcycle:public bicycle,public motorcar{
	public:
		motorcycle(int height,int seatnum1,int maxspeed3,int weight3);
        ~motorcycle();
};
motorcycle::motorcycle(int h,int s,int m,int w):bicycle(h,m,w),motorcar(s,m,w),vehicle(m,w){
    cout << "constructing motorcycle..." <<endl;
}
motorcycle::~motorcycle(){
    cout << "destructing motorcycle..." << endl;
}

int main(){
    int height,seatnum,maxspeed,weight;
    motorcycle a(height,seatnum,maxspeed,weight);
    a.run();
    a.stop();
    return 0;
}

  

 

#ifndef FACTION_H
#define FACTION_H

class Fraction {
    public:    
        Fraction();            //构造函数
        Fraction(int t, int b);//重载
        Fraction(int t);       //重载
        void show();           //show函数
        void add(Fraction &f1);//加
        void min(Fraction &f1);//减
        void mul(Fraction &f1);//乘
        void div(Fraction &f1);//除
        void compare(Fraction f1, Fraction f2);//比较 
        Fraction operator+(const Fraction &f0) const;
        Fraction operator-(const Fraction &f0) const;
        Fraction operator*(const Fraction &f0) const;
        Fraction operator/(const Fraction &f0) const;
    private:
        int top;   //分子 
        int bottom;//分母 
};

#endif

  

#include "fraction.h"
#include <iostream>
using namespace std;

Fraction::Fraction() {     
    top = 0; bottom = 1;
}
Fraction::Fraction(int t, int b) { 
    top = t; bottom = b;
}
Fraction::Fraction(int t) {        
    top = t; bottom = 1;
}
 void Fraction::add(Fraction &f1) {   //加法
     Fraction f2;
     f2.top = top * f1.bottom + f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.show();
}
 void Fraction::min(Fraction &f1) {    //减法
     Fraction f2;
     f2.top = top * f1.bottom - f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.show();
 }
 void Fraction::mul(Fraction &f1) {    //乘法
     Fraction f2;
     f2.top =top*f1.top;
     f2.bottom =bottom*f1.bottom;
     f2.show();
 }
 void Fraction::div(Fraction &f1) {    //除法
     Fraction f2;
     f2.top =top*f1.bottom;
     f2.bottom = bottom * f1.top;
     f2.show();
 }
 void Fraction::show(){          //show函数
    cout<<top<<"/"<<bottom<<endl;
}
void Fraction::compare(Fraction f1, Fraction f2) {    //比较
     float a,b;
     a = f1.top * f1.bottom; 
     b = f2.top * f2.bottom;
     if (a <= b) { 
     cout << f1.top << "/" << f1.bottom << "<="; 
     f2.show(); cout << endl; 
     }
     if (a > b) { 
     cout << f1.top << "/" << f1.bottom << ">"; 
     f2.show(); cout << endl;
     }
 } 
Fraction Fraction::operator+(const Fraction &f0)const {   
    return Fraction(top *f0.bottom + f0.top*bottom,bottom*f0.bottom);
}
Fraction Fraction::operator-(const Fraction &f0)const {            
    return Fraction(top *f0.bottom - f0.top*bottom,bottom*f0.bottom);
}
Fraction Fraction::operator*(const Fraction &f0)const {    
    return Fraction(top * f0.top,bottom * f0.bottom);
}
Fraction Fraction::operator/(const Fraction &f0)const {      
    return Fraction(top * f0.bottom,bottom * f0.top);
}

  

#include<iostream>
#include "fraction.h"
using namespace std;

int main()
{
    Fraction f1;
    Fraction f2(5);
    Fraction f3(3, 4);
    Fraction f4;
    f4=f1+f2;
    f4.show();
    f4=f1-f2;
    f4.show();
	f4=f3*f2;
    f4.show();
	f4=f3/f2;
    f4.show();   
    f2.compare(f2, f3);  //比较
    return 0;
}

  

 

转载于:https://www.cnblogs.com/Fgw520/p/9145795.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值