实验6
Posted 20178303027zl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验6相关的知识,希望对你有一定的参考价值。
#include<iostream> using namespace std; class Base{ public: Base (int a,int b):x(a),y(b) { cout<<x+y<<endl; } private: int x,y; }; class A:public Base{ public: A(int a,int b):Base(a,b),x(a),y(b){} void min() { cout<<x-y<<endl; } private: int x,y; }; class B:public Base{ public: B(int a,int b):Base(a,b),x(a),y(b){} void mul() { cout<<x*y<<endl; } private: int x,y; }; class C:public Base{ public: C(int a,int b):Base(a,b),x(a),y(b){} void div() { cout<<x/y<<endl; } private: int x,y; }; int main() { A a(5,2); a.min(); B b(2,2); b.mul(); C c(6,3); c.div(); return 0; }
#include<iostream> using namespace std; class vehicle{ private: int maxspeed, weight; public: vehicle(int m,int w):maxspeed(m),weight(w){ cout<<maxspeed<<endl; cout<<weight<<endl; }; void run() { cout<<"run"<<endl; } void stop() { cout<<"stop"<<endl; } }; class bicycle:virtual public vehicle{ public: bicycle(int m,int w,int h):vehicle(m,w),height(h){ cout<<height<<endl; } ~bicycle(){} private: int height; }; class motorcar:virtual public vehicle{ public: motorcar(int m,int w,int s):vehicle(m,w),seatnum(s){ cout<<"seatnum"<<endl; } ~motorcar(){} private: int seatnum; }; 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(){} }; int main() { int h,s,m,w; motorcycle a(10,20,30,40); a.run(); a.stop(); return 0; }
class Fraction { public: Fraction(); Fraction(int x, int y); Fraction(int x); void show(); void add(Fraction f1); void sub(Fraction f1); void mul(Fraction f1); void div(Fraction f1); Fraction operator+(const Fraction &f1)const;//分数相加 Fraction operator-(const Fraction &f1)const;//分数相减 Fraction operator*(const Fraction &f1)const;//分数相乘 Fraction operator/(const Fraction &f1)const;//分数相除 void compare(Fraction f1, Fraction f2); private: int top; int bottom; };
#include<iostream> #include"Fraction.h" using namespace std; Fraction::Fraction() { top = 0; bottom = 1; } Fraction::Fraction(int x, int b) { top = x; bottom = b; } Fraction::Fraction(int x) { top = x; 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::sub(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::compare(Fraction f1, Fraction f2)//比较 { float a = float(f1.top) * float(f2.bottom); float b = float(f2.top) / float(f1.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; } } void Fraction::show(){ cout<<top<<"/"<<bottom<<endl; } Fraction Fraction::operator+(const Fraction &f1)const { return Fraction(top * f1.bottom + f1.top*bottom,bottom * f1.bottom); } Fraction Fraction::operator-(const Fraction &f1)const { return Fraction(top * f1.bottom - f1.top*bottom,bottom * f1.bottom); } Fraction Fraction::operator*(const Fraction &f1)const { return Fraction(top*f1.top,bottom*f1.bottom); } Fraction Fraction::operator/(const Fraction &f1)const { return Fraction(top*f1.bottom,bottom * f1.top); }
#include<iostream> #include"Fraction.h" using namespace std; int main() { Fraction a; Fraction b(3,4); Fraction c(5); a.show(); b.show(); c.show(); b.add(c); b.sub(c); b.mul(c); b.div(c); a.compare(b,c); Fraction l1(4,2); Fraction l2(6,3); Fraction l3; l3=l1+l2; l3.show(); return 0; }
以上是关于实验6的主要内容,如果未能解决你的问题,请参考以下文章
20155201 李卓雯 《网络对抗技术》实验一 逆向及Bof基础