博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第六次实验报告
阅读量:7000 次
发布时间:2019-06-27

本文共 5111 字,大约阅读时间需要 17 分钟。

#include
using namespace std;class base1 {public: base1(int m = 0, int n = 0) :m1(m), n1(n) {}; void setnumber1(int m0, int n0) { m1 = m0; n1 = n0; }; int getnumber1() { return m1 + n1; };private: int m1, n1;};class base2 {public: base2(int m = 0, int n = 0) :m2(m), n2(n) {}; void setnumber2(int m0, int n0) { m2 = m0; n2 = n0; }; int getnumber2() { return m2 - n2; };private: int m2, n2;};class base3 {public: base3(int m = 0, int n = 0) :m3(m), n3(n) {}; void setnumber3(int m0, int n0) { m3 = m0; n3 = n0; }; int getnumber3() { return m3 * n3; };private: int m3, n3;};class base4 {public: base4(int m = 0, int n = 0) :m4(m), n4(n) {}; void setnumber4(int m0, int n0) { m4 = m0; n4 = n0; }; double getnumber4() { return (double)m4 / n4; };private: int m4, n4;};class A :public base1,public base2 {public: A(int m=1, int n=1) :base1(m, n), base2(m, n) {};};class B :public base1,public base3 {public: B(int m=1, int n=1) :base1(m, n), base3(m, n) {};};class C :public base1,public base4 {public: C(int m=1, int n=1) :base1(m, n), base4(m, n) {};};int main() { A a; B b; C c; cout << a.getnumber2() << endl; cout << b.getnumber3() <

 

#include
using namespace std;class vehicle {public: vehicle(int maxspeed0=0,int weight0=0):maxspeed(maxspeed0), weight(weight0){} ~vehicle(){} void run() { cout << "run" << endl; } void stop() { cout << "stop" << endl; }private: int maxspeed, weight;};class bicycle:virtual public vehicle {public: bicycle(int maxspeed1 = 0, int weight1 =0, int height1 = 0) :vehicle(maxspeed1, weight1), height(height1){} ~bicycle(){};private: int height;};class motorcar :virtual public vehicle {public: motorcar(int maxspeed2 = 0, int weight2 = 0, int seatnum2=0):vehicle(maxspeed2, weight2), seatnum(seatnum2){} ~motorcar(){}private: int seatnum;};class motorcycle :public bicycle, public motorcar {public: motorcycle(int maxspeed3 = 0, int weight3 = 0, int height3 = 0,int seatnum3=0):bicycle(maxspeed3,weight3,height3),motorcar(maxspeed3,weight3,seatnum3){} ~motorcycle(){}};int main() { motorcycle M(6, 6, 6, 6); M.run(); M.stop(); return 0;}

 fraction.h

#pragma onceclass Fraction {public:    Fraction();    Fraction(int top0, int bottom0);    Fraction(int top0);    void Compare(Fraction&f0);    void set_fraction();    void get_fraction();    Fraction operator+(const Fraction&f0)const;    Fraction operator-(const Fraction&f0)const;    Fraction operator*(const Fraction&f0)const;    Fraction operator/(const Fraction&f0)const;    ~Fraction();private:    int top;    int bottom;};

fraction.cpp

#include"Fraction.h"#include
using namespace std;Fraction::Fraction() :top(0), bottom(1) {}Fraction::Fraction(int top0) : top(top0), bottom(1) {}Fraction::Fraction(int top0, int bottom0) : top(top0), bottom(bottom0) {}Fraction::~Fraction() {}void::Fraction::set_fraction() { cin >> top >> bottom;}void::Fraction::get_fraction() { cout << top << "/" << bottom << endl;}Fraction Fraction:: operator+(const Fraction&f0)const { return Fraction(f0.top*bottom + top * f0.bottom, f0.bottom*bottom);}Fraction Fraction:: operator-(const Fraction&f0)const { return Fraction(top * f0.bottom - f0.top*bottom, f0.bottom*bottom);}Fraction Fraction:: operator*(const Fraction&f0)const { return Fraction(top * f0.top, f0.bottom*bottom);}Fraction Fraction:: operator/(const Fraction&f0)const { return Fraction(top * f0.bottom, bottom * f0.top);}void::Fraction::Compare(Fraction&f0) { if ((top * f0.bottom - f0.top*bottom)>0) cout << top << "/" << bottom << ">" << f0.top << "/" << f0.bottom << endl; else if ((top * f0.bottom - f0.top*bottom)<0) cout << top << "/" << bottom << "<" << f0.top << "/" << f0.bottom << endl; else cout << top << "/" << bottom << "=" << f0.top << "/" << f0.bottom << endl;}

ifraction.h

#pragma once#include"fraction.h"class iFraction: public Fraction{public:    iFraction(int top, int bottom, int wnum) ;    void showiFraction() ;private:    int wnum;};

ifraction.cpp

#include"ifraction.h"#include
using namespace std;iFraction::iFraction(int top0, int bottom0, int wnum0): Fraction(top0,bottom0),wnum(wnum0) {}void iFraction::showiFraction() { cout << wnum<<" "; get_fraction();}

main.cpp

#include"fraction.h"#include"ifraction.h"#include
using namespace std;int main() { Fraction f(1, 1); Fraction f1(2, 2); Fraction f3 = f1 + f; iFraction f4(1, 2, 3); f3.get_fraction(); f3 = f1 - f; f3.get_fraction(); f3 = f1 * f; f3.get_fraction(); f3 = f / f1; f3.get_fraction(); f4.showiFraction(); return 0;}

 做第一二题没有注意要求分文件,加上这两题不是很复杂,就没有采用分文件。

在第三题时发现了一些问题,因为fraction.h需要被多次包含,就出现了了重复编译的问题,这时候就需要采用宏或者预编译头的方式来避免重复编译

还有一个问题我没有解决,本来我在fraction类中是定义了复制构造函数的,但是在重载运算符时,创建了临时无名对象作为返回值时,显示fraction类中没有合适的复制构造函数。后来我把自己定义的复制构造函数删除之后就没有问题了,没有想明白,不知道谁可以为我解答吗?

转载于:https://www.cnblogs.com/miaorui1314/p/9104141.html

你可能感兴趣的文章
25.Linux-Nor Flash驱动(详解)
查看>>
Appium 解决中文输入问题
查看>>
ping正常但是ssh到linux服务器很卡的解决方法
查看>>
php sqlserver及xdebug扩展配置
查看>>
【转】curl命令总结,Http Post_Get 常用
查看>>
jmeter压测前清理内存
查看>>
Windows2012R2版本区别
查看>>
PAT Basic 1073. 多选题常见计分法
查看>>
在 ReactNative 的 App 中,集成 Bugly 你会遇到的一些坑
查看>>
MII_GMII_RGMII_RMII_SMII_SSMII_TBI_RTBI比较
查看>>
inotify+rsync实现实时同步
查看>>
解决微信OAuth2.0网页授权回调域名只能设置一个的问题
查看>>
设计模式:原型模式
查看>>
为什么选择Spring Boot作为微服务的入门级微框架
查看>>
sklearn5_preprocessing数据标准化
查看>>
由浅入深SCF无服务器云函数实践
查看>>
15. 3Sum
查看>>
Entity Framework Code First执行SQL语句、视图及存储过程
查看>>
Storm集群安装与部署
查看>>
FileUriExposedException_Android7.0适配
查看>>