int * arr1 = new int[3];
int * ptr1 = new int(3);
int *ptr = new int;
int &ref = *ptr;
ref = 20;
cout << *ptr << endl; // 20
#include <iostream>
using namespace std;
namespace CAR_CONST
{
enum
{
ID_LEN = 20,
MAX_SPD = 300,
FUEL_STEP = 2,
ACC_STEP = 10,
BRK_STEP = 10
};
}
using namespace CAR_CONST;
struct Car
{
char gamerID[ID_LEN];
int fuelGauge;
int curSpeed;
void ShowCarState()
{
cout << "ID : " << gamerID << endl;
cout << "Fuel : " << fuelGauge << '%' << endl;
cout << "Current Speed : "<< curSpeed << "km/s" << endl;
}
void Accel()
{
if (fuelGauge <= 0) return;
else fuelGauge -= FUEL_STEP;
if(curSpeed+ACC_STEP >= MAX_SPD)
{
curSpeed=MAX_SPD;
return;
}
curSpeed += ACC_STEP;
}
void Break()
{
if(curSpeed < BRK_STEP)
{
curSpeed = 0;
return;
}
curSpeed -= BRK_STEP;
}
};
int main(void)
{
Car run99 = {"run99", 100, 0};
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
Car sped77 = {"sped77", 100 ,0};
sped77.Accel();
sped77.Break();
sped77.ShowCarState();
run99.Accel();
run99.Break();
run99.ShowCarState();
return 0;
}
#include <iostream>
using namespace std;
namespace CAR_CONST
{
enum
{
ID_LEN = 20,
MAX_SPD = 300,
FUEL_STEP = 2,
ACC_STEP = 10,
BRK_STEP = 10
};
}
using namespace CAR_CONST;
struct Car
{
char gamerID[ID_LEN];
int fuelGauge;
int curSpeed;
void ShowCarState();
void Accel();
void Break();
};
void Car::ShowCarState()
{
cout << "ID : " << gamerID << endl;
cout << "Fuel : " << fuelGauge << '%' << endl;
cout << "Current Speed : "<< curSpeed << "km/s" << endl;
}
void Car::Accel()
{
if (fuelGauge <= 0) return;
else fuelGauge -= FUEL_STEP;
if(curSpeed+ACC_STEP >= MAX_SPD)
{
curSpeed=MAX_SPD;
return;
}
curSpeed += ACC_STEP;
}
void Car::Break()
{
if(curSpeed < BRK_STEP)
{
curSpeed = 0;
return;
}
curSpeed -= BRK_STEP;
}
int main(void)
{
Car run99 = {"run99", 100, 0};
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
Car sped77 = {"sped77", 100 ,0};
sped77.Accel();
sped77.Break();
sped77.ShowCarState();
run99.Accel();
run99.Break();
run99.ShowCarState();
return 0;
}
C++ 에서 파일 분할
#define __CAR__H__
#ifdef __CAR__H__
using namespace std;
namespace CAR_CONST
{
enum
{
ID_LEN = 20,
MAX_SPD = 300,
FUEL_STEP = 2,
ACC_STEP = 10,
BRK_STEP = 10
};
}
using namespace CAR_CONST;
class Car
{
private:
char gamerID[ID_LEN];
int fuelGauge;
int curSpeed;
public:
void InitMembers(char * ID, int fule);
void ShowCarState();
void Accel();
void Break();
};
#endif
#include <iostream>
#include <cstring>
#include "Car.h"
using namespace std;
void Car::InitMembers(char * ID, int fuel)
{
strcpy(gamerID, ID);
fuelGauge = fuel;
curSpeed = 0;
}
void Car::ShowCarState()
{
cout << "ID : " << gamerID << endl;
cout << "Fuel : " << fuelGauge << '%' << endl;
cout << "Current Speed : "<< curSpeed << "km/s" << endl;
}
void Car::Accel()
{
if (fuelGauge <= 0) return;
else fuelGauge -= FUEL_STEP;
if(curSpeed + ACC_STEP >= MAX_SPD)
{
curSpeed = MAX_SPD;
return;
}
curSpeed += ACC_STEP;
}
void Car::Break()
{
if(curSpeed < BRK_STEP)
{
curSpeed = 0;
return;
}
curSpeed -= BRK_STEP;
}
그리고 main에서 클래스 .h 헤더를 선언하고 프로그래밍한다.
인라인 함수는 헤더파일과 함께 넣어야 하니 주의한다.
private:
const int a = 1000; // x
const int a; // o
#include <iostream>
using namespace std;
class SimpleClass
{
private:
int num1;
int num2;
public:
SimpleClass()
{
num1 = 0;
num2 = 0;
}
SimpleClass(int n)
{
num1 = n;
num2 = 0;
}
SimpleClass(int n1, int n2)
{
num1 = n1;
num2 = n2;
}
SimpleClass(int n1 = 0, int n2 = 0;)
{
num1 = n1;
num2 = n2;
}
/*
void ShowData() const
{
cout << num1 << ' ' << num2 << endl;
}
*/
}
int main(void)
{
SimpleClass sc1;
sc1.ShowData();
SimpleClass sc2(100);
sc1.ShowData();
SimpleClass sc3(100, 200);
sc1.ShowData();
return 0;
}
#include <iostream>
using namespace std;
class SimpleClass
{
private:
int num1;
int num2;
public:
SimpleClass() : num1(0), num2(0)
{
cout << num1 << ' ' << num2 << endl;
}
SimpleClass(int n) : num1(n), num2(0)
{
cout << num1 << ' ' << num2 << endl;
}
SimpleClass(int n1, int n2) : num1(n1), num2(n2)
{
cout << num1 << ' ' << num2 << endl;
/*SimpleClass(int n1=0, int n2=0) : num1(n1), num2(n2)
{
cout << num1 << ' ' << num2 << endl;
} 안됨 */
};
int main(void)
{
SimpleClass sc1;
SimpleClass sc2(100);
SimpleClass sc3(100, 200);
return 0;
}
SimpleClass * s2 = new SimpleClass(1, 3);
delete s2;
this -> name = 10;
ObjRef& name()
{
return *this
}
SoSimple slim2(slim1)
exlicit SoSimple(const SoSimple ©) // & 반드시 있어야 복사생성자
: num1(copy.num1), num2(copy.num2)
{
}
#include <iostream>
using namespace std;
class SoComplex
{
private:
static int cmxObjCnt;
public:
SoComplex()
{
cmxObjCnt++;
cout << cmxObjCnt << "번 째 SoComplex 객체" << endl;
}
SoComplex(SoComplex ©)
{
cmxObjCnt++;
cout << cmxObjCnt << "번 째 copy SoComplex 객체" << endl;
}
};
int SoComplex::cmxObjCnt = 0;
int main(void)
{
SoComplex cmx1;
SoComplex cmx2=cmx1;
SoComplex();
return 0;
}
cout << SoSimple::simObjCnt << endl;
const SoSimple sim(2);
#include <iostream>
using namespace std;
class SoSimple
{
private:
int num;
public:
SoSimple(int n) : num(n) {}
SoSimple& AddNum(int n)
{
num += n;
return *this;
}
void ShowData() const
{
cout << "num : " << num << endl;
}
};
int main(void)
{
const SoSimple obj(7);
// obj.AddNum(20);
obj.ShowData();
return 0;
}
const static int RUSSIA = 1807540;
#include <iostream>
using namespace std;
class SoSimple
{
private:
int num1;
mutable int num2;
public:
SoSimple(int n1, int n2)
: num1(n1), num2(n2)
{}
void ShowSimpleData() const
{
cout << num1 << ", " << num2 << endl;
}
void CopyToNum2() const
{
num2 = num1;
}
};
int main(void)
{
SoSimple sm(1, 2);
sm.ShowSimpleData();
sm.CopyToNum2();
sm.ShowSimpleData();
return 0;
}