实验6
#include<iostream> using namespace std; class Base1{ public: Base1(int a,int b) { cout<<"x+y="<<a+b<<endl; } }; class A:public Base1{ public: A(int c,int d):Base1(c,d) { cout<<"x-y="<<c-d<<endl; } }; class B:public Base1{ public: B(int e,int f):Base1(e,f) { cout<<"x*y="<<e*f<<endl; } }; class C:public Base1{ public: C(int g,int h):Base1(g,h) { cout<<"x/y="<<g/h<<endl; } }; int main() { int x,y; cin>>x>>y; A(x,y); B(x,y); C(x,y); return 0; }
#include<iostream> using namespace std; class vehicle{ public: vehicle(int maxspeed,int weidht); ~vehicle(); void run(); void stop(); private: int maxspeed; int weight; }; void vehicle::run(){cout << "run1" <<endl;} void vehicle::stop(){cout << "stop1" <<endl;} vehicle::vehicle(int m,int w):maxspeed(m),weight(w){ cout << "constructing vehicle..." << endl; } vehicle::~vehicle(){ cout << "destructing vehicle..." << endl; } class bicycle:virtual public vehicle{ public: bicycle(int height,int maxspeed1,int weight1); ~bicycle(); private: int height; }; bicycle::bicycle(int h,int m,int w):height(h),vehicle(m,w){ cout << "constructing bicycle..." << endl; } bicycle::~bicycle(){ cout << "destructing bicycle..." << endl; } class motorcar:virtual public vehicle{ public: motorcar(int seatnum,int maxspeed2,int weight2); ~motorcar(); private: int seatnum; }; motorcar::motorcar(int s,int m,int w):seatnum(s),vehicle(m,w){ cout << "constructing motorcar..." << endl; } motorcar::~motorcar(){ cout << "destructing motorcar..." << endl; } class motorcycle:public bicycle,public motorcar{ public: motorcycle(int height,int seatnum1,int maxspeed3,int weight3); ~motorcycle(); }; motorcycle::motorcycle(int h,int s,int m,int w):bicycle(h,m,w),motorcar(s,m,w),vehicle(m,w){ cout << "constructing motorcycle..." <<endl; } motorcycle::~motorcycle(){ cout << "destructing motorcycle..." << endl; } int main(){ int height,seatnum,maxspeed,weight; motorcycle a(height,seatnum,maxspeed,weight); a.run(); a.stop(); return 0; }
#ifndef FACTION_H #define FACTION_H class Fraction { public: Fraction(); //构造函数 Fraction(int t, int b);//重载 Fraction(int t); //重载 void show(); //show函数 void add(Fraction &f1);//加 void min(Fraction &f1);//减 void mul(Fraction &f1);//乘 void div(Fraction &f1);//除 void compare(Fraction f1, Fraction f2);//比较 Fraction operator+(const Fraction &f0) const; Fraction operator-(const Fraction &f0) const; Fraction operator*(const Fraction &f0) const; Fraction operator/(const Fraction &f0) const; private: int top; //分子 int bottom;//分母 }; #endif
#include "fraction.h" #include <iostream> using namespace std; Fraction::Fraction() { top = 0; bottom = 1; } Fraction::Fraction(int t, int b) { top = t; bottom = b; } Fraction::Fraction(int t) { top = t; 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::min(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::show(){ //show函数 cout<<top<<"/"<<bottom<<endl; } void Fraction::compare(Fraction f1, Fraction f2) { //比较 float a,b; a = f1.top * f1.bottom; b = f2.top * f2.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; } } Fraction Fraction::operator+(const Fraction &f0)const { return Fraction(top *f0.bottom + f0.top*bottom,bottom*f0.bottom); } Fraction Fraction::operator-(const Fraction &f0)const { return Fraction(top *f0.bottom - f0.top*bottom,bottom*f0.bottom); } Fraction Fraction::operator*(const Fraction &f0)const { return Fraction(top * f0.top,bottom * f0.bottom); } Fraction Fraction::operator/(const Fraction &f0)const { return Fraction(top * f0.bottom,bottom * f0.top); }
#include<iostream> #include "fraction.h" using namespace std; int main() { Fraction f1; Fraction f2(5); Fraction f3(3, 4); Fraction f4; f4=f1+f2; f4.show(); f4=f1-f2; f4.show(); f4=f3*f2; f4.show(); f4=f3/f2; f4.show(); f2.compare(f2, f3); //比较 return 0; }