第一题:
代码:
//Base.h(基类) #pragma once #include<iostream> using namespace std; class Base { private: int m, n; public: Base(int a, int b) :m(a), n(b) {}; void plus() { cout << "m+n=" << m + n << endl; } int getm()const { return m; } int getn()const { return n; } };
//A.h(派生类) #pragma once #include"Base.h" #include<iostream> using namespace std; class A :public Base { public: A(int a, int b) :Base(a, b) {}; void min() { int x = getm(); int y = getn(); cout << "m-n=" << x - y << endl; } }; class B :public Base { public: B(int a, int b) :Base(a, b) {}; void mul() { int x = getm(); int y = getn(); cout << "m*n=" << x * y << endl; } }; class C :public Base { public: C(int a, int b) :Base(a, b) {}; void div() { int x = getm(); int y = getn(); cout << "m/n=" << x / y << endl; } };
//main.cpp #include"Base.h" #include"A.h" #include<iostream> using namespace std; int main() { A core1(4, 2); B core2(4, 2); C core3(4, 2); core1.plus(); core1.min(); core2.plus(); core2.mul(); core3.plus(); core3.div(); return 0; }
运行效果:
第二题:
//vehicle.h #pragma once #include<iostream> using namespace std; class vehicle { //基类 private: int maxspeed; int weight; public: vehicle(int a, int b) :maxspeed(a), weight(b) { cout << "maxspeed=" << maxspeed << " weight=" << weight << endl; }; void run() { cout << "run" << endl; } void stop() { cout << "stop" << endl; } ~vehicle() {} }; class bicycle:virtual public vehicle { //派生类(自行车) private: int height; public: bicycle(int a, int b, int c):vehicle(a, b) { height = c; cout << "height=" << height << endl; } ~bicycle() {} }; class motorcar :virtual public vehicle { //派生类(摩托车) private: int seatnum; public: motorcar(int a, int b, int c) :vehicle(a, b) { seatnum = c; cout << "seatnum=" << seatnum << endl; } ~motorcar(){} };
//main.cpp #include<iostream> #include"vehicle.h" using namespace std; int main() { bicycle giant(20, 3, 1); giant.run(); giant.stop(); motorcar Harley(60, 30, 2); Harley.run(); Harley.stop(); return 0; }
运行效果:
第三题:
代码:
//Fraction.h部分 #pragma once class Fraction { public: Fraction(); //构造函数 Fraction(int t, int b);//构造函数(函数重载) Fraction(int t); //构造函数(函数重载) Fraction operator+(const Fraction &f1)const;//分数相加 Fraction operator-(const Fraction &f1)const;//分数相减 Fraction operator*(const Fraction &f1)const;//分数相乘 Fraction operator/(const Fraction &f1)const;//分数相除 int gett() { return top; }int getb() { return bottom; } void show(); private: int top; int bottom; //top为分子,bottom为分母 };
//iFraction.h部分 #pragma once #include<iostream> #include"Fraction.h" using namespace std; class iFraction :public Fraction { public: iFraction(int t, int b) :Fraction(t, b) {} iFraction(int t):Fraction(t){} iFraction():Fraction(){} void show(); void convertF(); };
//Fraction.cpp部分 #include<iostream> #include"Fraction.h" using namespace std; int sim(int t, int j) { //sim函数,功能为找出分子与分母的最大公约数 int a, b, i, m, c; a = t; b = j; if (a >= b)m = b; else m = a; for (i = 1; i <= m; i++) { if (a%i == 0 && b%i == 0)c = i; } return c; //返回值为最大公约数c } Fraction::Fraction() { //Fraction函数的实现 top = 0; bottom = 1; } Fraction::Fraction(int t, int b) { //Fraction函数的实现(函数重载) top = t; bottom = b; } Fraction::Fraction(int t) { //Fraction函数的实现(函数重载) top = t; bottom = 1; } Fraction Fraction::operator+(const Fraction &f1)const { int x, y; x = bottom * f1.bottom; y = top * f1.bottom + bottom * f1.top; return(Fraction(y, x)); } Fraction Fraction::operator-(const Fraction &f1)const { //分数减法sub函数的实现 Fraction f2; int x, y; y = top * f1.bottom - f1.top*bottom; x = bottom * f1.bottom; return(Fraction(y, x)); } Fraction Fraction::operator*(const Fraction &f1)const { //分数乘法mul函数的实现 int x, y; y =top*f1.top; x =bottom*f1.bottom; return(Fraction(y, x)); } Fraction Fraction::operator/(const Fraction &f1)const { //分数除法div函数的实现 int x, y; y =top*f1.bottom; x = bottom * f1.top; return(Fraction(y, x)); } void Fraction::show() { //show函数的实现 if (top > 0 && bottom > 0) { int c; c = sim(top, bottom); cout <<"("<< top / c << "/" << bottom / c <<")"<< endl;//分子分母同除以最大公约数得到最简形式(扩展) } if (top < 0 && bottom>0) { int c; c = sim(-top, bottom); cout <<"("<< top / c << "/" << bottom / c <<")"<< endl;//分子分母同除以最大公约数得到最简形式(扩展) } if (top > 0 && bottom < 0) { int c; c = sim(top, -bottom); cout <<"("<< -top / c << "/" << -bottom / c <<")"<< endl;//将分母的负号移动至分子上(扩展) } if (top < 0 && bottom < 0) { int c; c = sim(-top, -bottom); cout <<"("<< -top / c << "/" << -bottom / c <<")"<< endl;//分子分母同为负数时去除负号 } if (top == 0) cout <<"("<< top << "/" << bottom <<")"<< endl; }
//iFraction.cpp部分 #include<iostream> #include"Fraction.h" #include"iFraction.h" #include<cmath> using namespace std; int sim(int t, int j) { //sim函数,功能为找出分子与分母的最大公约数 int a, b, i, m, c; a = t; b = j; if (a >= b)m = b; else m = a; for (i = 1; i <= m; i++) { if (a%i == 0 && b%i == 0)c = i; } return c; //返回值为最大公约数c } void iFraction::show() { if (gett() > 0 && getb() > 0) { int c; c = sim(gett(), getb()); cout <<"("<< gett() / c << "/" << getb() / c <<")"<< endl;//分子分母同除以最大公约数得到最简形式 } if (gett() < 0 && getb()>0) { int c; c = sim(-gett(), getb()); cout <<"("<< gett() / c << "/" << getb() / c <<")"<< endl;//分子分母同除以最大公约数得到最简形式 } if (gett() > 0 && getb() < 0) { int c; c = sim(gett(), -getb()); cout <<"("<< -gett() / c << "/" << -getb() / c <<")"<< endl;//将分母的负号移动至分子上 } if (gett() < 0 && getb() < 0) { int c; c = sim(-gett(), -getb()); cout <<"("<< -gett() / c << "/" << -getb() / c <<")"<< endl;//分子分母同为负数时去除负号 } if (gett() == 0) { cout << "(" << gett() << "/" << getb() << ")" << endl; } } void iFraction::convertF() { if (abs(gett()) / abs(getb())>=1&&abs(gett())%abs(getb())) { int m = gett() / getb(); int n = gett() - m * getb(); iFraction f1(n, getb()); cout << m; f1.show(); }
//main.cpp部分 #include<iostream> #include"Fraction.h" #include"iFraction.h" using namespace std; int main() { iFraction f1; iFraction f2(5); iFraction f3(3, 6); iFraction f4(-5, 6); iFraction f5(3, -4); iFraction f6(-5, -6); iFraction f7(5, 3); f1.show(); f2.show(); f3.show(); f4.show(); f5.show(); f6.show(); f7.convertF(); Fraction f8(3, 6); Fraction f9(-5, 6); Fraction f10 = f8 + f9; f10.show(); f10 = f8 * f9; f10.show(); f10 = f9 / f8; f10.show(); f10 = f8 - f9; f10.show(); return 0; }
运行效果:
实验总结与反思:
最近事情比较多,写得比较粗糙,最后一道题还没有写,过两天我再用另一篇随笔补上吧。