CMPT 212
Fall 1997
|
class myClass { public: int a; protected: int b; private: inc c; };a can be accessed publicly.
// file graphobj.h #ifndef _graphobj_h_ #define _graphobj_h_ class Point { public: void move(float new_x, float new_y); void setColour(int new_colour); void display(); protected: float x_coord; float y_coord; int colour; }: class Circle : public Point { public: void display(); void setRadius(float new_radius); protected: float radius; }; class Disk : public Circle { public: void display(); }; #endif // file graphobj.cpp #include "graphobj.h" void Point::move(float new_x, float new_y) { x_coord = new_x; y_coord = new_y; } void Point::setColour(int new_colour) { colour = new_colour; } void Point::display() { showpoint(x_coord, y_coord, colour); // Assume graphics function exists. } void Circle::display() { showcircle(x_coord, y_coord, colour, radius); // Assume graphics function exists. } void Circle::setRadius(float new_radius) { radius = new_radius; } void Disk::display() { Circle::display(); fillarea(x_coord, y_coord, colour); // Assume graphics function exists. }
#include "graphobj.h" int main() { Point p; Circle c; Disk d; p.move(1.0, 2.0); p.setColour(15). p.display(); c.move(5.0, 3.0); c.setColour(12); c.setRadius(4.0); c.display(); d.move(7.0, 4.0); d.setRadius(15.0); d.setColour(3); d.display(); return 0; }
Return to lecture notes index |
|
This page is maintained by simpson@cs.sfu.ca. | Last updated on 29 Oct 1997. |