实验6

第一题


#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;
}

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

posted @ 2018-06-07 23:03  在云深不知处烤兔子  阅读(151)  评论(0编辑  收藏  举报