一:
#include<iostream> using namespace std; class basic { public: basic(int a,int b):m(a),n(b){} ~basic(){} void jia() { cout<<m+n<<endl; } private: int m,n; }; class A:public basic { public: A(int a,int b):basic(a,b),m(a),n(b){} ~A(){} void jian() { cout<<m-n<<endl; } private: int m,n; }; class B:public basic { public: B(int a,int b):basic(a,b),m(a),n(b){} ~B(){} void cheng() { cout<<m*n<<endl; } private: int m,n; }; class C:public basic { public: C(int a,int b):basic(a,b),m(a),n(b){} ~C(){} void chu() { cout<<m/n<<endl; } private: int m,n; }; int main() { int i,j; cin>>i>>j; A a(i,j); B b(i,j); C c(i,j); a.jian(); b.cheng(); c.chu(); return 0; }
二:
#include <iostream> using namespace std; class vehicle { public: vehicle(int ,int ); ~vehicle(); void run(); void stop(); private: int maxspeed; int weight; }; vehicle::vehicle(int a,int b):maxspeed(a),weight(b) { cout<<"constructing vehicle"<<endl; cout<<"maxspeed:"<<maxspeed<<endl; cout<<"weight:"<<weight<<endl; } vehicle::~vehicle(){} void vehicle::run() {cout<<"run"<<endl;} void vehicle::stop() {cout<<"stop"<<endl;} class bicycle:virtual public vehicle { public: bicycle(int ,int ,int ); ~bicycle(); private: int height; }; bicycle::bicycle(int a,int b,int c):vehicle(a,b),height(c) { cout<<"constructing bicycle"<<endl; cout<<"height:"<<height<<endl; } bicycle::~bicycle(){} class motorcar:virtual public vehicle { public: motorcar(int ,int ,int ); ~motorcar(); private: int seatnum; }; motorcar::motorcar(int a,int b,int c):vehicle(a,b),seatnum(c) { cout<<"constructing motorcar"<<endl; cout<<"seat:"<<seatnum<<endl; } motorcar::~motorcar(){} class motorcycle:public bicycle,public motorcar { public: motorcycle(int ,int ,int ,int ); motorcycle(); ~motorcycle(); }; motorcycle::motorcycle(int a,int b,int c,int d):vehicle(a,b),bicycle(a,b,c),motorcar(a,b,d){} motorcycle::~motorcycle(){} int main() { motorcycle car(100,100,2,4); car.run(); car.stop(); return 0; }
三:
#include "Fraction.h" #include <iostream> using namespace std; Fraction::Fraction(int i,int j):top(i),bottom(j){} Fraction::Fraction(int i):top(i) { bottom=1; } Fraction::Fraction() { top=0; bottom=1; } Fraction::~Fraction(){} Fraction Fraction::operator+(Fraction &b) { Fraction temp; temp.top=top*b.bottom+b.top*bottom; temp.bottom=bottom*b.bottom; return temp; } Fraction Fraction::operator-(Fraction &b) { Fraction temp; temp.top=top*b.bottom-b.top*bottom; temp.bottom=bottom*b.bottom; return temp; } Fraction Fraction::operator*(Fraction &b) { Fraction temp; temp.top=top*b.top; temp.bottom=bottom*b.bottom; return temp; } Fraction Fraction::operator/(Fraction &b) { Fraction temp; temp.top=top*b.bottom; temp.bottom=bottom*b.top; return temp; } void Fraction::divide(Fraction &a) { cout<<top*a.bottom<<"/"<< bottom * a.top << endl; } void Fraction::compare(Fraction &a) { if(top*a.bottom>bottom*a.top) cout<<top<<"/"<<bottom<<endl; else if(top*a.bottom<bottom*a.top) cout<<a.top<<"/"<<a.bottom<<endl; else if(top*a.bottom==bottom*a.top) cout<<"一样大"<<endl; } void Fraction::input() { cin>>top>>bottom; } void Fraction::output() { cout<<top<<"/"<<bottom<<endl; }
#ifndef Fraction_h #define Fraction_h class Fraction { public: Fraction(int i,int j); Fraction(int i); Fraction(); ~Fraction(); Fraction operator+(Fraction &b); Fraction operator-(Fraction &b); Fraction operator*(Fraction &b); Fraction operator/(Fraction &b); void divide(Fraction &a); void compare(Fraction &a); void input(); void output(); private: int top; int bottom; }; #endif
#include"Fraction1.h" class iFraction:public Fraction { public: iFraction(int x=0,int y=1,int z=0):Fraction(x,y),zheng(z){} void print(); friend iFraction convertF(iFraction &d); private: int zheng; };
#include"iFraction.h" #include<iostream> #include<iomanip> using namespace std; void iFraction::print() { if(top==0) cout<<setw(4)<<zheng<<endl; else if(zheng==0) { cout<<setw(4)<<top<<endl; cout<<setw(4)<<'-'<<endl; cout<<setw(4)<<bottom<<endl; } else { cout<<setw(4)<<top<<endl; cout<<setw(3)<<zheng<<'-'<<endl; cout<<setw(4)<<bottom<<endl; } } iFraction convertF(iFraction &d) { d.simplify(); d.zheng+=d.top/d.bottom; d.top%=d.bottom; return d; }
#include <iostream> #include "iFraction.h" using namespace std; int main() { Fraction a(-6,8); Fraction b(5); Fraction c; cout<<"a="; a.show(); cout<<"b="; b.show(); cout<<"c="; 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(); c=a/b; cout<<"c=a/b="; c.show(); iFraction d(9,6,5); cout<<"d="<<endl; d.print(); d=convertF(d); cout<<"规范化处理"<<endl; d.print(); return 0; }
这是我操作时出现的问题,我也不知到这么解决,我点一下main就会自动新建一个main,然后我写了数据的那个删都删不掉。
五:
这次的实验还是比较简单比较少的(附加是什么?)