본문 바로가기

분류 전체보기167

C++ 상속 날씨 예보를 위한 WeatherPrediction 클래스class WeatherPrediction{public: virtual void setCurrentTempFahrenheit(int inTemp); virtual void setPositionOfJupiter(int inDistanceFromMars); virtual int getTomorrowTempFahrenheit(); virtual double getChanceOfRain(); virtual void showResult(); virtual std::string getTemperature() const;protected: int mCurrentTempFahrenheit; int mDistanceFromMars;};#include "Weather.. 2024. 10. 2.
const 클래스의 데이터 멤버를 const로 선언하면, 생성 시점에 초깃값을 부여한 다음 더는 값을 변경할 수 없게 된다. 그런데 객체 수준에서 상숫값을 보유하는 것은 대부분은 메모리 낭비이다.이때는 static const 멤버를 이용해서 객체간에 상숫값을 공유하도록 할 수 있다.예를 들어 스프레드 시트가 가질수 있는 가로 / 세로 최대크기를 관리해야한다면 당므과 같이 static const 멤버로 선언하여 사용자가 최댓값이상으로 스프레드 시트를 키우려고 할 때 최댓값 까지만 설정되도록 강제할 수 있다.class Spreadsheet {public : static const int kMaxHeight = 100; static const int kMaxWidth = 100; }상수값들은 다음처럼 생성자에서도 이용될.. 2024. 10. 2.
C++ 생성자 생성자class SpreadsheetCell{ public: SpreadsheetCell(double initialValue); SpreadsheetCell(string initialValue);}SpreadsheetCell::SpreadsheetCell(double initialValue){ setValue(initialValue);}SpreadsheetCell::SpreadsheetCell(string initialValue){ setString(initialValue);}스택생성자SpreadsheetCell myCell(5), anotherCell(4);cout힙 객체와 생성자SpreadsheetCell *myCellp = new SpreadsheetCell(5);SpreadsheetCell *.. 2024. 10. 2.
OOP OOP실 세계의 객체를 어떻게 모델링 하는가?작업단위 x 물리적 객체들의 모델들로 쪼갠다.물리적 객체 : class, component, property, behavior클래스 : 계층, 분류, 유형IS-A 관계HAS-A 관계다형성Polymorphism : 표준적인 프로퍼티/행동 집합을 정의해두고, 그것을 따르는 객체들이라면 그 중 어느 객체를 이용하든 정상적으로 이용할 수 있다는 개념.NOT-A관계실 세계에서는 매우 비슷하더라도 코드 상에서는 공통기능이 없는 경우다중상속이 꺼려지는 이유 :1: 다중 상속 관계는 그림을 나타내기에 복잡함2: 간명할 수도 있는 클래스 구조를 망가뜨릴 수 ㅣㅇㅆ다.3: 구현이 까다롭다.첨가 클래스 Mix-in 클래스c++에서는 첨가 클래스가 문법적으로 다중상속과 같다. 하지.. 2024. 10. 2.