首页 > 编程语言 >C++分数类设计题

C++分数类设计题

时间:2022-09-20 10:25:27浏览次数:95  
标签:分数 fractions int C++ should returns fraction constructor 设计

C++分数类设计题

Define and implement (that is, create Fraction.h and Fraction.cpp files) for a class called Fraction. The class represents a fraction in math (i.e., an integer over an integer) that has the following features and operations:
The class's attributes should consist of two int values: numerator and denominator.
There should be the following constructors:A parameterless constructor that initializes the fraction to 0. That is, 0/1.
A constructor that takes a single int value, x. The fraction should be initialized to be a fraction equivalent of the int value. That is, x/1.
A constructor that takes two int values, x and y. The fraction should be initialized to be a fraction equivalent of the int value. That is, x/y.
A copy constructor.
The following operations should  be defined:toDouble() : returns a double representation of the fraction.
toString() : returns a string representation of the fraction.
getNumerator() : returns the numerator
getDenominator() : returns the denominator
The following operators should be overridden:

+ : adds two fractions in the typical way
- : subtracts fractions in the typical way
* : multiplies fractions in the typical way
/ : divides fractions in the typical wayThe user should assume the responsibility of not dividing by zero. We'll learn how to deal with this later.

The stream operators << and >> should be implemented (as friend functions).
You must also provide a test program (main.cpp) that exercises the features of the class and ensures they work correctly.

源码传送门

传送门:https://pan.baidu.com/s/1JJs9vbZahUCB6cQvXLgAVg?pwd=1111

标签:分数,fractions,int,C++,should,returns,fraction,constructor,设计
From: https://www.cnblogs.com/codewriter/p/16710115.html

相关文章

  • C++进制转换题
    C++进制转换题159.102InstructionsforAssignment1Assignment1startsinWeek2andisdueinWeek5(ThursdaySep.22,2022at7pm)(ChinaTime).Youshouldpl......
  • C++ populate template array via random generator and finally sort,print
    #pragmaonce#pragmacomment(lib,"rpcrt4.lib")#include<algorithm>#include<cstring>#include<iostream>#include<random>#include<vector>#include<Windo......
  • 通用ORM的设计与实现
    介绍我们通用的ORM,基本模式都是想要脱离数据库的,几乎都在编程语言层面建立模型,由程序去与数据库打交道。虽然脱离了数据库的具体操作,但我们要建立各种模型文档,用代码去写......
  • 【设计模式】Java设计模式 - 模板模式
    Java设计模式-模板模式......
  • 数据结构算法与应用:C++语言描述(第2章 程序性能)
    目录2.1引言2.2空间复杂性(spaceComplexity)\(S_p(n)\)2.2.1空间复杂性的组成2.2.2举例2.3时间复杂性(timecomplexity)\(T(n)\)2.3.1时间复杂性的组成2.3.2操作计......
  • 设计模式在业务系统中的应用
      本文的重点在于说明工作中所使用的设计模式,为了能够更好的理解设计模式,首先简单介绍一下业务场景。使用设计模式,可以简化代码、提高扩展性、可维护性和复用性。有哪......
  • 数据结构算法与应用:C++语言描述(第2章 程序性能)
    目录2.1引言2.2空间复杂性(spaceComplexity)\(S_p(n)\)2.2.1空间复杂性的组成2.2.2举例2.3时间复杂性(timecomplexity)\(T(n)\)2.3.1时间复杂性的组成2.3.2操作计......
  • C++ 头文件接口设计浅谈
    C++头文件接口设计浅谈作者:独钓寒江雪链接:https://zhuanlan.zhihu.com/p/338227526对于很多出入门C++的程序员来说,大部门新手都是在用别人封装好的库函数,却没有尝试过......
  • C++个人财务管理系统
    C++个人财务管理系统个人财务管理系统功能要求1.初始化:将余额置零;2.记录发生的业务操作:生成一条新的业务信息(包括日期(年、月、日)、业务说明(如收到父母转过来的生......
  • 设计模式---六大设计原则
    六大设计原则单一职责原则接口隔离原则开闭原则依赖倒置原则里氏代换原则迪米特法则单一职责原则我们分别看两个案例,一个是遵守单一职责原则,另一个是违背。违背......