Help with fill in code exercise

I have a "fill in the ..." on my online C++ book, where they give some code and you have to fill the ...
with the correct code (then you click a Code Check button to see if it runs).

Anyway i am stuck on a particular exercise:


You are given a Rectangle class that produces a string which, when printed, displays a rectangle with a fancy border. The interior of the rectangle contains spaces. Write a derived class FilledRect whose to_string method replaces the spaces with a fill character.
The rectangle string and fill character can contain Unicode characters that occupy multiple bytes. However, each space contains a single byte.


the code they give is given as:
filledrect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "filledrect.h"

FilledRect::FilledRect(int h, int w, string fill_char)
...

string FilledRect::to_string() const
{
	string rect = ...;
	string result = '';
	for (int i = 0; i < rect.length(); i++)
	{
		if (rect[i] == ' ')
		{
			...
		}
		else
		{
			...
		}
	}
	return result;
}



filledrect.h
1
2
3
4
5
6
7
8
9
10
#include "rectangle.h"

class FilledRect : public Rectangle
{
public:
	FilledRect(int h, int w, string fill_char);
	string to_string() const;
private:
	...
};


prog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>

#include "filledirect.h"

using namespace std;

int main()
{
	Rectangle* rects[3];
	rects[0] = new Rectangle(10, 4);
	rects[1] = new FilledREct(10, 4, "/");
	rects[2] = nre FilledRect(5, 5, u8"\U000025AF");
	for (int i = 0; i < 3; i++)
		cout << rects[i]->to_string() << "\n";
	return 0;
}



rectangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

using namespace std;

class Rectangle
{
public:
	/**
		Constructs a rectangle of the given width and height.
	*/
	Rectangle(int q, int h);
	int get_width() const;
	int get_height() const;
	virtual string to_string() const;
private:
	int width;
	int height;
};



They don't give the rectangle.cpp file. So from what i can tell they want me to override the "to_string()" method from the Rectangle class. For some reason i really confused about the blanks they want me to fill in. The constructor for FilledRect i'm guessing is supposed to call the construct from the Rectangle class and fill in the width and height parameters, and also set a string variable to fill_char for the "to_string()" method to use? I feel like if they gave the rectangle.cpp i would be able to copy this code into visual studios and figure out what to do, but as of right now i'm lost and everything i try just gets me initializer errors with FilledRect constructor.
You shouldn't need to see rectangle.cpp. The header file contains all the info that you need. In particular, you shouldn't care how Rectangle::to_string() works. You only care that it does what it's supposed to do.

Although the assignment doesn't say so, it appears from Prog.cpp that the Unicode string is encoded in UTF-8.
So from what i can tell they want me to override the "to_string()" method from the Rectangle class.
Correct. In fact, filledrect.cpp contains a skeleton of the new to_string() method. You just need to fill in the missing code.

i really confused about the blanks they want me to fill in
It's pretty simple. You just need to call Rectangle::to_string() to get the string with spaces. Then change the spaces to to the fill string. The only hard part is that the fill string can be a multi-byte character.

The constructor for FilledRect i'm guessing is supposed to call the construct from the Rectangle class and fill in the width and height parameters, and also set a string variable to fill_char for the "to_string()" method to use?
100% correct. You'll need to add a private member to FilledRect to store the fill_char.

everything i try just gets me initializer errors with FilledRect constructor
FilledRect's constructor needs to tell the compiler how to call Rectangle's constructor:
1
2
3
4
FilledRect(int h, int w, string fill_char): Rectangle(h,w)
{
    ...
}

I got it! thank you!
Topic archived. No new replies allowed.