[ create a new paste ] login | about

Link: http://codepad.org/XEIFldjx    [ raw code | output | fork ]

scwizard - C++, pasted on Dec 7:
#include <iostream>
#include <string>

class Rectangle {
private:
	static const unsigned int MAX_HEIGHT = 30;
	static const unsigned int MAX_WIDTH = 30;
	static const char FILLING = ' ';
	unsigned int width;
	unsigned int height;
	
	void drawHorizontalSides();
	void drawVerticalSide();
	
public:
	Rectangle(unsigned int w, unsigned int h);
	void draw();
};

Rectangle::Rectangle (unsigned int w, unsigned int h) : width(w), height(h) {
}

/* Draws a ASCII representation of the rectangle to standard output. */
void Rectangle::draw () {
	if(this->width > MAX_WIDTH) {
		std::cout << "Too large of a width, enter in something smaller!";
		std::cout << std::endl << std::endl;
	}
	else if(this->width > MAX_HEIGHT) {
		std::cout << "Too large of a height, enter in something smaller!";
		std::cout << std::endl << std::endl;
	}
	else {
		if(this->width > 0) {
			// Draw top.
			std::cout << " ";
			this->drawVerticalSide();
			std::cout << " " << std::endl;
			
			if(this->height > 0) {
				// Draw sides.
				this->drawHorizontalSides();
				
				// Draw bottom.
				std::cout << "|";
				this->drawVerticalSide();
				std::cout << "|";
			}
		}
		else {
			// If the width is 0, then we only want to draw one horizontal side.
			for(unsigned int n = this->height; n > 0; --n) {
				std::cout << "|" << std::endl;
			}
		}
	}
}

void Rectangle::drawHorizontalSides() {
	std::string spacing(this->width, this->FILLING);
	for(unsigned int n = this->height - 1; n > 0; --n) {
		std::cout << "|" << spacing << spacing << "|" << std::endl;
	}
}

void Rectangle::drawVerticalSide() {
	for(unsigned int n = this->width; n > 0; --n) {
		std::cout << "__";
	}
}

int main(int argc, char* argv[]) {
	Rectangle sample_rectangle(23, 5);
	sample_rectangle.draw();
}


Output:
1
2
3
4
5
6
 ______________________________________________ 
|                                              |
|                                              |
|                                              |
|                                              |
|______________________________________________|


Create a new paste based on this one


Comments: