实验六

#include<iostream>
using namespace std;

class Base {
public:
	Base(int m,int n):m(m),n(n){}
	void plus();
private:
	int m, n;
};
void Base::plus() {
	cout << "m+n=" << m + n << endl;
}

class A :public Base {
public:
	A(int a,int b):Base(a,b),m1(a),n1(b){}
	void min();
private:
	int m1, n1;
};
void A::min() {
	cout << "m-n=" << m1 - n1 << endl;
}

class B :public Base {
public:
	B(int a, int b) :Base(a, b), m2(a), n2(b) {}
	void mul();
private:
	int m2, n2;
};
void B::mul() {
	cout << "m*n=" << m2 * n2 << endl;
}

class C :public Base {
public:
	C(int a,int b):Base(a,b),m3(a),n3(b){}
	void div();
private:
	int m3, n3;
};
void C::div() {
	cout << "m/n=" << m3 / n3 << endl;
}

int main() {
	Base(2, 1);
	A a(2,1);
	B b(2,1);
	C c(2,1);
	a.plus();
	a.min();
	b.plus();
	b.mul();
	c.plus();
	c.div();
	return 0;
}

  

#include<iostream>
using namespace std;

class vehicle {
public:
	vehicle(int m, int w) :maxspeed(m), weight(w) {}
	~vehicle();
	void run();
	void stop();
private:
	int maxspeed, weight;
};
vehicle::~vehicle() {
	cout << "Function is over" << endl;
}
void vehicle::run() {
	cout << "RUN" << endl;
}
void vehicle::stop() {
	cout << "STOP" << endl;
}

class bicycle :virtual public vehicle {
public:
	bicycle(int m, int w, int h) :vehicle(m, w), height(h) {}
	~bicycle();
private:
	int height;
};
bicycle::~bicycle() {
	cout << "Function is over" << endl;
}

class motorcar :virtual public vehicle {
public:
	motorcar(int m, int w, int s) :vehicle(m, w), seatnum(s) {}
	~motorcar();
private:
	int seatnum;
};
motorcar::~motorcar() {
	cout << "Function is over" << endl;
}

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();
};
motorcycle::~motorcycle(){
	cout << "Funtion is over" << endl;
}

int main() {
	bicycle a(1, 1, 1);
	motorcar b(1, 1, 1);
	motorcycle c(1, 1, 1, 1);
	a.run();
	a.stop();
	b.run();
	b.stop();
	c.run();
	c.stop();
	return 0;
}

  

//fraction.h
#pragma once
class Fraction {
public:
	Fraction(int t, int b) :top(t), bottom(b) {};
	Fraction(int t) :top(t), bottom(1) {};
	Fraction() :top(0), bottom(1) {};
	Fraction operator+(Fraction &f);
	Fraction operator-(Fraction &f);
	Fraction operator*(Fraction &f);
	Fraction operator/(Fraction &f);
	void compare(Fraction &pf1, Fraction &f2);
	void show();
private:
	int top,bottom;
};
//fraction.cpp
#include<iostream>
#include"fraction.h"
using namespace std;
Fraction Fraction::operator+(Fraction &f) {
	Fraction a;
	a.top = top * f.bottom + bottom * f.top;
	a.bottom = bottom * f.bottom;
	return a;
}
Fraction Fraction::operator-(Fraction &f) {
	Fraction a;
	a.top = top * f.bottom - bottom * f.top;
	a.bottom = bottom * f.bottom;
	return a;
}
Fraction Fraction::operator*(Fraction &f) {
	Fraction a;
	a.top = top * f.top;
	a.bottom = bottom * f.bottom;
	return a;
}
Fraction Fraction::operator/(Fraction &f) {
	Fraction a;
	a.top = top * f.bottom;
	a.bottom = bottom * f.top;
	return a;
}
void Fraction::compare(Fraction &f1, Fraction &f2) {
	if (f1.top / f1.bottom>f2.top / f2.bottom)
		cout << "f1>f2" << endl;
	else if (f1.top / f1.bottom<f2.top / f2.bottom)
		cout << "f1<f2" << endl;
	else if (f1.top / f2.bottom == f2.top / f2.bottom)
		cout << "f1=f2" << endl;
}
void Fraction::show() {
	cout << top << "/" << bottom << endl;
}
//ifraction.h
#pragma once
#include<iostream>
#include”fraction.h”
using namespace std;
class iFraction :public Fraction {
public:
	iFraction(int t, int b) :Fraction(t, b),top(t),bottom(b) {};
	iFraction(int t) :Fraction(t),top(t),bottom(1){};
	iFraction() :Fraction(),top(0),bottom(1) {};
private:
	int top, bottom;
};
//main.cpp
#include<iostream>
#include"fraction.h"
#include"ifraction.h"
using namespace std;
int main() {
	Fraction a;
	Fraction b(1);
	Fraction c(1, 2);
	Fraction d, e, f, g;
	d = b + c;
	e = b - c;
	f = b * c;
	g = b / c;
	a.show();
	b.show();
	c.show();
	d.show();
	e.show();
	f.show();
	g.show();
	return 0;
}

  

转载于:https://www.cnblogs.com/nuist20178303037/p/9150965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值