Question

In: Computer Science

Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...

Define the following class:

class XYPoint {

public:

// Contructors including copy and default constructor

// Destructors ? If none, explain why

double getX();

double getY();

// Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2)

private: double x, y;

};

Solutions

Expert Solution

#include <iostream>
#include <cmath>
using namespace std;

class XYPoint {
public:
    XYPoint(double x, double y) : x(x), y(y) {}
    XYPoint() {}
    XYPoint(XYPoint& other) {
        x = other.x;
        y = other.y;
    }
    ~XYPoint() {}

    double getX();
    double getY();
// Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2)
    bool operator<(const XYPoint &rhs) const;
    bool operator>(const XYPoint &rhs) const;
    bool operator<=(const XYPoint &rhs) const;
    bool operator>=(const XYPoint &rhs) const;
    bool operator==(const XYPoint &rhs) const;

    double distance() const {
        return sqrt(x*x + y*y);
    }
private: double x, y;
};

double XYPoint::getX() {
    return 0;
}

double XYPoint::getY() {
    return 0;
}

bool XYPoint::operator<(const XYPoint &rhs) const {
    return distance() < rhs.distance();
}

bool XYPoint::operator>(const XYPoint &rhs) const {
    return distance() > rhs.distance();
}

bool XYPoint::operator<=(const XYPoint &rhs) const {
    return distance() <= rhs.distance();
}

bool XYPoint::operator>=(const XYPoint &rhs) const {
    return distance() >= rhs.distance();
}

bool XYPoint::operator==(const XYPoint &rhs) const {
    return distance() == rhs.distance();
}

int main() {
    // your test code..
    return 0;
}

Related Solutions

class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs);...
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs); //Destructor. Clear all nodes. ~DoubleLinkedList(); // Insert function. Returns true if item is inserted, // false if the item it a duplicate value bool insert(int x); // Removes the first occurrence of x from the list, // If x is not found, the list remains unchanged. void remove(int x); //Assignment operator. const DoubleLinkedList& operator=(const DoubleLinkedList & rhs); private: struct node{ int data; node* next;...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
What is a constructor ? What is a destructor? What is a default constructor? Is it...
What is a constructor ? What is a destructor? What is a default constructor? Is it possible to have more than one default constructor?
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically...
package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums = new double[nums1.length]; for(int i=0;i<nums1.length;i++) nums[i] = nums1[i]; numElements = nums1.length; } void printArray(){ // cost, times System.out.printf("printArray(%d,%d): ",numElements,nums.length); for(int i=0; i<numElements;i++) System.out.print(nums[i]+" "); System.out.println(); }...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c public void setA(double newA) public void setB(double newB) public void setC(double newC) (6) public static QuadraticExpression scale( double r, QuadraticExpression q) returns...
Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double...
Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double capacity) where capacity is a value measured in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh. The method public void drain(double amount) drains the capacity of the battery by the given amount. The method public void charge() charges the battery to its original capacity. The method public double getRemainingCapacity() gets the remaining capacity of the battery. Supply a...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT