Question: 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 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 code as necessary.

Create a new class named Cat that derives from the base class Animal.

In the main portion of the program, add code to instantiate the Cat class and telegraph that the Cat class's constructor and destructor are being called.

Note: To do this, you may copy and customize the code that does this for the Dog class, choosing a variable name such as myAnimal2 for the instantiation of the Cat object.

Create a member function called communicate() in the base Animal class, the Cat class, and the Dog class. The communicate() member function should produce "Speak" when called on an instance of Animal, "Woof!" when called on an instance of Dog, and "Meow!" when called on an instance of Cat as follows:

// Class member function for Animal

// Place public function below the constructor

// and destructor in the Animal class.

void communicate() {

cout << "Speak." << endl;

}

// Class member function for Dog

// Place public function below the constructor

// and destructor in the Dog class.

void communicate() {

cout << "Woof!" << endl;

}

// Class member function for Cat

// Place public function below the constructor

// and destructor in the Cat class.

void communicate() {

cout << "Meow!" << endl;

}

In the main portion of your program, instantiate the Animal, Cat, and Dog classes as shown below, and then call the communicate member function on each instance as follows:

int main() {

Animal genericAnimal;

genericAnimal.communicate();

Dog ralph;

ralph.communicate();

Cat fluffy;

fluffy.communicate();

return 0;

}

Save your C++ code in a file named Week5Program_YourName.cpp, and take a screenshot of your running program. Copy your screenshot in to Microsoft Word document.

*Source code for Program 15-16 (See below)

#include using namespace std;

// Animal is a base class. class Animal { public: // Constructor Animal() { cout << "Animal constructor executing. "; } // Destructor virtual ~Animal() { cout << "Animal destructor executing. "; } };

// The Dog class is derived from Animal class Dog : public Animal { public: // Constructor Dog() : Animal() { cout << "Dog constructor executing. "; }

// Destructor ~Dog() { cout << "Dog destructor executing. "; } };

//************************************************* // main function * //*************************************************

int main() { // Create a Dog object, referenced by an // Animal pointer. Animal *myAnimal = new Dog; // Delete the dog object. delete myAnimal; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!