Can anyone help me with this assignment, I want to get the right answer then work through it to undestand.
thanks!
create an abstract base class called Pet. This class will have one attribute called name.
Then I want to Create set and get methods for name and 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.
Inheritance and Polymorphism
Moderator: General Moderators
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Inheritance and Polymorphism
Will we get credit for the course?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Inheritance and Polymorphism
Of course! I'll attribute all of the help I had in completing this assignment to this fantastic group. I always attribute where I finally got the answers from.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Inheritance and Polymorphism
I meant class creditjackaboo wrote:Of course! I'll attribute all of the help I had in completing this assignment to this fantastic group. I always attribute where I finally got the answers from.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Inheritance and Polymorphism
I don't think your teacher will be too pleased to learn that you got the answer from some people on the Internet.jackaboo wrote:Of course! I'll attribute all of the help I had in completing this assignment to this fantastic group. I always attribute where I finally got the answers from.
We won't give you the answer but we will help you if you ask specific questions. Preferably those involving some amount of code.
Re: Inheritance and Polymorphism
ok I'm stuck at the end....
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.
<?php
abstract class Pet{
private $name;
function __construct($name) {
$this->name =$name;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
abstract protected function speak();
}
?>
<?php
require_once('Pet.php');
class Cat extends Pet {
private $weight;
function __construct($name, $Weight) {
Pet::__construct($name);
$this->Weight =$Weight;
}
public function setWeight($Weight) {
$this->Weight = $Weight;
}
public function getWeight() {
return $this->Weight;
}
public function speak() {
return $this->getName();
}
}
$animal = new Pet("Fido");
$heavy = new Cat("10");
echo $animal->getName();
print " I'm a dog and I weigh ";
echo $heavy->getWeight();
?>
<?php
require_once('pet.php');
require_once('cat.php');
$myarray = array(pet,cat);
for (?????)
{
print $myarray[$speak];
print "<br />";
}
?>
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.
<?php
abstract class Pet{
private $name;
function __construct($name) {
$this->name =$name;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
abstract protected function speak();
}
?>
<?php
require_once('Pet.php');
class Cat extends Pet {
private $weight;
function __construct($name, $Weight) {
Pet::__construct($name);
$this->Weight =$Weight;
}
public function setWeight($Weight) {
$this->Weight = $Weight;
}
public function getWeight() {
return $this->Weight;
}
public function speak() {
return $this->getName();
}
}
$animal = new Pet("Fido");
$heavy = new Cat("10");
echo $animal->getName();
print " I'm a dog and I weigh ";
echo $heavy->getWeight();
?>
<?php
require_once('pet.php');
require_once('cat.php');
$myarray = array(pet,cat);
for (?????)
{
print $myarray[$speak];
print "<br />";
}
?>
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Inheritance and Polymorphism
Code: Select all
require_once('pet.php');
require_once('cat.php');
$myarray = array(pet,cat);
for (?????)
{
print $myarray[$speak];
print "<br />";
}(#10850)