Page 1 of 1

Inheritance and Polymorphism

Posted: Tue Dec 15, 2009 10:57 am
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.

Re: Inheritance and Polymorphism

Posted: Tue Dec 15, 2009 11:23 am
by AbraCadaver
Will we get credit for the course?

Re: Inheritance and Polymorphism

Posted: Tue Dec 15, 2009 1:12 pm
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.

Re: Inheritance and Polymorphism

Posted: Tue Dec 15, 2009 1:25 pm
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 :-)

Re: Inheritance and Polymorphism

Posted: Tue Dec 15, 2009 2:49 pm
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.

Re: Inheritance and Polymorphism

Posted: Wed Dec 16, 2009 2:16 pm
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 />";
}


?>

Re: Inheritance and Polymorphism

Posted: Wed Dec 16, 2009 2:48 pm
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().