Resize a square
Nov 1, 2020 at 1:56pm UTC
Hey, In a part of my programm I want to resize a square.
This is my code:
const int MAX = 50;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
class square {
public :
square();
void print();
private :
int height, width;
};
square::square(void ) {
print();
}
void square::print() {
int i, j;
for (i=1; i<MAX; i++) {
for (j=1; j<=MAX; j++) {
if (i==1 || i==MAX || j==1 || j==MAX) {
cout << "+" ;
}
else {
cout << " " ;
}
}
cout "\n" ;
}
}
In the constructor square() I want to resize the height and width of the square to (for example) 20. How do I do that?
Nov 1, 2020 at 2:14pm UTC
You don't seem to be setting height/width anywhere? Why use MAX? Shouldn't you be using height/width here? But as this is a square and not a rectangle, why 2 sizes and not 1?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
include <iostream>
using namespace std;
class square {
public :
square() {};
square(int s);
void print() const ;
void resize(int s);
private :
int size {5};
};
square::square(int s) : size(s) {}
void square::resize(int s)
{
size = s;
}
void square::print() const {
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
if (i == 1 || i == size || j == 1 || j == size) {
cout << "+" ;
} else {
cout << " " ;
}
}
cout << "\n" ;
}
}
int main()
{
square s(5);
s.print();
s.resize(20);
s.print();
}
Topic archived. No new replies allowed.