#include <iostream>
#include <cstring>
using namespace std;
namespace CAR_CONST
{
enum
{
ID_LEN = 20, MAX_SPD = 200, FUEL_STEP = 2, ACC_STEP = 10, BRK_STEP = 10
};
}
class Car
{
private:
char gamerID[CAR_CONST::ID_LEN];
int fuelGauge;
int curSpeed;
public:
void InitMenbers(const char* ID, int fuel);
void ShowCarState();
void Accel();
void Break();
};
void Car::InitMenbers(const char * ID, int fuel)
{
strcpy(gamerID, ID);
fuelGauge = fuel;
curSpeed = 0;
}
void Car::ShowCarState()
{
cout << "소유자ID: " << gamerID << endl;
cout << "연료량: " << fuelGauge << endl;
cout << "현재속도: " << curSpeed << "km/s" << endl << endl;
}
void Car::Accel()
{
if (fuelGauge <= 0)
return;
else
fuelGauge -= CAR_CONST::FUEL_STEP;
if (curSpeed + CAR_CONST::ACC_STEP >= CAR_CONST::MAX_SPD)
{
curSpeed = CAR_CONST::MAX_SPD;
return;
}
curSpeed += CAR_CONST::ACC_STEP;
}
void Car::Break()
{
if (curSpeed < CAR_CONST::BRK_STEP)
{
curSpeed = 0;
return;
}
curSpeed -= CAR_CONST::BRK_STEP;
}
int main()
{
Car run99;
run99.InitMenbers("run99",100);
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
return 0;
}
#include <iostream>
using namespace std;
class MyClass
{
private: // 외부 접근 (95%)X
// 값은 멤버함수를 통해서만 초기화 가능
int private_val;
public: // 외부 접근 O
int public_val;
protected: // 상속에서만 접근 가능
int protected_val;
public: // 멤버변수는 무조건 public
void get()
{
cout << "private_val: " << private_val << endl;
cout << "public_val: " << public_val << endl;
}
void set(int an)
{
private_val = an;
}
};
int main()
{
MyClass o;
// o.private_val;
o.public_val = 10;
// o.protected_val;
o.set(1);
o.get();
MyClass m;
m.public_val = 20;
m.set(2);
m.get();
return 0;
}
classname object;
classname * ptrObj = new classname;
프로그램 실행 과정
//39_Car.h
#pragma once
#ifndef __CAR_H__
#define __CAR_H__
namespace CAR_CONST
{
enum
{
ID_LEN = 20, MAX_SPD = 200, FUEL_STEP = 2, ACC_STEP = 10, BRK_STEP = 10
};
}
class Car
{
private:
char gamerID[CAR_CONST::ID_LEN];
int fuelGauge;
int curSpeed;
public:
void InitMenbers(const char* ID, int fuel);
void ShowCarState();
void Accel();
void Break();
};
#endif // !__CAR_H__
//39_Car.cpp
#include <iostream>
#include <cstring>
#include"39_Car.h"
using namespace std;
void Car::InitMenbers(const char* ID, int fuel)
{
strcpy(gamerID, ID);
fuelGauge = fuel;
curSpeed = 0;
}
void Car::ShowCarState()
{
cout << "소유자ID: " << gamerID << endl;
cout << "연료량: " << fuelGauge << endl;
cout << "현재속도: " << curSpeed << "km/s" << endl << endl;
}
void Car::Accel()
{
if (fuelGauge <= 0)
return;
else
fuelGauge -= CAR_CONST::FUEL_STEP;
if (curSpeed + CAR_CONST::ACC_STEP >= CAR_CONST::MAX_SPD)
{
curSpeed = CAR_CONST::MAX_SPD;
return;
}
curSpeed += CAR_CONST::ACC_STEP;
}
void Car::Break()
{
if (curSpeed < CAR_CONST::BRK_STEP)
{
curSpeed = 0;
return;
}
curSpeed -= CAR_CONST::BRK_STEP;
}
//39_RacingMain.cpp
#include "39_Car.h"
int main()
{
Car run99;
run99.InitMenbers("run99", 100);
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
return 0;
}
#include <iostream>
using namespace std;
class FruitSeller
{
private:
int APPLE_PRICE;
int numofApples;
int myMoney;
public:
void InitMenbers(int price, int num, int money)
{
APPLE_PRICE = price;
numofApples = num;
myMoney = money;
}
int SaleApples(int money)
{
int num = money / APPLE_PRICE;
numofApples -= num;
myMoney += money;
return num;
}
void ShowSalesResult()
{
cout << "남은 사과: " << numofApples << endl;
cout << "판매 수익: " << myMoney << endl << endl;
}
};
class FruitBuyer
{
int myMoney;
int numofApples;
public:
void InitMenbers(int money)
{
numofApples = 0;
myMoney = money;
}
void BuyApples(FruitSeller& seller, int money)
{
numofApples += seller.SaleApples(money);
myMoney -= money;
}
void ShowSalesResult()
{
cout << "현재 잔액: " << myMoney << endl;
cout << "사과 개수: " << numofApples << endl << endl;
}
};
int main()
{
FruitSeller seller;
seller.InitMenbers(1000, 20, 0);
cout << "과일 판매자의 현황: " << endl;
seller.ShowSalesResult();
FruitBuyer buyer;
buyer.InitMenbers(5000);
buyer.BuyApples(seller, 2000);
cout << "과일 판매자의 현황: " << endl;
seller.ShowSalesResult();
cout << "과일 구매자의 현황: " << endl;
buyer.ShowSalesResult();
return 0;
}