SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Polymorphism And Virtual Functions
Exercise:
Programming Projects
Question:1 | ISBN:9780132846813 | Edition: 5

Question

Consider a graphics system that has classes for various figures—say, rect- angles, squares, triangles, circles, and so on. For example, a rectangle might have data members height, width, and center point, while a square and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system.

The class Figure is the base class. You should add only Rectangle and Triangle classes derived from Figure. Each class has stubs for member functions erase and draw. Each of these member functions outputs a message telling what func- tion has been called and what the class of the calling object is. Since these are just stubs, they do nothing more than output this message. The member function center calls erase and draw to erase and redraw the figure at the center. Because you have only stubs for erase and draw , center will not do any “centering” but will call the member functions erase and draw . Also, add an output message in the member function center that announces that center is being called. The member functions should take no arguments. There are three parts to this project:

a. Do the class definitions using no virtual functions. Compile and test.

b. Make the base class member functions virtual. Compile and test.

c. Explain the difference in results.

For a real example, you would have to replace the definition of each of these member functions with code to do the actual drawing. You will be asked to do this in Programming Project 15.2.

Use the following main function for all testing:

//This program tests Programming Problem 15.1.

#include <iostream>

#include "figure.h"

#include "rectangle.h"

#include "triangle.h"

using std::cout;

int main( )

{

Triangle tri;

tri.draw( );

cout <<

"\nDerived class Triangle object calling center( ).\n";

tri.center( ); //Calls draw and center

Rectangle rect;

rect.draw( );

cout << "\nDerived class Rectangle object calling center( ).\n";

rect.center( ); //Calls draw and center

return 0;

}

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.