如何写这节课

时间:2019-09-26 16:26:37

标签: c++ data-structures computer-science

我需要编写一个实现以下内容的Point类:

  
      
  • 点的输入和输出形式为(x,y),其中x和y是两个小数对象。

  •   
  • 使用operator +和operator-

  • 加减两点   
  • 将两点相乘,计算出叉积

  •   
  • 将点乘以分数,该分数将点的两个坐标乘以给定的分数。   请注意,两种形式的乘法均应使用运算符*;对于叉积,参数应为Point,对于缩放,参数应为分数。

  •   

这是我到目前为止所拥有的:

Optional.ofNullable(foo).isPresent();
Optional.ofNullable(foo).ifPresent(consumer);
Optional.ofNullable(foo).map(mapper)

1 个答案:

答案 0 :(得分:0)

我不会为您编写该类的实现,但是会指出到目前为止您所显示的内容中的一些错误。

// Don't start header guards with underscore plus an uppercase letter.
// It's reserved for the implementation.
#ifndef POINT_H 
#define POINT_H

// Put your own headers first. It's not a rule but it's good for finding missing headers
// inside your own headers.
#include "fraction.h"
#include <string>

class Point {
public:
    // Take arguments by reference and not by value (unless they are fundamental types).
    // It's often quicker to use references. Make the references const since you are not
    // planning on changing the objects you use as arguments.
    Point(const Fraction& x, const Fraction& y);

    // None of these modifies your object, so mark the member functions const too
    Point operator+(const Point& rhs) const;
    Point operator-(const Point& rhs) const;
    Point operator*(const Point& rhs) const;

    bool operator==(const Point& rhs) const;
    bool operator!=(const Point& rhs) const;

    // You add the wrong streaming operators. Fraction is not what you want to stream.
    // You want to stream Point objects.
    friend std::istream& operator>>(std::istream&, Point&);

    // Streaming out your object should not change the object so make it const:
    friend std::ostream& operator<<(std::ostream&, const Point&);

private:
    Fraction x, y;
};
#endif