Enuntul urmator sintetizeaza aspectele esentiale ale
mecanismului si sintaxei mostenirii de clase in PHP, intre care accesul din
clasa derivata la membrii privati ai clasei parinte prin intermediul metodelor
mostenite, apelarea din clasa derivata a constructorului din clasa parinte,
apelul membrilor constanti, suprascrierea metodelor.
"Develop
a hierarchic structure of classes:Circle and Cylinder
The base class is Circle and the derived class is Cylinder. It inherits the method Display() from base class Circle.
You have to define the new necessary fields and methods and override the method Display() to print the total surface area and volume of the cylinder. "
The base class is Circle and the derived class is Cylinder. It inherits the method Display() from base class Circle.
You have to define the new necessary fields and methods and override the method Display() to print the total surface area and volume of the cylinder. "
Daca baza
cilindrului cu inaltimea H este cercul de raza R cu suprafata S = PI*R*R,
atunci suprafata totala a cilindrului este: 2*PI*R*H + 2*S iar volumul este:
S*H.
<?php
// Class circle and class cylinder
class Circle
{
private $radius;
const PI = 3.14;
function __construct()
{
echo "Enter Radius:";
$this->radius=trim(fgets(STDIN));
}
function getRadius()
{
return $this->radius;
}
function Display()
{
echo "Area of circle = ".$this->radius*$this->radius*self::PI;
}
}
class Cylinder extends Circle
{
private $height;
function __construct()
{
parent::__construct();
echo "Enter Height:";
$this->height = trim(fgets(STDIN));
}
function getHeight()
{
return $this->height;
}
function Display()
{
echo "Total Surface Area of cylinder = ";
echo ($this->getRadius()*$this->getRadius()*2*self::PI) + (2*self::PI*$this->getRadius()*$this->height);
echo "\nVolume of cylinder = ";
echo self::PI*$this->getRadius()*$this->getRadius()*$this->height;
}
}
echo "Select Option:\n";
echo "1.Circle 2.Cylinder:";
$opt=trim(fgets(STDIN));
switch($opt)
{
case 1: $circle=new Circle(); $circle->Display();break;
case 2: $cylinder= new Cylinder(); $cylinder->Display();break;
default: echo "Invalid option:";
}
exit;
?>
Link-ul catre codul rulat pe programmr.com, aici.
No comments:
Post a Comment