Inheritance and Polymorphism

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jackaboo
Forum Newbie
Posts: 3
Joined: Tue Dec 15, 2009 10:55 am

Inheritance and Polymorphism

Post by jackaboo »

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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Inheritance and Polymorphism

Post by AbraCadaver »

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.
jackaboo
Forum Newbie
Posts: 3
Joined: Tue Dec 15, 2009 10:55 am

Re: Inheritance and Polymorphism

Post by jackaboo »

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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Inheritance and Polymorphism

Post by AbraCadaver »

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.
I meant class credit :-)
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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Inheritance and Polymorphism

Post by requinix »

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.
I don't think your teacher will be too pleased to learn that you got the answer from some people on the Internet.

We won't give you the answer but we will help you if you ask specific questions. Preferably those involving some amount of code.
jackaboo
Forum Newbie
Posts: 3
Joined: Tue Dec 15, 2009 10:55 am

Re: Inheritance and Polymorphism

Post by jackaboo »

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 />";
}


?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Inheritance and Polymorphism

Post by Christopher »

Code: Select all

    require_once('pet.php');
    require_once('cat.php');
    
 
    $myarray = array(pet,cat);
    
    for (?????)
 
        {
        print $myarray[$speak];
        print "<br />";
        }
Where's the dog? You need to create instances to assign to the array. And then take a look at the foreach().
(#10850)
Post Reply