Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help to do the folowing Workshop of c++ lab (part 1) (100%) Implement four modules for the following classes; Shape, LblShape, Line and Rectangle

Please help to do the folowing Workshop of c++

lab (part 1) (100%)

Implement four modules for the following classes;Shape, LblShape, LineandRectangle

1- TheShapeinterface

Build the following twoPure virtual functions:

apure virtual functionis a virtual function that has no implementation. To indicate that the pure virtual function does not have implementation set its prototype to zero (= 0;) in the class declaration.

draw

Returns void and receives a reference toostreamas an argument. This pure virtual function can not modify the current object.

getSpecs

Returns void and receives a reference toistreamas an argument.

destructor

Builda virtual empty destructor for the shape interface.

this guarantees that any dynamically allocated derived class from the shape interface pointed by a base class pointer will be removed properly from memory when deleted.

Shapehelper functions

Overload the insertion and extraction operators (usingdrawandgetSpecsfunctions) so any shape object can be written or read using ostream and istream.

2- TheLblShapeabstract Class (the Labeled Shape class)

Inherit an abstract class from the interfaceShapecalledLblShape. This class adds a label to aShape.

This class will implement the pure virtual functiongetSpecsbut will not implement the draw function; therefore it remains abstract.

Private Member variable

Add a character pointer member variable calledm_labeland initialize it to null. This member variable will be used to hold the dynamically allocated label for theShape.

Protected members

label()

Add a query calledlabelthat returns the unmodifiable value of m_label member variable.

public members

Default (no argument) constructor

Sets the label pointer to null. (You don't need to perform if them_labelis already initialized to null)

One argument constructor

Allocates memory large enough to hold the incoming Cstring argument pointed by them_labelmember variable. Then copies the Cstring argument to the newly allocated memory.

Destructor

Deletes the memory pointed bym_labelmember variable.

deleted actions

The copy constructor and assignment operator are deleted to prevent copying or assignment of instances of this class.

getSpecs

Reads a comma-delimited Cstring form istream: Override theShape::getSpecspure virtual function to receive a Cstring (a label) fromistreamup to the','character (and then extract and ignore thecomma). Afterward, follow the same logic as was done in the one argument constructor; allocate memory large enough to hold the Cstring and copy the Cstring into the newly allocated memory.

3- TheLineconcrete class

Line inherits theLblShapeclass to Build a horizontal line with a label.

Private Member variable

Build a member variable calledm_lengthto hold the length of theLinein characters.

Default (no argument) constructor

Sets them_lengthmember variable to zero, and invokes the default constructor of the base class.

Two argument constructor

Receives a Cstring and a value for the length of the line. Passes the Cstring to the constructor of the base class and sets them_lengthmember variable to the value of the second argument.

Destructor

This class has no destructor implemented.

getSpecs

Reads comma-separated specs of theLinefrom istream. This function overrides thegetSpecsfunction of the base class as follows. First, it will call thegetSpecsfunction of the base class then it will read the value of the m_length attribute from the istream argument, and then it will ignore The rest of the characters up to and including the newline character' '.

draw

This function overrides the draw function of the base class. If them_lengthmember variable is greater than zero and thelabel()is not null, this function will first print thelabel()and then go to the new line. Afterwards it keeps printing the'='(assignment character) to the value of them_lengthmember variable. Otherwise, it will take no action.

For example, if the Cstring returned by the label query is "Separator" and the length is 40, the draw function should insert the following into ostream:

Separator ======================================== 

3- TheRectangleconcrete class

The Rectangle class inherits theLblShapeclass to build a frame with a label inside.

Private Member variable

Build two member variables calledm_widthandm_heightto hold the width and the height of a rectangular frame (number of characters).

Default (no argument) constructor

Sets the width and height member variables to zero. It will also invoke the default constructor of the base class.

Three argument constructor

Receives a Cstring for the label, and two values for the width and height of theRectanglefrom the argument list. Passes the Cstring to the constructor of the base class and sets them_widthandm_heightmember variables to the corresponding values received from the argument list. However if them_heightis less than 3 orm_widthis less the length of thelabel() + 2it will set the Rectangle to an empty state.

Destructor

This class has no destructor implemented.

getSpecs

Reads comma-separated specs of theRectanglefrom istream. This function overrides thegetSpecsfunction of the base class as follows. First, it will call thegetSpecsfunction of the base class, then it will read two comma-separated values from istream form_widthandm_lengthand then ignores the rest of the characters up to and including the newline character (' ').

draw

This function overrides the draw function of the base class. If the Rectangle is not in an empty state, this function will draw a rectangle with a label inside as follows, otherwise, it will do nothing:

First line: prints '+', then prints the '-' character (m_width - 2) times and then prints '+' and goes to newline.

Second line: prints '|', then in (m_width-2) spaces it prints thelabel()left justified and then prints '|' and goes to new line.

In next (m_height - 3) lines: prints '|', (m_width-2) spaces then prints '|' and goes to new line.

Last line:exactly like first line.

For example, if the Cstring returned by the label query is "Container", the width is 30 and the height is 5, this function should insert the following into ostream:

+----------------------------+ |Container | | | | | +----------------------------+ 

mainModule

/* ------------------------------------------------------ Workshop 8 part 1 Module: N/A Filename: main.cpp Version 1 Author: Fardad Soleimanloo 22/03/17 Revision History ----------------------------------------------------------- Date Reason -----------------------------------------------------------*/ #include  #include  #include "Shape.h" #include "Rectangle.h" #include "Line.h" using namespace sdds; using namespace std; int main() { cout << "Nothing should be printed between these two lines" << endl; cout << "------------------------------------" << endl; Line BadOne; Rectangle BadRectangle("Bad one", 1, 1); cout << "------------------------------------" << endl; Line* L = new Line("Separator", 50); Rectangle* R = new Rectangle("Container", 30, 5); Shape* S[2] = { L, R }; cout << BadOne << BadRectangle; cout << *L << endl; cout << *R << endl; cout << "Enter the following: >Line two,40 >"; cin >> *L; cout << "The following outputs should be the same" << endl; cout << "The correct output:" << endl; cout << "Line two ========================================" << endl; cout << "Your output:" << endl; cout << *L << endl; cout << "Enter the following: >A 50 by 4 frame to be printed on screen,50,4 >"; cin >> *R; cout << "The following outputs should be the same" << endl; cout << "The correct output:" << endl; cout << "+------------------------------------------------+ " "| A 50 by 4 frame to be printed on screen | " "| | " "+------------------------------------------------+" << endl; cout << "Your output:" << endl; cout << *R << endl; cout << "printing Line and Rectangle using Shape pointers: " << endl; cout << *S[0] << endl << *S[1] << endl; delete S[0]; delete S[1]; cout << "Reading the data from a file" << endl; ifstream file("ws8data.csv"); Shape* sptr; char type; while (file) { sptr = nullptr; type = 'x'; file.get(type); file.ignore(); if (type == 'L') { sptr = new Line; } else if(type == 'R') { sptr = new Rectangle; } if (sptr) { file >> *sptr; cout << *sptr << endl; delete sptr; } } return 0; }

Do not modify this module!Walk through the code and make sure you understand it.

Sample Output

Nothing should be printed between these two lines ------------------------------------ ------------------------------------ Separator ================================================== +----------------------------+ |Container | | | | | +----------------------------+ Enter the following: >Line two,40 >Line two,40 The following outputs should be the same The correct output: Line two ======================================== Your output: Line two ======================================== Enter the following: >A 50 by 4 frame to be printed on screen,50,4 >A 50 by 4 frame to be printed on screen,50,4 The following outputs should be the same The correct output: +------------------------------------------------+ | A 50 by 4 frame to be printed on screen | | | +------------------------------------------------+ Your output: +------------------------------------------------+ |A 50 by 4 frame to be printed on screen | | | +------------------------------------------------+ printing Line and Rectangle using Shape pointers: Line two ======================================== +------------------------------------------------+ |A 50 by 4 frame to be printed on screen | | | +------------------------------------------------+ Reading the data from a file line one ========== line two ============================== line three ================================================== line four ====================================================================== +-------------+ |Step four | | | +-------------+ +-----------------------+ |Step three | | | +-----------------------+ +---------------------------------+ |Step two | | | +---------------------------------+ +-------------------------------------------+ |Step one | | | +-------------------------------------------+ 

SubmissionFiles to submit

Shape.h Shape.cpp LblShape.h LblShape.cpp Line.h Line.cpp Rectangle.h Rectangle.cpp main.cpp

We can make this file beautiful and searchable if this error is corrected: It looks like row 5 should actually have 3 columns, instead of 4. in line 4.

L,line one,10
L,line two,30
L,line three,50
L,line four,70
R,Step four,15,4
R,Step three,25,4
R,Step two,35,4
R,Step one,45,4

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

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions

Question

What is American Polity and Governance ?

Answered: 1 week ago

Question

What is Constitution, Political System and Public Policy? In India

Answered: 1 week ago