How to resolve the algorithm Polymorphism step by step in the PHP programming language
How to resolve the algorithm Polymorphism step by step in the PHP programming language
Table of Contents
Problem Statement
Create two classes Point(x,y) and Circle(x,y,r) with a polymorphic function print, accessors for (x,y,r), copy constructor, assignment and destructor and every possible default constructors
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Polymorphism step by step in the PHP programming language
This code demonstrates object-oriented programming principles in PHP, specifically the creation and use of classes and objects representing points and circles. Let's break down the code step by step:
Class Point:
-
The Point class represents a point in a 2D space with coordinates x and y.
-
The constructor
__construct()
method takes either a single Point object or two numeric values (x and y) as arguments to initialize the point's coordinates. -
It also has getter and setter methods for accessing and modifying the x and y coordinates.
-
The
output()
method prints a string representation of the point in the format "Point [x:,y: ]".
Class Circle:
-
The Circle class extends the Point class, inheriting the x and y coordinates from the parent class.
-
The constructor
__construct()
method takes either a single Circle object or a combination of arguments (Point and radius, or x, y, and radius) to initialize the circle's properties. -
It also has a getter and setter method for the radius property.
-
The
getPoint()
method returns a Point object representing the center of the circle. -
The
__toString()
method returns a string representation of the circle in the format "Circle [,radius: ]".
Usage:
-
An instance of the Point class is created with coordinates (1, 5).
-
An instance of the Circle class is created with center at (1, 5) and radius 6.
-
The
output()
method is called on the point and circle objects to print their string representations. Alternatively, you can use theecho
statement with the objects directly since they have overridden the__toString()
method.
Output:
Point [x:1,y:5]
Circle [Point [x:1,y:5],radius:6]
Source code in the php programming language
class Point
{
protected $_x;
protected $_y;
public function __construct()
{
switch( func_num_args() )
{
case 1:
$point = func_get_arg( 0 );
$this->setFromPoint( $point );
break;
case 2:
$x = func_get_arg( 0 );
$y = func_get_arg( 1 );
$this->setX( $x );
$this->setY( $y );
break;
default:
throw new InvalidArgumentException( 'expecting one (Point) argument or two (numeric x and y) arguments' );
}
}
public function setFromPoint( Point $point )
{
$this->setX( $point->getX() );
$this->setY( $point->getY() );
}
public function getX()
{
return $this->_x;
}
public function setX( $x )
{
if( !is_numeric( $x ) )
{
throw new InvalidArgumentException( 'expecting numeric value' );
}
$this->_x = (float) $x;
}
public function getY()
{
return $this->_y;
}
public function setY( $y )
{
if( !is_numeric( $y ) )
{
throw new InvalidArgumentException( 'expecting numeric value' );
}
$this->_y = (float) $y;
}
public function output()
{
echo $this->__toString();
}
public function __toString()
{
return 'Point [x:' . $this->_x . ',y:' . $this->_y . ']';
}
}
class Circle extends Point
{
private $_radius;
public function __construct()
{
switch( func_num_args() )
{
case 1:
$circle = func_get_arg( 0 );
$this->setFromCircle( $circle );
break;
case 2:
$point = func_get_arg( 0 );
$radius = func_get_arg( 1 );
$this->setFromPoint( $point );
$this->setRadius( $radius );
break;
case 3:
$x = func_get_arg( 0 );
$y = func_get_arg( 1 );
$radius = func_get_arg( 2 );
$this->setX( $x );
$this->setY( $y );
$this->setRadius( $radius );
break;
default:
throw new InvalidArgumentException( 'expecting one (Circle) argument or two (Point and numeric radius) or three (numeric x, y and radius) arguments' );
}
}
public function setFromCircle( Circle $circle )
{
$this->setX( $circle->getX() );
$this->setY( $circle->getY() );
$this->setRadius( $circle->getRadius() );
}
public function getPoint()
{
return new Point( $this->getX(), $this->getY() );
}
public function getRadius()
{
return $this->_radius;
}
public function setRadius( $radius )
{
if( !is_numeric( $radius ) )
{
throw new InvalidArgumentException( 'expecting numeric value' );
}
$this->_radius = (float) $radius;
}
public function __toString()
{
return 'Circle [' . $this->getPoint() . ',radius:' . $this->_radius . ']';
}
}
$point = new Point( 1, 5 );
$circle = new Circle( 1, 5, 6 );
$point->output();
// or
echo $point;
echo "\n";
$circle->output();
// or
echo $circle;
You may also check:How to resolve the algorithm Price fraction step by step in the BASIC programming language
You may also check:How to resolve the algorithm Fractal tree step by step in the Processing programming language
You may also check:How to resolve the algorithm Search a list step by step in the Yorick programming language
You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Kotlin programming language