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...

  • #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;...

  • #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...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

  • Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class...

    Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class Product { private:    // private variables    int id_number;    char pDescription[40];    int mId;    double productPrice;    double productMarkUp;    int qty; public:    // constructor    Product() {        id_number = 0;        strcpy_s(pDescription, "");        mId = 0;        productPrice = 0.0;        productMarkUp = 0.0;        qty = 0;    }   ...

  • Hello, I ran into some issues with my code and wanted to ask if anyone can...

    Hello, I ran into some issues with my code and wanted to ask if anyone can help me fix them. Thank you in advance! Question: You and your team of software developers are experiencing problems saving the state of a program between different times that the program is run. An object oriented program state can be maintained by saving the state of the objects in the program. Also, you can transfer the state of an object between different computer systems...

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
ADVERTISEMENT