πŸ“‘ 개인 ν™œλ™ λͺ¨μŒμ§‘ πŸ“‘/β›Ί `23 ν•˜κ³„ C++ ν”„λ‘œκ·Έλž˜λ° λΆ€νŠΈ μΊ ν”„ β›Ί

[6μ°¨μ‹œ 유제] 면적 계산 κΈ°λŠ₯을 κ°€μ§„ Rectangle 클래슀λ₯Ό μž‘μ„±ν•˜κ³  전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ.

lrycro_ 2023. 7. 10. 19:45

Q. λ‹€μŒ main() ν•¨μˆ˜κ°€ 잘 μž‘λ™ν•˜λ„λ‘ ν”„λ‘œκ·Έλž˜λ° ν•˜μ‹œμ˜€.

- λ„ˆλΉ„(width)와 높이(height) λ³€μˆ˜λ₯Ό ν™œμš©

- 면적 계산 κΈ°λŠ₯을 κ°€μ§„ Rectangle 클래슀λ₯Ό μž‘μ„±ν•˜κ³  전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ.

int main() {
  Rectangle rect;
  rect.width = 3;
  rect.height = 5;
  cout << "μ‚¬κ°ν˜•μ˜ 면적은 " << rect.getArea() << endl;
}

A.

#include <iostream>
using namespace std;

class Rectangle
{
	public:
		int width, height;
		int getArea();
};

int Rectangle::getArea()
{
	return width * height;
}

int main()
{
	Rectangle rect;
	rect.width = 3;
	rect.height = 5;
	cout << "μ‚¬κ°ν˜•μ˜ 면적은 " << rect.getArea() << endl;
}