Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help me pass all tests. Do not change main.cpp and canvas.h. You can only make changes in a new canvas.cpp file. I REPEAT. DO NOT

Help me pass all tests. Do not change main.cpp and canvas.h. You can only make changes in a new canvas.cpp file.

I REPEAT. DO NOT MAKE CHANGES ON CANVAS.H AND MAIN.CPP

I INCLUDED MY CANVAS.CPP FILE you only need to add a Canvas(string s) and a void add(char x) to the canvas.cpp file

image text in transcribed

---------------canvas.h--------------

#ifndef CANVAS_H #define CANVAS_H

#include

using namespace std;

// In this homework, you'll manipulate ASCII art images // consisting of a rectangular grid of chararacter pixels. class Canvas { public: // Allocates a canvas of the given width and height 5 that // consists entirely of ' ' (space) chars. If the width is 0, // the canvas is empty (pointer is NULL and _width is 0) Canvas(int width);

// Allocates a canvas with width 5 and height 5 that looks like: // // ### #### #### #### // # # # # # # # // ##### or #### or # or # # // # # # # # # # // # # #### #### #### // // depending upon which character ('A', 'B', 'C', or 'D') is // given as a parameter. If some other character is given, // allocates a canvas of ' ' chars with width 5 and height 5. Canvas(char x);

// Returns the width of the canvas. int width();

// Returns the entire canvas as a single string, consisting of each row // of the canvas, followed by the newline character (' '). // If the canvas is empty, returns an empty string. string to_string(); // Replaces every instance in the canvas of old_char with new_char. // For instance, if old_char is '#' and new char is '@', then: // // ### @@@ // # # @ @ // ##### becomes @@@@@ // # # @ @ // # # @ @ // void replace(char old_char, char new_char);

// Destructor. Deallocates all of the memory allocated by the canvas. ~Canvas();

// Allocates a canvas containing the sequence of characters // in the string with 2 columns of space between each pair // of adjacent characters. For instance, Canvas("BADCAB") // should yield: // // #### ### #### #### ### #### // # # # # # # # # # # # // #### ##### # # # ##### #### // # # # # # # # # # # # // #### # # #### #### # # #### // // Any characters in s not from {'A', 'B', 'C', 'D'} should be // replaced with empty 5x5 space, just like previous constructor. // If the string is empty (null string), the canvas is empty // (pointer is NULL and _width is 0) Canvas(string s);

// Adds a character to the Canvas's sequence of characters. void add(char x);

private: // A canvas is represented as a 2D char array, i.e. // an array of pointers to char (sub)arrays. // Each subarray corresponds to a COLUMN of the image. char** C; int _width; };

#endif

--------------------main.cpp---------------------------

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

---------------------my canvas.cpp code----------------------------

#include #include "canvas.h" #include

using namespace std;

int maxr;

Canvas::Canvas(int width){ _width =width; C = new char*[_width]; // setting the double pointer to point to cols for (int hop2 = 0; hop2

Canvas::Canvas(char x) { _width = 5; C = new char*[_width]; for (int j = 0; j

//spaces for(int hop1=0;hop1

C[1][2]='#'; C[2][2]='#'; C[3][2]='#'; }

if((x=='B')||(x=='b')) { //Letter B for(int x1=0;x1

if((x=='C')||(x=='c')){ //Letter C for(int x1=1;x1

}

if((x=='D')||(x=='d')){ //Letter D for(int x1=0;x1

Canvas::~Canvas() { if (C != nullptr) { for (int i = 0; i

string Canvas::to_string() { //add spaces string result; if (_width != 0) { for (int i = 0; i

In this homework, you'll implement a class that represents "ASCII art" images and operations on them, represented as two-dimensional char arrays allocated on the heap (using the new operator). The following files are given to you: 1. A C++ header file (canvas.h) declaring the Canvas class. 2. A C++ source file (main.cpp) containing a main() function with tests. Create a new C++ source file named canvas.cpp that implements the class declared in canvas.h so that canvas.cpp and the provided files compile into a program that runs with no failed tests. "u\---@. + "u \- + \ ." ()[dad 7sa// // Test Canvas(string) Canvas C12("ADD"); test ( C12.width ()==19); + "\# \# \# \# \# \# " + "\#\#\#\#\# \# ### " + "\# \# \# \# \# \# " + "\# \# \#\#\#\# \#\#\#\# ); Canvas C13("VBAD!"); test ( C13.width ()==33); test( C13.to_string ()==string(" \#\#\#\# ###### n" ) + " ###### \# # n" + " \#\#\#\# \#\#\#\#\# \# \# + " ###### \#n" + " \#\#\#\# \# ####(n"); Canvas C14("DAD CAB"); test(C14.width ()==47); + "\# ########### " + "\#\#\#\# \# \#\#\#\#\# #####(n"); cout "Assignment complete." endl

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

Students also viewed these Databases questions