삶은 계란

[C++] X자로 별표 출력하기 본문

C·C++

[C++] X자로 별표 출력하기

삶과계란사이 2021. 2. 15. 15:56

[C++] X자로 별표 출력하기

 

1. 소스코드 설명

엑스(x) 모양을 X자로 출력한 다음, 별표(*) 모양을 X자로 출력하는 소스코드이다.

 

2. 소스코드

// X자로 별표 출력하기

#include <iostream>
using namespace std;

class DrawClass {   // DrawClass 클래스 선언부
	int line;
	char text = 'x';
public:
	DrawClass(int l);
	void draw();
	void setFill(char t);
	void setSize(int s);
};

// DrawClass 클래스 구현부
DrawClass::DrawClass(int l) {
	line = l;
}

void DrawClass::draw()
{
	for (int i = 0; i < line; i++) {
		for (int j = 0; j < line; j++) {
			if (j == i || i + j == line - 1)
				cout << text;
			else
				cout << " ";
		}
		cout << endl;
	}
}

void DrawClass::setFill(char t) {
	text = t;
}

void DrawClass::setSize(int s) {
	line = s;
}

int main()
{
	DrawClass draw(5);   // draw 객체 생성(생성자 호출)
	draw.draw();   // draw() 멤버 함수 호출

	draw.setFill('*');   // setFill() 멤버 함수 호출
	draw.setSize(7);   // setSize() 멤버 함수 호출
	draw.draw();   // draw() 멤버 함수 호출

	return 0;
}

 

3. 실행결과

 

 

Comments