Page 345 - CITS - CSA - TP (Volume 1) - Hindi
P. 345
कंप्यूटर सॉफ्टवेयर एप्लीकेशन- CITS
// Parent class
class Animal
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function speak()
{
echo $this->name . “ makes a sound.\n”;
}
}
// Child class inheriting from Animal
class Dog extends Animal
{
public function bark()
{
echo $this->name . “ barks.\n”;
}
}
// Child class inheriting from Animal
class Cat extends Animal
{
public function meow()
{
echo $this->name . “ meows.\n”;
}
}
// Creating instances of child classes
$dog = new Dog(“Buddy”);
$cat = new Cat(“Whiskers”);
// Calling methods from parent and child classes
$dog->speak(); // Output: Buddy makes a sound.
$dog->bark(); // Output: Buddy barks.
$cat->speak(); // Output: Whiskers makes a sound.
$cat->meow(); // Output: Whiskers meows.
?>
</body>
</html>
329
CITS : IT & ITES - कं ूटर सॉ वेयर ए ीके शन - अ ास 60

