Как написать этот класс - PullRequest
       47

Как написать этот класс

0 голосов
/ 26 сентября 2019

Мне нужно написать класс Point, который реализует следующее:

  • Ввод и вывод точки в форме (x, y), где x и y - дваДробные объекты.

  • Сложите и вычтите две точки, используя оператор + и оператор-

  • Умножьте две точки, вычисляя перекрестное произведение

  • Умножьте точку на дробь, которая умножит обе координаты точки на заданную дробь.Обратите внимание, что обе формы умножения должны использовать оператор *;для кросс-произведения параметр должен быть точкой, а для масштабирования - дробью.

Это то, что я до сих пор имею:

#ifndef _POINT_H
#define _POINT_H

#include <string>
#include "fraction.h"

class Point {
  public:
    Point(Fraction x, Fraction y);

    Point operator+(Point rhs);
    Point operator-(Point rhs);
    Point operator*(Point rhs);

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

    friend std::istream &operator>>(std::istream &,Fraction &);
    friend std::ostream &operator<<(std::ostream &,Fraction &);

    private:
        Fraction x, y;
};
#endif

1 Ответ

0 голосов
/ 26 сентября 2019

Я не буду писать реализацию класса для вас, но укажу на некоторые ошибки в том, что вы уже показали.

// 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
...