#include<iostream> #include"core_parent.h" #include"core_A.h" #include"core_B.h" #include"core_C.h" using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { core_A test1(20,15); core_B test2(7,9); core_C test3(6,8); test1.plus(); test1.minus(); cout<<endl; test2.plus(); test2.x(); cout<<endl; test3.plus(); test3.division(); return 0; }
#include<iostream> using namespace std; class core_parent{ public: core_parent(float a,float b):m(a),n(b){ } void plus(){ cout<<m+n<<endl; } const float get_m(){ return m; } const float get_n(){ return n; } private: float m; float n; };
#include<iostream> using namespace std; class core_A:public core_parent{ public: core_A(float a,float b):core_parent(a,b){ } void minus(){ cout<<get_m()-get_n()<<endl; } };
#include<iostream> using namespace std; class core_B:public core_parent{ public: core_B(float a,float b):core_parent(a,b){ } void x(){ cout<<get_m()*get_n()<<endl; } };
#include<iostream> using namespace std; class core_C:public core_parent{ public: core_C(float a,float b):core_parent(a,b){ } void division(){ cout<<get_m()/get_n()<<endl; } };

#include <iostream> #include"vehicle.h" #include"bicycle.h" #include"motorcar.h" #include"motorcycle.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { motorcar AE86(280,5,4);//最大时速 280kmh,重5t,限载4人 cout<<endl; bicycle shared(3,1,1); cout<<endl; motorcycle halo(200,1,2,1); cout<<endl; AE86.run(); shared.stop(); halo.run(); halo.stop(); cout<<halo.maxspeed<<endl; return 0; }
#ifndef _VEHICLE #define _VEHICLE #include<iostream> using namespace std; class vehicle{ public: vehicle(int m,int w):maxspeed(m),weight(w){ cout<<"构造一辆vehicle"<<endl; } void run(){ cout<<"run"<<endl; } void stop(){ cout<<"stop"<<endl; } ~vehicle(){ cout<<"析构一辆vehicle"<<endl; } int maxspeed; int weight; }; #endif
#ifndef _BICYCLE #define _BICYCLE #include<iostream> using namespace std; class bicycle:virtual public vehicle{ public: bicycle(int m,int w,int h):vehicle(m,w),height(h){ cout<<"构造一辆共享单车"<<endl; } ~bicycle(){ cout<<"析构一辆共享单车"<<endl; } int height; }; #endif
#ifndef _MOTORCAR #define _MOTORCAR #include<iostream> using namespace std; class motorcar:virtual public vehicle{ public: motorcar(int m,int w,int s):vehicle(m,w),seatnum(s){ cout<<"构造一辆AE86"<<endl; } ~motorcar(){ cout<<"析构一辆AE86"<<endl; } int seatnum; }; #endif
#ifndef _MOTORCYCLE #define _MOTORCYCLE #include<iostream> using namespace std; class motorcycle:public bicycle,public motorcar{ public: motorcycle(int m,int w,int s,int h):bicycle(666,666,h),vehicle(m,w),motorcar(233,233,s){ cout<<"构造一辆摩托自行车"<<endl; } ~motorcycle(){ cout<<"析构一辆摩托自行车"<<endl; } }; #endif

#include <iostream> #include "Fraction.h" #include "iFraction.h" using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { Fraction a(10,100); iFraction b(40,100); a.show(); b.show(); cout<<endl<<endl; a+b; a.show(); a-b; a*b; a.show(); return 0; }
#ifndef FRACTION_H #define FRACTION_H class Fraction { public: Fraction(); Fraction(int t0); Fraction(int t0,int b0); void plus(Fraction &f); void minus(Fraction &f); void x(Fraction &f); void divide(Fraction &f); void compare(Fraction &f); void show(); Fraction operator+(Fraction &f); Fraction operator-(Fraction &f); Fraction operator*(Fraction &f); Fraction operator/(Fraction &f); void test(); friend void convertF(); private: int top; int bottom; }; #endif
#include<iostream> #include"Fraction.h" using namespace std; Fraction::Fraction():top(0),bottom(1){ cout<<"构造函数已调用(1),分数初始化"<<endl; } Fraction::Fraction(int t0):top(t0),bottom(1){ cout<<"构造函数已调用(2),分数初始化"<<endl; } Fraction::Fraction(int t0,int b0):top(t0),bottom(b0){ cout<<"构造函数已调用(3),分数初始化"<<endl; } void Fraction::plus(Fraction &f){ top*=f.bottom; top+=((f.top)*bottom); bottom*=f.bottom; cout<<"结果为 "<<top<<'/'<<bottom<<endl; } void Fraction::minus(Fraction &f){ top*=f.bottom; top-=((f.top)*bottom); bottom*=f.bottom; cout<<"结果为 "<<top<<'/'<<bottom<<endl; } void Fraction::x(Fraction &f){ top*=f.top; bottom*=f.bottom; cout<<"结果为 "<<top<<'/'<<bottom<<endl; } void Fraction::divide(Fraction &f){ top*=f.bottom; bottom*=f.top; cout<<"结果为 "<<top<<'/'<<bottom<<endl; } void Fraction::compare(Fraction &f){ int thisone,thatone; thisone=top*f.bottom; thatone=bottom*f.top; if(thisone>thatone) { cout<<"前者大"<<endl; } else if(thisone==thatone) { cout<<"一样大"<<endl; } else { cout<<"后者大"<<endl; } } void Fraction::show(){ cout<<top<<'/'<<bottom<<endl; } void Fraction::test(){ int gong_yin=0; int n=2; for(;n<=(top<bottom?top:bottom);n++) { if(top%n==0&&bottom%n==0) { gong_yin=1; break; } } if(gong_yin) { top/=n; bottom/=n; Fraction::test(); } } Fraction Fraction::operator+(Fraction &f){ this->top *= f.bottom; this->top +=(f.top * this->bottom); this->bottom *= f.bottom; Fraction::test(); } Fraction Fraction::operator-(Fraction &f){ this->top *= f.bottom; this->top -=(f.top * this->bottom); this->bottom *= f.bottom; Fraction::test(); } Fraction Fraction::operator*(Fraction &f){ this->top *= f.top; this->bottom *= f.bottom; Fraction::test(); } Fraction Fraction::operator/(Fraction &f){ this->top *= f.bottom; this->bottom *= f.top; Fraction::test(); }
#ifndef IFRACTION_H #define IFRACTION_H #include "Fraction.h" class iFraction:public Fraction { public: iFraction(int t0,int b0):Fraction(t0,b0){ real=0; } void show(); private: int real; }; #endif
#include<iostream> #include"iFraction.h" using namespace std; void iFraction::show(){ if(real!=0) { cout<<real<<"+"; } Fraction::show(); }

浙公网安备 33010602011771号