You are required to complete the following tasks: Create a AppWindow class that
ID: 3678907 • Letter: Y
Question
You are required to complete the following tasks:
Create a AppWindow class that will contain as member a rectangle class AppRect defining its position and size. AppWindow will have the following two constructors:
/*! Constructor receives the window area size. */
AppWindow ( int x, int y, int w, int h );
AppWindow ( const AppRect& r );
Write a method to retrieve the rectangle:
const AppRect& AppWindow::rect();
Write now the resize() virtual method as described below. Every time this method is called your implementation is supposed to change the coordinates of the internal rectangle of the window.
/*! Method resize will be called by the system everytime the window is resized */
virtual void AppWindow::resize ( int w, int h );
Write now two classes deriving AppWindow: CircleWin, and RectWin. These classes will have correct constructors as needed and will override the resize method such that:
- CircleWin will get the minimum dimension (among w and h) and will print it like this: "radius: <min>", where <min> is the minimum dimension;
- RectWin will print the area like this: "area: <area>".
Upload your three classes as a single header file named Header.h.
Explanation / Answer
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <vector>
class AppRect
{
public:
int x, y, w, h;
AppRect(int x, int y, int w, int h)
{
this->x = x;
this->y = y;
this->w = w;
this->h = h;
}
};
class AppWindow
{
protected:
AppRect* self;
public:
AppWindow()
{
self = new AppRect(0, 0, 0, 0);
}
AppWindow(int x, int y, int w, int h)
{
self = new AppRect(x, y, w, h);
}
AppWindow(const AppRect& r)
{
self = new AppRect(r.x, r.y, r.w, r.h);
}
const AppRect& rect()
{
return *self;
}
virtual void resize(int w, int h)
{
self->w = w;
self->h = h;
}
};
class CircleWin : public AppWindow
{
private:
double radius;
public:
CircleWin(int x, int y, int w, int h)
{
self->x = x;
self->y = y;
self->w = w;
self->h = h;
(w < h) ? radius = w : radius = h;
}
CircleWin(const AppRect& r)
{
self->x = r.x;
self->y = r.y;
self->w = r.w;
self->h = r.h;
(r.w < r.h) ? radius = r.w : radius = r.h;
}
void resize(int w, int h)
{
(w < h) ? radius = w : radius = h;
std::cout << "radius: " << radius << std::endl;
}
};
class RectWin : public AppWindow
{
private:
double area;
public:
RectWin(int x, int y, int w, int h)
{
self->x = x;
self->y = y;
self->w = w;
self->h = h;
area = w*h;
}
RectWin(const AppRect& r)
{
self->x = r.x;
self->y = r.y;
self->w = r.w;
self->h = r.h;
area = r.w*r.h;
}
void resize(int w, int h)
{
self->w = w;
self->h = h;
area = w*h;
std::cout << "area: " << area << std::endl;
}
};
********
using namespace std;
int main(int argc, const char * argv[])
{
AppRect r(2,0,4,2);
vector<AppWindow*> w;
w.push_back(new CircleWin(r));
w.push_back(new RectWin(r));
w.push_back(new RectWin(1,1,2,3));
w.push_back(new CircleWin(1,1,3,2));
cout << w[0]->rect().x << endl;
for (int i = 0; i < w.size(); i++){
w[i]->resize(w[i]->rect().x, w[i]->rect().h * 2);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.