第一题:题目要求:
代码:
//chip.h #include<iostream> #pragma once using namespace std; class chip { public: chip(int a,int b):m(a),n(b) {}; int geta(){return m;} int getb(){return n;} int show(); private: int m; int n; }; class chipa:public chip { public: chipa(int a ,int b):chip(a,b){}; int showa(); }; class chipb:public chip { public: chipb(int a,int b):chip(a,b){}; int showb(); }; class chipc:public chip { public: chipc(int a,int b):chip(a,b){}; int showc(); };
//chip.cpp #include<iostream> #include"chip.h" using namespace std; int chip::show() { return m+n; } int chipa::showa() { cout<<"chipa:"<<endl; cout<<"m+n="<<chip::show()<<endl; cout<<"m-n="; return geta()-getb(); } int chipb::showb() { cout<<"chipb:"<<endl; cout<<"m+n="<<chip::show()<<endl; cout<<"m*n="; return geta()*getb(); } int chipc::showc() { cout<<"chipc:"<<endl; cout<<"m+n="<<chip::show()<<endl; cout<<"m/n="; return geta()/getb(); }
#include <iostream> #include"chip.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) { chipa A(1, 2); chipb B(1, 2); chipc C(1, 2); cout << "m=1 n=2" << endl; cout << A.showa() << endl; cout << B.showb() << endl; cout << C.showc() << endl; return 0; }
运行图:
第二题:
:
代码:
//vehicle #pragma once #include<iostream> using namespace std; class vehicle { public: //外部接口 vehicle(int a, int b) :maxspeed(a), weight(b) { //构造函数 cout<<"maxspeed="<<maxspeed<<endl<<"weight="<<weight<<endl;}; void run() {//run()函数 cout << "run" << endl; } void stop() { //stop()函数 cout << "stop" << endl; } ~vehicle() { //析构函数 cout << "Destructing vehicle" << endl; } private: int maxspeed;//int变量maxspeed和weight int weight; }; //定义派生类自行车 class bicycle :virtual public vehicle { public: //新增外部接口 bicycle(int a, int b, int c) :vehicle(a, b), height(c) { cout << "height=" << height << endl; } ~bicycle() { //析构函数 cout << "Destructing bicycle" << endl; } private: int height;//新增int 变量height }; //定义派生类汽车 class motorcar :virtual public vehicle { public: //新增外部接口 motorcar(int a, int b, int d) :vehicle(a, b), seatnum(d) { cout << "seatnum=" << seatnum << endl; } ~motorcar() { //析构函数 cout << "Destructing motorcar" << endl; } private: int seatnum;//新增int变量seatnum }; //定义派生类摩托车 class motocycle :public bicycle, public motorcar { public: //新增外部接口 motocycle(int a, int b, int c, int d) :vehicle(a, b), bicycle(a, b, c), motorcar(a, b, d) {}; ~motocycle() { //析构函数 cout << "Destructing motocycle" << endl; } };
#include <iostream> #include"vehicle.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) { motocycle M(1, 2, 1,2 ); //定义motocycle类对象M M.run(); M.stop(); return 0; }
运行图:
第三题:
代码:
//Fraction.h #pragma once class Fraction { public: Fraction(); //构造函数 Fraction(int t, int b);//函数重载 Fraction(int t); //函数重载 void show(); //输出 Fraction operator+ (const Fraction &f0) const { //重载+ Fraction f; f.top = top * f0.bottom + f0.top*bottom; f.bottom = bottom * f0.bottom; return f; }; Fraction operator- (const Fraction &f0) const { //重载- Fraction f; f.top = top * f0.bottom - f0.top*bottom; f.bottom = bottom * f0.bottom; return f; }; Fraction operator* (const Fraction &f0) const { //重载* Fraction f; f.top = top * f0.top; f.bottom = bottom * f0.bottom; return f; }; Fraction operator/ (const Fraction &f0) const { //重载/ Fraction f; f.top = top * f0.bottom; f.bottom = bottom * f0.top; return f; }; int gett() { return top; } int getb() { return bottom; } private: int top; //分子 int bottom; //分母 };
//Fraction.cpp #include "Fraction.h" #include <iostream> #include<stdlib.h> using namespace std; int measure(int x, int y) //构造函数,找出分子与分母的最大公约数 { int z = y; while (x%y != 0) { z = x % y; x = y; y = z; } return z; } 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::show() { //输出 int t,b,z; t = top; b = bottom; while (b!=0) { if (t == 0) { cout << "0" << endl; break; } z = measure(t, b); t /= z; b /= z; if (b == 1) { cout << t << endl; break; } if (b == -1) { cout << -t << endl; break; } if (t > 0 && b> 0) { cout << t << "/" << b << endl; break; } if (t < 0 && b < 0) { cout << abs(t) << "/" << abs(b) << endl; break; } if (t > 0 && b< 0) { cout << -abs(t) << "/" << abs(b) << endl; break; } if (t < 0 && b> 0) { cout << -abs(t) << "/" << abs(b) << endl; break; } } }
//iFraction.h #pragma once #include<iostream> #include"Fraction.h" using namespace std; class iFraction :public Fraction { public: iFraction() :Fraction() {}; iFraction(int t, int b) :Fraction(t, b) {}; iFraction(int t) :Fraction(t) {}; void convertF(); };
//iFraction.cpp #include "iFraction.h" #include <iostream> using namespace std; int measure(int x, int y); //构造函数,找出分子与分母的最大公约数 void iFraction::convertF() { //输出 int t, b, z, A; t = gett(); b = getb(); while (b != 0) { A = t / b; if (A != 0) { cout << A; t %= b; z = measure(t, b); t /= z; b /= z; if (t == 0) { cout << endl; break; } else { cout << "(" << abs(t) << "/" << abs(b) << ")" << endl; break; } } if (A == 0) { Fraction::show(); break; } } }
#include <iostream> #include"raction.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 f1; //不提供初始值 Fraction f2(-2, 3); //提供两个初始值 Fraction f3(3); //提供一个初始值 Fraction f4,f5,f6,f7; f4 = f2 + f3; f5 = f2 - f3; f6 = f2 * f3; f7 = f2 / f3; f1.show(); f2.show(); f3.show(); f4.show(); f5.show(); f6.show(); f7.show(); iFraction F1(6, 3); iFraction F2(2, 4); iFraction F3(10, 6); iFraction F4(-10, 6); F1.convertF(); F2.convertF(); F3.convertF(); F4.convertF(); return 0; }
运行图: