实验6

#include
using namespace std;
class Base{
	public:
	 Base (int a,int b):x(a),y(b)
	{
		cout<



#include
using namespace std;
class vehicle{
	private:
		int maxspeed, weight;
	public:
		vehicle(int m,int w):maxspeed(m),weight(w){
		cout< 
  

  

 

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

  

#include
#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)