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

Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
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...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
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?
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
White the class Trace with a copy constructor and an assignment operator, printing a short message...
White the class Trace with a copy constructor and an assignment operator, printing a short message in each. Use this class to demonstrate the difference between initialization Trace t("abc"); Trace u = t; and assignment. Trace t("abc"); Trace u("xyz"); u = t; the fact that all constructed objects are automatically destroyed. the fact that the copy constructor is invoked if an object is passed by value to a function. the fact that the copy constructor is not invoked when a...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template <typename ItemType> ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer list } // end default constructor template <typename ItemType> int ArrayBag<ItemType>::getCurrentSize() const { return item_count_; } template <typename ItemType> bool ArrayBag<ItemType>::isEmpty() const { return item_count_ == 0; } template <typename ItemType> bool ArrayBag<ItemType>::add(const ItemType &new_entry) {    bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY); if (has_room_to_add) { items_[item_count_] = new_entry; item_count_++; } // end if return has_room_to_add;...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const container& c); container& operator=(const container& c) // MEMBER FUNCTIONS bool lookup(const int& target); void remove(const int& target); void add(const int& target); private: struct node{ int key; node *next; }; node* head; node* tail; }; #endif For a linked list based implementation of a container class shown above, implement the constructor, copy constructor, destructor and copy assignment functions.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT