C++ 实验六
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; class ABC { public: void abc(int x, int y){ m = x; n = y; } void add() { cout << m + n << endl; } private: int m, n; }; class A :public ABC { public: void a(int x, int y) { abc(x,y); m = x; n = y; } void minus() { cout << m - n << endl; }; private: int m, n; }; class B :public ABC { public: void b(int x, int y) { abc(x, y); m = x; n = y; } void mul() { cout << m * n << endl; }; private: int m, n; }; class C :public ABC { public: void c(int x, int y) { abc(x, y); m = x; n = y; } void divide() { cout << m / n << endl; }; private: int m, n; }; int main() { int a, b; cin >> a >> b; cout << "A" << endl; A t1; t1.a(a, b); t1.add(); t1.minus(); cout << "B" << endl; B t2; t2.b(a, b); t2.add(); t2.mul(); cout << "C" << endl; C t3; t3.c(a, b); t3.add(); t3.divide(); system("pause"); return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; class vehicle{ public: vehicle(int x, int y):maxspeed(x),weight(y){}; void run() { cout << "run" << endl; }; void stop() { cout << "stop" << endl; }; virtual ~vehicle() { cout << "Deleting..." << endl; }; protected: int maxspeed, weight; }; class bicycle:virtual public vehicle{ public: bicycle(int x, int y, int z):vehicle(x,y), height(z){}; virtual ~bicycle() { cout << "Deleting..." << endl; }; protected: int height; }; class motorcar:virtual public vehicle{ public: motorcar(int x, int y, int s) :vehicle(x, y), seatnum(s) {}; virtual ~motorcar() { cout << "Deleting..." << endl; }; protected: int seatnum; }; class motorcycle:public bicycle ,public motorcar { public: motorcycle(int x, int y, int z, int s) :vehicle(x, y), bicycle(x,y,z),motorcar(x,y,s){}; virtual ~motorcycle() { cout << "Deleting..." << endl; }; }; int main() { cout<<"vehicle:"<<endl; vehicle a(200,20); a.run(); a.stop(); cout << "bicycle:" << endl; bicycle b(200,20,1); b.run(); b.stop(); cout << "motorcar:" << endl; motorcar c(200,20,8); c.run(); c.stop(); cout << "motorcycle:" << endl; motorcycle d(200,20,1,8); d.run(); d.stop(); return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef _FRACTION_H_ #define _FRACTION_H_ #include<iostream> using namespace std; class Fraction { public: Fraction(int t, int b); Fraction(int t); Fraction() { top = 0; bottom = 1; } void show() { cout << top << "/" << bottom << endl; }; Fraction operator+(const Fraction &b)const; Fraction operator-(const Fraction &b)const; Fraction operator*(const Fraction &b)const; Fraction operator/(const Fraction &b)const; void com(Fraction x); private: int top; int bottom; }; #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "Fraction.h" #include <iostream> using namespace std; Fraction::Fraction(int t, int b) :top(t), bottom(b) {} Fraction::Fraction(int t) : top(t), bottom(1) {} Fraction Fraction::operator+(const Fraction &b)const{ return Fraction(top * b.bottom + b.top*bottom, bottom * b.bottom); } Fraction Fraction::operator-(const Fraction &b)const{ return Fraction(top * b.bottom - b.top*bottom, bottom * b.bottom); } Fraction Fraction::operator*(const Fraction &b)const{ return Fraction(top * b.bottom, bottom * b.bottom); } Fraction Fraction::operator/(const Fraction &b)const{ return Fraction(top * b.bottom, bottom * b.bottom); } void Fraction::com(Fraction x) { if (top*x.bottom > bottom*x.top) cout << top << "/" << bottom << ">" << x.top << "/" << x.bottom << endl; else cout << top << "/" << bottom << "<" << x.top << "/" << x.bottom << endl; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include "Fraction.h" using namespace std; int main() { Fraction a(1,2),b(3, 4),c; c = a + b; cout << "c = a + b="; c.show(); c = a - b; cout << "c = a - b="; c.show(); c = a * b; cout << "c = a * b="; c.show(); c = a / b; cout << "c = a / b="; c.show(); system("pause"); return 0; }
符号重载over