Monday, 8 May 2023

Advance php-8-Write a PHP Script to create class Shape and its sub-class Triangle, Square, Circle and display area of selected shape (use concept of inheritance).

 

<?php

 define('PI',3.14);

 abstract class Shape

 {

  private $x = 0;

  private $y = 0;

  private $r = 0;

  public abstract function area();

   }

  class Triangle extends Shape

 {

         function __construct($x, $y)

           {

                    $this->x = $x;

                    $this->y = $y;

           }

 

          function area()

          {

                    return 0.5 * $this->x * $this->y;

           }

          }

 

class Square extends Shape

 

{

           function __construct($r)

           {

                    $this->r = $r;

                 }

 

               function area()

 

          {

 

                   return $area  =  $this->r*$this->r;

 

                 

 

          }

 

        

}

 

 

 

class Circle extends Shape

 

{

 

          function __construct($x)

 

          {

 

                   $this->x = $x;

 

          }

 

        

 

          function area ()

 

        {

 

         return PI * ($this->x * $this->x);

 

    }

 

 

}

 

          $c   =  new Triangle(12,4);

 

          echo "<br>CIRCLE Area : ".$c->area();

 

        

 

          echo "<br>-----------------------------------------------------------------";

 

 

 

           $r=new Circle(4);

 

          echo "<br>Circle Area : ".$r->area();

 

        

 

          $r=new Square(4);

 

          echo "<br>Square: ".$r->area();

 

        

 

          echo "<br>-----------------------------------------------------------------";

 

        

 

         

 

?>