Polymorphism

From Rosetta Code
Task
Polymorphism
You are encouraged to solve this task according to the task description, using any language you may know.

Create two classes Point and Circle with a polymorphic function

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

Compiler: DMD,GDC

Forth

Fortran

Java


JavaScript


Perl

Interpeter: Perl


PHP