Polymorphism: Difference between revisions
Content deleted Content added
Line 24: | Line 24: | ||
protected: |
protected: |
||
int x, y; |
int x, y; |
||
public: |
public: |
||
Point(int x0 = 0, int y0 = 0) : x(x0), y(y0) {} |
Point(int x0 = 0, int y0 = 0) : x(x0), y(y0) {} |
||
Point(const Point& p) : x(p.x), y(p.y) {} |
Point(const Point& p) : x(p.x), y(p.y) {} |
||
virtual ~Point() {} |
virtual ~Point() {} |
||
const Point& operator=(const Point& p) |
const Point& operator=(const Point& p) |
||
{ |
{ |
||
Line 39: | Line 37: | ||
return *this; |
return *this; |
||
} |
} |
||
int getX() { return x; } |
int getX() { return x; } |
||
int getY() { return y; } |
int getY() { return y; } |
||
int setX(int x0) { x = x0; } |
int setX(int x0) { x = x0; } |
||
int setY(int y0) { y = y0; } |
int setY(int y0) { y = y0; } |
||
⚫ | |||
⚫ | |||
}; |
}; |
||
Line 53: | Line 48: | ||
private: |
private: |
||
int r; |
int r; |
||
public: |
public: |
||
Circle(Point p, int r0 = 0) : Point(p), r(r0) {} |
Circle(Point p, int r0 = 0) : Point(p), r(r0) {} |
||
Circle(int x0 = 0, int y0 = 0, int r0 = 0) : Point(x0, y0), r(r0) {} |
Circle(int x0 = 0, int y0 = 0, int r0 = 0) : Point(x0, y0), r(r0) {} |
||
virtual ~Circle() {} |
virtual ~Circle() {} |
||
const Circle& operator=(const Circle& c) |
const Circle& operator=(const Circle& c) |
||
{ |
{ |
||
Line 69: | Line 62: | ||
return *this; |
return *this; |
||
} |
} |
||
int getR() { return r; } |
int getR() { return r; } |
||
int setR(int r0) { r = r0; } |
int setR(int r0) { r = r0; } |
||
⚫ | |||
⚫ | |||
}; |
}; |
||
Revision as of 04:30, 23 February 2007
Polymorphism
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
BASIC
Interpeter: QuickBasic 4.5, PB 7.1
C
Compiler: GCC, MSVC, BCC, Watcom
Libraries: Standard
C++
Compiler: GCC, Visual C++, BCC, Watcom
class Point { protected: int x, y; public: Point(int x0 = 0, int y0 = 0) : x(x0), y(y0) {} Point(const Point& p) : x(p.x), y(p.y) {} virtual ~Point() {} const Point& operator=(const Point& p) { if(this != &p) { x = p.x; y = p.y; } return *this; } int getX() { return x; } int getY() { return y; } int setX(int x0) { x = x0; } int setY(int y0) { y = y0; } virtual void print() { printf("Point\n"); } };
class Circle : public Point { private: int r; public: Circle(Point p, int r0 = 0) : Point(p), r(r0) {} Circle(int x0 = 0, int y0 = 0, int r0 = 0) : Point(x0, y0), r(r0) {} virtual ~Circle() {} const Circle& operator=(const Circle& c) { if(this != &c) { x = c.x; y = c.y; r = c.r; } return *this; } int getR() { return r; } int setR(int r0) { r = r0; } virtual void print() { printf("Circle\n"); } };
C#
D
Forth
Fortran
Java
JavaScript
Perl
Interpeter: Perl