Fiyero1988 Posted December 13, 2009 Share Posted December 13, 2009 I can't for the life of me figure out how to write what is being asked. Maybe someone can figure it out: Create an abstract base class called Pet. This class will have one attribute called name. Create set and get methods for name. Create an abstract method called speak. Implement a constructor that initializes name. Create another class called Cat that extends the class Pet. This class will have an attribute for weight. Create set and get methods for weight. Implement the speak method so it prints the name of the pet and prints “I’m a cat and I weigh” and the value of weight. Implement a constructor that send the value of name to the Pet constructor and initializes the weight. Create another class called Dog that extends the class Pet. This class will have an attribute for weight. Create set and get methods for weight. Implement the speak method so it prints the name of the pet and prints “I’m a dog and I weigh” and the value of weight.. Implement a constructor that send the value of name to the Pet constructor and initializes the weight. Use the following line in the speak method to display the name: echo $this‐>getName(); The above line uses the current instance to call the method getName in the base class. In the driver program create and array that consists of an instance of a cat and an instance of a dog. Use a for loop to get each element in the array and call its speak method. Link to comment https://forums.phpfreaks.com/topic/184947-php-nightmarefor-me-at-least/ Share on other sites More sharing options...
rajivgonsalves Posted December 13, 2009 Share Posted December 13, 2009 Its pretty easy you just create classes and extend them over each other the following link should point you in the right direction http://php.net/manual/en/language.oop5.php Link to comment https://forums.phpfreaks.com/topic/184947-php-nightmarefor-me-at-least/#findComment-976346 Share on other sites More sharing options...
ngreenwood6 Posted December 13, 2009 Share Posted December 13, 2009 i was bored so i figured I would help you out. I added the stuff at the top so you could see how to use it. the last part i left for you with the array. <?php $dog = new Dog('dog',50); $cat = new Cat('cat',25); $dog->speak(); echo '<br />'; $cat->speak(); abstract class Pet{ private $name; abstract protected function speak(); function __construct($name){ $this->setName($name); } protected function setName($name){ $this->name = $name; } protected function getName(){ return $this->name; } } class Cat extends Pet{ private $weight; function __construct($name, $weight){ $this->setName($name); $this->setWeight($weight); } private function getWeight(){ return $this->weight; } protected function setWeight($weight){ $this->weight = $weight; } public function speak(){ echo 'I am a '.$this->getName().' and I weigh '. $this->getWeight(); } } class Dog extends Pet{ private $weight; function __construct($name, $weight){ $this->setName($name); $this->setWeight($weight); } private function getWeight(){ return $this->weight; } protected function setWeight($weight){ $this->weight = $weight; } public function speak(){ echo 'I am a '.$this->getName().' and I weigh '. $this->getWeight(); } } ?> Link to comment https://forums.phpfreaks.com/topic/184947-php-nightmarefor-me-at-least/#findComment-976350 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.