0
내 모노 폴리 게임에서 Field
유형의 배열을 포함하는 Property : public Field
및 Board
클래스 파생 클래스 Field
을 기본 클래스로 사용합니다.클래스 디자인 및 상속 문제
class Field {
protected:
int m_position;
public:
Field() { }
Field(int p_position) : m_position(p_position) { }
virtual void showProperty() const { std::cout << "Actuall output"; };
};
다음 클래스는 property.cpp에 필드
class Property : public Field {
float m_price;
public:
Property(int, float);
void showProperty() const;
};
에서 파생 속성입니다
Property::Property(int p_position, float p_price) : Field(p_position), m_price(p_price) { }
void Property::showProperty() const {
std::cout <<
"Position: " << m_position << "\n" <<
"Price: " << std::to_string(m_price) << "\n";
}
지금 board.h
constexpr int BOARD_SIZE = 40;
class Board {
std::unique_ptr<Field[]> m_board;
public:
Board();
Field getField(int index) { return m_board[index]; }
};
와 생성자를 볼 수 있습니다
0 주에서Board::Board() {
m_board = std::make_unique<Field[]>(BOARD_SIZE);
Property test(1, 100);
m_board[0] = test;
}
내가이
int main() {
Board newBoard;
newBoard.getField(0).showProperty();
}
같은 것을 만들고 난 Property::showProperty()
전화를 기대하지만 Field::showProperty()
를 호출합니다. 파생 클래스 함수를 사용하는 이유는 무엇입니까?