Question

Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height;...

Given the following code:

#include <iostream>
#include <iomanip>
using namespace std;

class Animal{
private:
    int height;
    int weight;
public:
    void setHeight(int h){
        height = h;
    }
    int getHeight(){
        return height;
    }
    void setWeight(int w){
        weight = w;
    }
    int getWeight(){
        return weight;
    }
    virtual void makeSound() = 0;
    virtual void eat();
};

class Dog: public Animal{
public:
    void makeSound(){
        cout << "Bow Bow\n";
    }
    void eat (){
        cout <<"The dog is eating\n";
    }
    void Catch(){
        cout << "The dog is catching the ball\n";
    }
};

class Cat: public Animal{
public:
    void makeSound(){
        cout << "Meow, Meow \n";
    }
    void eat(){
        cout << "The cat is eating\n";
    }
    void play(){
        cout << "The cat is playing\n";
    }
};
class Cow: public Animal{
public:
    void makeSound(){
        cout << "Moooooo\n";
    }
    void eat(){
        cout << "The cow is eating\n";
    }
};

int main() {
    Cat *cat = new Cat;
    cat->setWeight(10);
    cat->setHeight(50);

    Dog *dog = new Dog();
    dog->setWeight(20);
    dog->setHeight(70);

    Cow *cow = new Cow();
    cow->setWeight(150);
    cow->setHeight(130);

    dog->Catch();
    dog->makeSound();
    dog->eat();

    cat->play();
    cat->makeSound();
    cat->eat();

    cow->makeSound();
    cow->eat();
    return 0;
}

Show the concept of down casting by having the dog objects to do a catch and Cat objects to do a play inside the loop that is performed in polymorphic form.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <iomanip>

using namespace std;

class Animal {
private:
    int height;
    int weight;
public:
    void setHeight(int h) {
        height = h;
    }

    int getHeight() {
        return height;
    }

    void setWeight(int w) {
        weight = w;
    }

    int getWeight() {
        return weight;
    }

    virtual void makeSound() = 0;

    virtual void eat();
};

class Dog : public Animal {
public:
    void makeSound() {
        cout << "Bow Bow\n";
    }

    void eat() {
        cout << "The dog is eating\n";
    }

    void Catch() {
        cout << "The dog is catching the ball\n";
    }
};

class Cat : public Animal {
public:
    void makeSound() {
        cout << "Meow, Meow \n";
    }

    void eat() {
        cout << "The cat is eating\n";
    }

    void play() {
        cout << "The cat is playing\n";
    }
};

class Cow : public Animal {
public:
    void makeSound() {
        cout << "Moooooo\n";
    }

    void eat() {
        cout << "The cow is eating\n";
    }
};

int main() {
    Cat *cat = new Cat;
    cat->setWeight(10);
    cat->setHeight(50);

    Dog *dog = new Dog();
    dog->setWeight(20);
    dog->setHeight(70);

    Cow *cow = new Cow();
    cow->setWeight(150);
    cow->setHeight(130);

    dog->Catch();
    dog->makeSound();
    dog->eat();

    cat->play();
    cat->makeSound();
    cat->eat();

    cow->makeSound();
    cow->eat();

    Animal *cats[] = {new Cat, new Cat};
    for (int i = 0; i < 2; ++i) {
        ((Cat *)cats[i])->play();
    }

    Animal *dogs[] = {new Dog(), new Dog(), new Dog()};
    for (int i = 0; i < 3; ++i) {
        ((Dog *)dogs[i])->Catch();
    }
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...

    #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...

  • Create a base class and two derived classes. Also, create and call a member function named...

    Create a base class and two derived classes. Also, create and call a member function named communicate() that behaves differently when called by each class. Create a single C++ project (and CPP source file) named animal_communication as follows: Into this source, type the source code for Program 15-16, "Program Output," in Section 15.6, "Polymorphism and Virtual Member Functions," in Ch. 15, "Inheritance, Polymorphism, and Virtual Functions," of Starting Out With C++ From Control Structures Through Objects. Run and debug the...

  • previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print...

    previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() {    //create Animal object    Animal animal("Dog","Mammal",4);    cout<<"Animal object details"<<endl;    cout<<"Name: "<<animal.getName()<<endl;    cout<<"Type: "<<animal.getType()<<endl;    cout<<"# of Legs: "<<animal.getLegs()<<endl;          cout<<endl<<endl;    //create Cat object    Cat cat("byssinian cat","carnivorous mammal",4,"short...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • What is the output of this program? #include <iostream>   using namespace std;   class rect   {       int...

    What is the output of this program? #include <iostream>   using namespace std;   class rect   {       int x, y;       public:       void val (int, int);       int area ()       {           return (x * y);       }   };   void rect::val (int a, int b)   {       x = a;       y = b;   }   int main ()   {       rect rect;       rect.val (3, 4);       cout << "rect area: " << rect.area();       return 0; } a) rect area:7 b) rect area: 12 c) rect area:24 d) none of the...

  • //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...

    //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;        int size;        int capacity;               void resize(){            capacity*=2;            T * newArr = new T[capacity];            for(int i=0;i<size;i++){                newArr[i] = arr[i];            }            delete(arr);            arr = newArr;                   }           public:       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT