//---------------------------------------------------------------- // // coloredTriangle.cpp // // Implements some classes to do with geometrical shapes, making use // of the point example from Pohl, C++ by Dissection, pages 166-169. // // The code illustrates composition of classes and the derivation of one // class from another. // // written by: Simon Parsons // modified : 24th February 2010 // //---------------------------------------------------------------- #include using namespace std; //---------------------------------------------------------------- // // point // The class point represents a location in a set of cartesian coordinates. // // It has two data members, an x and a y value, and three constructors. // The constructors initialise the data members. class point { private: double x, y; public: point() : x(0), y(0) { } // Default constructor point(double); // Convert double to point point(double, double); // Initialise both x and y if we are // given values for them. void print() const; void set( double u, double v ); double getX(); double getY(); }; // Constructors inline point::point(double u){ // Illustrates the use of "inline" x = u; y = 0; } point::point(double x, double y){ // Shows one use of "this" this->x = x; this->y = y; } // Print the values void point::print() const { cout << "(" << x << "," << y << ")\n"; } // Set the values. void point::set( double u, double v ) { x = u; y = v; } // Accessor functions. We need these to get hold of x and y since they // are private. double point::getX(){ return x; } double point::getY(){ return y; } // End of point //---------------------------------------------------------------- // // triangle // The class triangle is made up of three points. // // The class demonstrates *composition* since it contains three point // objects. class triangle { private: point a, b, c; public: triangle(){}; triangle(point, point, point); // Initialise a, b and c if we are // given values for them. void print() const; void set( point, point, point); // Set the values of the three points point getA(); // and retrieve them also. point getB(); point getC(); }; // Constructor triangle::triangle(point p, point q, point r){ a.set(p.getX(), p.getY()); b.set(q.getX(), q.getY()); c.set(r.getX(), r.getY()); } // Function definitions void triangle::print() const{ cout << "The triangle has the following points:" << endl; a.print(); b.print(); c.print(); } void triangle::set(point p, point q, point r){ a.set(p.getX(), p.getY()); b.set(q.getX(), q.getY()); c.set(r.getX(), r.getY()); } point triangle::getA(){ return a; } point triangle::getB(){ return b; } point triangle::getC(){ return c; } // End of triangle //---------------------------------------------------------------- // // coloredTriangle // // A sub-class of triangle. // // The classe demonstrates *derivation* since coloredTriangle is // derived from triangle. // // This is just a sketch of the class, A full implementation would // need access functions for color also. class coloredTriangle: public triangle { private: string color; public: coloredTriangle(point, point, point); // Initialise points if we are // given values for them. }; // Constructor // // Note that because coloredTriangle is a triangle, we can call the public // member functions of triangle. Here we use these to set the points. coloredTriangle::coloredTriangle(point p, point q, point r){ set(p, q, r); // However, we can't access the private data members of triangle. If you try: //a.set(p.getX(), p.getY()); //b.set(q.getX(), q.getY()); //c.set(r.getX(), r.getY()); // then the compiler will flag up an error --- try it } // End of coloredTriamngle //---------------------------------------------------------------- // // Main program // This creates three instances of point, creating them in different ways, and // then printing out their contents. int main() { point a; point b(2.2); point c(2.3, 4.5); a.set(1.2, 3.4); cout << "point a = "; a.print(); cout << "point b = "; b.print(); cout << "point c = "; c.print(); cout << endl; triangle t(a, b, c); t.print(); cout << endl; coloredTriangle e(a, b, c); e.print(); } // end of main()