Answer the given question with a proper explanation and step-by-step solution. C++ Virtual Methods Create the following 3 classes: 1. AppWindow class, containing: - A protected instance of a rectangle class Rect defining the AppWindow's position (x,y) and size (w,h). (You may use the Rect class you developed in the previous lab. In main(), the width and height are assumed to be public and called "w" and "h".) - The following constructors to initialize the dimensions: AppWindow(); // initialize member rect's dimensions to 0 AppWindow( float x, float y, float w, float h ); // initialize member rect's dimensions with the 4 floats AppWindow( const Rect& r ); // initialize member rect's dimensions with r's dimensions - A method to retrieve the member rectangle: const Rect& get_rect(); - A virtual method called resize(). When this method is called, your implementation is supposed to change the dimensions of the internal rectangle of the window. virtual void resize( int w, int h ); 2. CircleWin class, which inherits from AppWindow, containing: - A protected float called "radius". For this class, radius is considered to be the maximum dimension among w and h of the rectangle. - The same three constructors as AppWindow. You should only initialize the value of radius in this class, after using AppWindow's constructor to initialize the inherited rectangle. (Recall the initializer list.) - An overriden resize() method such that it will recompute the radius and print it out like this: "radius: ", where is the maximum dimension. Remember to still change the dimensions of the internal rectangle. 3. RectWin class, which also inherits from AppWindow, containing: - A protected float called "area". The area is equal to w * h. - The same three constructors as AppWindow. You should only initialize the value of area in this class, after using AppWindow's constructor to initialize the inherited rectangle. (Again, recall the initializer list.) - An overriden resize() method such that it will recompute the area and print it out like this: "area: ". Remember to still change the dimensions of the internal rectangle. Upload your three classes in a single header file named App.h. Your code will be tested for correctness using the file virtualMethods.cpp. SAMPLE OUTPUT radius: 4 radius: 10 area: 12 area: 40 virtualMethods.cpp #include #include #include "App.h" using namespace std; int main( int argc, const char* argv[] ) { Rect r( 2, 0, 4, 5 ); vector w; w.push_back( new CircleWin( 1, 1, 3, 2 ) ); w.push_back( new CircleWin( r ) ); w.push_back( new RectWin( 1, 1, 2, 3 ) ); w.push_back( new RectWin( r ) ); for( int i = 0; i < w.size(); i++ ) w[i]->resize( w[i]->get_rect().w, w[i]->get_rect().h * 2 ); return 0; }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter11: Inheritance And Composition
Section: Chapter Questions
Problem 23SA
icon
Related questions
Question

Answer the given question with a proper explanation and step-by-step solution.

C++

Virtual Methods

Create the following 3 classes:

1. AppWindow class, containing:

- A protected instance of a rectangle class Rect defining the AppWindow's position (x,y) and size (w,h). (You may use the Rect class you developed in the previous lab. In main(), the width and height are assumed to be public and called "w" and "h".)

- The following constructors to initialize the dimensions:

AppWindow(); // initialize member rect's dimensions to 0
AppWindow( float x, float y, float w, float h ); // initialize member rect's dimensions with the 4 floats
AppWindow( const Rect& r ); // initialize member rect's dimensions with r's dimensions

- A method to retrieve the member rectangle:

const Rect& get_rect();

- A virtual method called resize(). When this method is called, your implementation is supposed to change the dimensions of the internal rectangle of the window.

virtual void resize( int w, int h );

2. CircleWin class, which inherits from AppWindow, containing:

- A protected float called "radius". For this class, radius is considered to be the maximum dimension among w and h of the rectangle.

- The same three constructors as AppWindow. You should only initialize the value of radius in this class, after using AppWindow's constructor to initialize the inherited rectangle. (Recall the initializer list.)

- An overriden resize() method such that it will recompute the radius and print it out like this: "radius: <max>", where <max> is the maximum dimension. Remember to still change the dimensions of the internal rectangle.

3. RectWin class, which also inherits from AppWindow, containing:

- A protected float called "area". The area is equal to w * h.

- The same three constructors as AppWindow. You should only initialize the value of area in this class, after using AppWindow's constructor to initialize the inherited rectangle. (Again, recall the initializer list.)

- An overriden resize() method such that it will recompute the area and print it out like this: "area: <area>". Remember to still change the dimensions of the internal rectangle.

Upload your three classes in a single header file named App.h. Your code will be tested for correctness using the file virtualMethods.cpp.

SAMPLE OUTPUT
radius: 4
radius: 10
area: 12
area: 40

 

virtualMethods.cpp

#include <iostream>
#include <vector>

#include "App.h"

using namespace std;

int main( int argc, const char* argv[] )
{
   Rect r( 2, 0, 4, 5 );

   vector<AppWindow*> w;

   w.push_back( new CircleWin( 1, 1, 3, 2 ) );
   w.push_back( new CircleWin( r ) );
   w.push_back( new RectWin( 1, 1, 2, 3 ) );
   w.push_back( new RectWin( r ) );

   for( int i = 0; i < w.size(); i++ )
       w[i]->resize( w[i]->get_rect().w, w[i]->get_rect().h * 2 );

   return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 3 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning