/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <string>
#include "MainHeader.h"
using namespace std;
int main()
{
BaseCar baseCar("심플카", 500L);
for(int loopIdx=0; loopIdx<3; loopIdx++)
{
baseCar.doAccelerator();
baseCar.doAccelerator();
cout << "현재 속도: " << baseCar.getSpeedConst() << "km/h" << endl;
cout << "(에어백 센서 체크)" << endl;
baseCar.checkAirBag();
baseCar.doBreak(25);
}
cout << "==================================================" << endl;
const BaseCar constCar("const카", 500L);
cout << "현재 속도: " << constCar.getSpeedConst() << "km/h" << endl;
//constCar.doAccelerator();//const로 선언된 객체다보니 내부값 변경이 발생하는건 에러.
//constCar.checkAirBag();//read-only로 정의된 함수가 아니다 보니 그냥 막힘.
cout << "==================================================" << endl;
return 0;
}
#ifndef CAR_H_INCLUDE
#define CAR_H_INCLUDE
using namespace std;
class BaseCar
{//부모클래스(혹은 상위 클래스, 기초 클래스, 슈퍼 클래스등으로 불림)
private://상속시 유전 안 됨.
//클래스 멤버에 대한 가시성 지시어 기본값이 private라 생략해도 가능은 함.
//nothing.
protected://상속시 유전 됨.
const string modelName;
int maxSpeed;
int currentSpeed;
int feul;
long price;
public://상속시 유전 됨. 외부용 인터페이스(특히 생성자/소멸자)
BaseCar(string argModelName, long argPrice, int maxSpeed=90): modelName{argModelName}
{//생성자.
price = argPrice;
//modelName = argModelName;//const로 선언된터라 함수명 옆에 있는 초기화 리스트에서 처리.
feul = 100;
this->maxSpeed = maxSpeed;//this가 C++에도 가능했구나!
}
void doAccelerator()
{
if(feul==0){ return; }
feul--;
currentSpeed+=30;
}
void doBreak(int argPress)
{
if(currentSpeed==0){ return; }
currentSpeed -= argPress;
if(currentSpeed<0){ currentSpeed=0; }
}
int getSpeedConst() const
{//read-only인 멤버함수. 해당 함수내에서는 변수 변경자체가 금지.
return currentSpeed;
}
void checkAirBag()
{
if(currentSpeed>=maxSpeed){ cout<< "에어백 강제 실행 될수 있는 속도입니다." << endl; }
}
~BaseCar()
{//소멸자.
currentSpeed=0;
feul=0;
cout<< "폐차되었습니다. [차량:" << modelName << "]"<< endl;
//반환타입이 없으니 return 불가. 매개변수X
}
};
#endif//CAR_H_INCLUDE