/*---- implementation file for class: ColorPoint a ColorPoint represents a point in a 2-dimensional coordinate space that has a color by: Sharon Tuttle last modified: 2022-11-11 - cleaned up after class 2022-11-10 - completed 1st version during class, W12-2 2022-11-08 - just STARTED during class, W12-1 ----*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> #include "Point.h" #include "ColorPoint.h" using namespace std; // constructors // for the 0-argument constructor, you get // (0, 0, "") // (note the call to base class Point's constructor // in the method headers) ColorPoint::ColorPoint(): Point() { // need to initialize data fields // particular to ColorPoint color = ""; } ColorPoint::ColorPoint(double init_x, double init_y, string init_color): Point(init_x, init_y) { color = init_color; } // overloaded == operator // (because it ADDS an == when comparing // a ColorPoint to the INHERITED == when // comparing a Point... I THINK...?) bool ColorPoint::operator==(const ColorPoint& rhs) { return ( (abs(get_x() - rhs.get_x()) < 0.0000001) && (abs(get_y() - rhs.get_y()) < 0.0000001) && (color == rhs.color) ); } // accessors string ColorPoint::get_color() const { return color; } // mutators void ColorPoint::set_color(string new_color) { color = new_color; } // redefined display method -- it is to replace // that of Point for a ColorPoint void ColorPoint::display() const { // I decided, if color data field is "", // just don't display a label it, either // (excuse to demo calling the base class' version // of a method! 8-) ) if (color == "") { cout << "Color"; Point::display(); cout << " (and no color, currently)" << endl; } else { cout << "ColorPoint: (x: " << get_x() << ", y: " << get_y() << ", color: " << color << ")" << endl; } } // OVERLOADED version of display - this adds a second, // one-argument display method for a ColorPoint void ColorPoint::display(string desired_start) const { cout << desired_start << ":"; display(); } // REDEFINED version of to_string string ColorPoint::to_string() const { return "ColorPoint(" + std::to_string(get_x()) + ", " + std::to_string(get_y()) + ", " + color + ")"; }