//---------------------------------------------------------------- // // robots.cpp // // An example of friend classes and simple derivation. // // written by: Simon Parsons // modified : 13th March 2009 // //---------------------------------------------------------------- #include #include #include #include using namespace std; //---------------------------------------------------------------- // // point // The class point represents a location in a set of cartesian // coordinates. class point { private: double x, y; public: point() : x(0), y(0) { } point(double); point(double, double); void print() const; void set( double u, double v ); double getX(); double getY(); }; // Constructors inline point::point(double u){ x = u; } point::point(double x, double y){ 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 //---------------------------------------------------------------- // // dirt class dirt { private: point location; public: void set(double x, double y); double getX(); double getY(); }; // Accessor functions. void dirt::set(double u, double v){ location.set(u, v); } double dirt::getX(){ return location.getX(); } double dirt::getY(){ return location.getY(); } // End of dirt // Make the dirt global so that we can access it inside other classes dirt dust[3]; //---------------------------------------------------------------- // // robot class robot { private: point location; public: void set(double x, double y); double getX(); double getY(); }; // Accessor functions. We need these to get hold of x and y since they // are private. void robot::set(double u, double v){ location.set(u, v); } double robot::getX(){ return location.getX(); } double robot::getY(){ return location.getY(); } // End of robot //---------------------------------------------------------------- // // roomba // // Roomba is a sub-class of robot. class roomba: public robot { friend class trilobite; // Trilobite has access to all the data // members of roomba. private: string type; public: void setType(string s); void vacuum(double x, double y); }; void roomba::setType(string s){ type = s; } void roomba::vacuum(double x, double y){ for(int i = 0; i < 3; i++){ if((dust[i].getX() == x) && (dust[i].getY() == y)){ cout << "Yay!, got that dirt" << endl; } else{ cout << "Missed" << endl; } } } // End of roomba // Declare a global roomba object so that we can use it in the trilobite // class. roomba roomba1; //---------------------------------------------------------------- // // trilobite // // Trilobite is a sub-class of robot class trilobite: public robot { friend void printType(); private: int secretNumber; public: void setNumber(int n); void print(); }; void trilobite::setNumber(int n){ secretNumber = n; } // Because trilobite is a friend of roomba, it has access to the // private data members of roomba void trilobite::print(){ cout << "The roomba's type is "; cout << roomba1.type <