Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C + + Not sure how to set up to derive and override the line class from the shape class and how to get the

C++ Not sure how to set up to derive and override the line class from the shape class and how to get the line class to draw itself on the console window.
Add a Line class to the Graphics project. The Line class should derive from Shape.
Add 1 Point2D field called endPt. Add getter/setter methods for the field.
Add a constructor that takes a start Point2D, end Point2D, and console color. Make sure to call the base constructor. Dont duplicate what the base constructors do.
Implement the pseudocode for the PlotLine (see the pseudo-code for PlotLine below). NOTE: youll need to implement the Plot method it should move the cursor to the x,y
position and prints a space.
Override the draw method of the Shape class (that means you need to mark the base as virtual). Do not call the base. Instead, set the background color, call PlotLine, then reset
the color.
PlotLine(x0, y0, x1, y1)
dx = abs(x1- x0)
sx = x0< x1?1 : -1
dy =-abs(y1- y0)
sy = y0< y1?1 : -1
error = dx + dy
while true
Plot(x0, y0)
if x0== x1 && y0== y1 break
e2=2* error
if e2>= dy
if x0== x1 break
error = error + dy
x0= x0+ sx
end if
if e2<= dx
if y0== y1 break
error = error + dx
y0= y0+ sy
end if
end while
In main (which is in Graphics.cpp), add code to case 2 of the menu switch. Generate 2 random Point2D points with an x,y anywhere in the console. Use those points to create a
Line instance with any color you want. Call draw on the Line instance.
Point2D.h code
struct POINT2D
{
int x;
int y;
POINT2D(int x_point, int y_point) : x(x_point), y(y_point){}
};
Shape.h code
class Shape
{
private:
// fields
POINT2D startPt;
ConsoleColor color;
public:
// first constructor
Shape(POINT2D startPt_point, ConsoleColor color_point) : startPt(startPt_point), color(color_point){}
//2nd constructor
Shape(int x, int y, ConsoleColor color_point) : startPt(x, y), color(color_point){}
// Getters for Point2D & ConsoleColor
POINT2D GetStartPt() const {
return startPt;
}
ConsoleColor GetColor() const {
return color;
}
// Setters for Point2D & ConsoleColor
void SetStartPt(POINT2D startPt_point){
startPt = startPt_point;
}
void SetColor(ConsoleColor color_point){
color = color_point;
}
virtual void draw(){
// SET BACKGROUND COLOR
Console::SetBackgroundColor(Magenta);
// MOVE CURSOR TO POINT2D POSITION
Console::SetCursorPosition(startPt.x, startPt.y);
// PRINT A SPACE
Console::Write("");
// RESET THE COLOR. CALL THE RESET METHOD ON THE CONSOLE CLASS.
Console::Reset();
}
};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

What is the GAAP definition or accounts receivable?

Answered: 1 week ago