实验6
Posted zszssy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验6相关的知识,希望对你有一定的参考价值。
1
base.h
class base{ public: void add(); tog(int m0,int n0); private: int m; int n; }; class A:public base{ public: void min(); A(int m0,int n0); private: int m; int n; }; class B:public base{ public: void mul(); B(int m0,int n0); private: int m; int n; }; class C:public base{ public: void div(); C(int m0,int n0); private: int m; int n; };
base.cpp
#include"base.h" #include<iostream> using namespace std; base::tog(int m0,int n0){ m=m0; n=n0; } void base::add(){ cout<<"m+n="<<m+n<<endl; }; A::A(int m0,int n0){ tog(m0,n0); m=m0; n=n0; } void A::min(){ cout<<"m-n="<<m-n<<endl; }; B::B(int m0,int n0){ tog(m0,n0); m=m0; n=n0; } void B::mul(){ cout<<"m*n="<<m*n<<endl; }; C::C(int m0,int n0){ tog(m0,n0); m=m0; n=n0; } void C::div(){ cout<<"m/n="<<m/n<<endl; };
main.cpp
#include <iostream> #include"base.h" using namespace std; int main() { A A(3,2); A.add(); A.min(); B B(3,4); B.add(); B.mul(); C C(4,2); C.add(); C.div(); return 0; }
截图
2
vehicle.h
class vehicle{ public: void stop(); void run(); vehicle(int max,int wei); private: int maxspeed; int weight; }; class bicycle:virtual public vehicle{ public: bicycle(int max,int wei,int hei); private:int height; }; class motorcar:virtual public vehicle{ public: motorcar(int max,int wei,int sea); private:int seatnum; }; class motorcycle:public bicycle,public motorcar{ public: motorcycle(int max,int wei,int hei,int sea); };
vehicle.cpp
#include"vehicle.h" #include<iostream> using namespace std; vehicle::vehicle(int max,int wei):maxspeed(max),weight(wei){ cout<<"maxspeed"<<maxspeed<<endl; cout<<"weight"<<weight<<endl; } void vehicle::run(){ cout<<"run"<<endl; } void vehicle::stop(){ cout<<"stop"<<endl; } bicycle::bicycle(int max,int wei,int hei):vehicle(max,wei),height(hei){ cout<<"height"<<height<<endl; } motorcar::motorcar(int max,int wei,int sea):vehicle(max,wei),seatnum(sea){ cout<<"seatnum"<<seatnum<<endl; } motorcycle::motorcycle(int max, int wei, int hei, int sea):vehicle(max,wei),bicycle(max,wei,hei),motorcar(max,wei,sea){}
main.cpp
#include <iostream> #include"vehicle.h" using namespace std; int main() { motorcycle M(100,80,10,5); M.run(); M.stop(); return 0; }
截图
3
Fraction.h
#include<iostream> class Fraction{ public: Fraction(int t,int b); Fraction(int t); Fraction(); void show(); int zc(int t,int b); void simplify(); Fraction operator+(Fraction &b); Fraction operator-(Fraction &b); Fraction operator*(Fraction &b); Fraction operator/(Fraction &b); protected: int top; int bottom; };
Fraction.cpp
#include"Fraction.h" #include<iostream> #include<cmath> using namespace std; Fraction::Fraction(int t,int b){ top=t; bottom=b; } Fraction::Fraction(int t){ top=t; bottom=1; } Fraction::Fraction(){ top=0; bottom=1; } int Fraction::zc(int t,int b){ if(t%b==0) return b; else zc(b,t%b); } void Fraction::simplify(){ if(bottom<0){ top=-top; bottom=-bottom; } int z=zc(abs(top),abs(bottom)); top=top/z; bottom=bottom/z; } void Fraction::show(){ cout<<top<<"/"<<bottom<<endl; } Fraction Fraction::operator+(Fraction &b){return Fraction(top*b.bottom+b.top*bottom,bottom*b.bottom);} Fraction Fraction::operator-(Fraction &b){return Fraction(top*b.bottom-b.top*bottom,bottom*b.bottom);} Fraction Fraction::operator*(Fraction &b){return Fraction(top*b.top,bottom*b.bottom);} Fraction Fraction::operator/(Fraction &b){return Fraction(top*b.bottom,bottom*b.top);}
iFraction.h
#include"Fraction.h" class iFraction:public Fraction{ private: int i; public: iFraction(int t=0,int b=1,int z=0):Fraction(t,b),i(z){} void print(); friend iFraction convertF(iFraction &p); };
iFraction.cpp
#include"iFraction.h" #include<iostream> using namespace std; void iFraction::print(){ if(top==0) cout<<i<<endl; else if(i==0){ cout<<top<<endl; cout<<"-"<<endl; cout<<bottom<<endl; } else{ cout<<" "<<top<<endl; cout<<i<<"-"<<endl; cout<<" "<<bottom<<endl; } } iFraction convertF(iFraction &p){ p.simplify(); p.i+=p.top/p.bottom; p.top%=p.bottom; return p; }
main.cpp
#include <iostream> #include "iFraction.h" using namespace std; int main(){ Fraction a(4,8); cout<<"a="; a.show(); Fraction b(2); cout<<"b="; b.show(); Fraction c; cout<<"c="; c.show(); c=a+b; cout<<"a+b="; c.show(); c=a-b; cout<<"a-b="; c.show(); c=a*b; cout<<"a*b="; c.show(); c=a/b; cout<<"a/b="; c.show(); iFraction d(9,6,2); cout<<"d="<<endl; d.print(); d=convertF(d); cout<<"convertF d="<<endl; d.print(); return 0; }
截图
以上是关于实验6的主要内容,如果未能解决你的问题,请参考以下文章
20155201 李卓雯 《网络对抗技术》实验一 逆向及Bof基础