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

实验6

时间:2018-06-06 18:19:14      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:cti   include   otto   namespace   std   opera   tor   图片   turn   

#include<iostream>
using namespace std;
class Base{
	public:
	 Base (int a,int b):x(a),y(b)
	{
		cout<<x+y<<endl;
	}
	private:
		int x,y;
}; 
class A:public Base{
	public:
		A(int a,int b):Base(a,b),x(a),y(b){}
		void min()
		{
			cout<<x-y<<endl;
		}
		private:
		int x,y;
};
class B:public Base{
	public:
		B(int a,int b):Base(a,b),x(a),y(b){}
		void mul()
		{
			cout<<x*y<<endl;
		}
		private:
		int x,y;
};
class C:public Base{
	public:
		C(int a,int b):Base(a,b),x(a),y(b){}
		void div()
		{
			cout<<x/y<<endl;
		}
		private:
		 int x,y;
};
int main()
{
	A a(5,2);
	a.min();
	B b(2,2);
	b.mul();
    C c(6,3);
    c.div();
    return 0;
}

技术分享图片


#include<iostream>
using namespace std;
class vehicle{
	private:
		int maxspeed, weight;
	public:
		vehicle(int m,int w):maxspeed(m),weight(w){
		cout<<maxspeed<<endl;
		cout<<weight<<endl;
		};
		void run()
		{
			cout<<"run"<<endl;
		}
		void stop() 
		{
			cout<<"stop"<<endl;
		}
}; 
class bicycle:virtual public vehicle{
public:
    bicycle(int m,int w,int h):vehicle(m,w),height(h){
	cout<<height<<endl;
	}
    ~bicycle(){}
private:
    int height;
};
class motorcar:virtual public vehicle{
    public: 
        motorcar(int m,int w,int s):vehicle(m,w),seatnum(s){
        cout<<"seatnum"<<endl;
        }
        ~motorcar(){}
    private:
        int 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){}
        ~motorcycle(){}
}; 
int main()
{
	int h,s,m,w;
	motorcycle a(10,20,30,40);
	a.run();
	a.stop();
	return 0;
}

 

  技术分享图片

 

class Fraction {
public:
    Fraction();            
    Fraction(int x, int y);
    Fraction(int x);       
    void show();           
    void add(Fraction f1);
    void sub(Fraction f1);
    void mul(Fraction f1);
    void div(Fraction f1);
    Fraction operator+(const Fraction &f1)const;//分数相加
    Fraction operator-(const Fraction &f1)const;//分数相减
    Fraction operator*(const Fraction &f1)const;//分数相乘
    Fraction operator/(const Fraction &f1)const;//分数相除
    void compare(Fraction f1, Fraction f2);
private:
    int top; int bottom;   
};

  

#include<iostream>
#include"Fraction.h"
using namespace std;
Fraction::Fraction() {      
    top = 0; bottom = 1;
}
Fraction::Fraction(int x, int b) { 
    top = x; bottom = b;
}
Fraction::Fraction(int x) {        
    top = x; 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::sub(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::compare(Fraction f1, Fraction f2)//比较
 {
     float a = float(f1.top) * float(f2.bottom); float b = float(f2.top) / float(f1.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;
      }
  } 
  void Fraction::show(){
      cout<<top<<"/"<<bottom<<endl;
  }
  Fraction Fraction::operator+(const Fraction &f1)const
  {
  	return Fraction(top * f1.bottom + f1.top*bottom,bottom * f1.bottom);
  }
  Fraction Fraction::operator-(const Fraction &f1)const
  {
  	return Fraction(top * f1.bottom - f1.top*bottom,bottom * f1.bottom);
  }
  Fraction Fraction::operator*(const Fraction &f1)const
  {
  	return Fraction(top*f1.top,bottom*f1.bottom);
  }
  Fraction Fraction::operator/(const Fraction &f1)const
  {
  	return Fraction(top*f1.bottom,bottom * f1.top);
  }
  

  

#include<iostream>
#include"Fraction.h"
using namespace std;
int main()
{
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    a.show();
    b.show();
    c.show();
    b.add(c);
    b.sub(c);
    b.mul(c);
    b.div(c);
    a.compare(b,c);
    Fraction l1(4,2);
    Fraction l2(6,3);
    Fraction l3;
    l3=l1+l2;
    l3.show();

    return 0;
}

  技术分享图片

 


  

实验6

标签:cti   include   otto   namespace   std   opera   tor   图片   turn   

原文地址:https://www.cnblogs.com/20178303027zl/p/9145873.html

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