생성자 - triangle class
Triangle
실습 예제
ch07ex9_삼각형클래스.cpp
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
class Triangle
{
public:
Triangle(int h, char ch)
{
setHeight(h);
setShape(ch);
}
inline void setHeight(int h)
{
if (h > 0) height = h;
}
inline void setShape(char ch)
{
shape = ch;
}
inline int getHeight() const
{
return height;
}
inline char getShape() const
{
return shape;
}
void displayShape() const;
private:
int height = 1;
char shape = '\0';
};
int main()
{
Triangle t1(5, '*');
t1.displayShape();
int h;
char sh;
cout << "높이: ";
cin >> h;
t1.setHeight(h);
cout << "모양: ";
cin >> sh;
t1.setShape(sh);
t1.displayShape();
cout << "삼각형의 모양은 " << t1.getShape() << "입니다." << endl;
cout << "삼각형의 높이는 " << t1.getHeight() << "입니다." << endl;
return 0;
}
void Triangle::displayShape() const
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j <= i; j++)
{
cout << shape;
}
cout << endl;
}
}
실습 문제
ch07lab8_RGB.cpp
RGB Color 클래스를 작성하시오.
1. 멤버 변수 (private)
2. 생성자 (public)
3. 멤버 함수 (public)
void display() const
void display() const4. main()
Last updated