728x90

이론 80

Cpp 클래스 템플릿 부분 특수화 예제

출처 : KGCA 게임 아카데미(http://www.kgcaschool.com/). 수업 예제 파일. OriginalTemplate.h #pragma once // 어차피 멤버 함수를 클래스 템플릿 외부에 정의해서 다른 파일로 분리할 수 없으니 // 클래스 템플릿 안에다가 인라인으로 넣어버림. // 기본 타입 클래스 템플릿 template class Bag { T* elem; int size; int max_size; public: void add(T t) { T* tmp; if (size + 1 >= max_size){ max_size *= 2; tmp = new T[max_size]; for (int iCnt = 0; iCnt < size; iCnt++) { tmp[iCnt] = elem[iCnt];..

Cpp 함수 템플릿 예제

출처 : KGCA 게임 아카데미(http://www.kgcaschool.com/). 수업 예제 파일. 참조 사이트 FunctionTemplates.h #pragma once #include #include #include //기본적인 함수 템플릿 template T add(T x, T y) { T result = x + y; return result; } //함수 템플릿의 특수화 template char* add(char* x, char* y) { char* result = new char[255]{ 0, }; strcat_s(result,100, x); strcat_s(result,100, y); return result; } //함수 템플릿을 특수화한 것 template double add(doubl..

Cpp 예외클래스 예제

출처 : KGCA 게임 아카데미(http://www.kgcaschool.com/). 수업 예제 파일. MSDN 예외처리 부분 MSDN Exception class TODO : 더 자세히 공부할 것(..) ExceptionHandlingSample.cpp #include using std::endl; using std::cout; using std::cin; //exception const char* account = "1234-5678"; //계좌번호 int sid = 1122; //비밀번호 int balance = 1000; //잔액 int sk = 0; class AccountExpt //예외 클래스 { char acc[10]; int sid; int secretkey; public: void wha..

Cpp 다형성 예제(2) (직원 급여 관리 예제)

Employee.h #pragma once #include #include #include using std::cout; using std::cin; using std::endl; using std::string; class Employee //기본 추상 클래스 { protected: stringm_sName; intm_iPositionLevel; public: string getName() const { return m_sName; } virtual int getPay() = 0;//순수가상함수 //급여를 계산하는 함수 void PrintData(); Employee(string name, int PL); }; Employee.cpp #include "Employee.h" void Employee::P..

Cpp 다형성 예제(1)

Figure.h #pragma once #include using std::cout; using std::endl; class Figure //기본 부모 클래스 { protected: int x; //중심의 x좌표 int y; //중심의 y좌표 int width; //가로길이 int height; //세로길이 public: void move(int x, int y); void resize(int width, int height); virtual void draw();//가상함수 void shape(); //가상함수가 아님 public: Figure(); Figure(int x, int y, int width, int height); virtual ~Figure(); }; Figure.cpp #includ..

Cpp 상속 예제

출처 : KGCA 게임 아카데미(http://www.kgcaschool.com/). 수업 예제 파일. parents.h #pragma once #include using std::cout; using std::endl; class parents { private: int money; public: void Character(); //성품 출력 void Appearance(); //외모 출력 void Wealth(); //재산 출력 parents(); //생성자 parents(int money); //생성자 virtual ~parents(); //소멸자 }; parents.cpp #include "parents.h" void parents::Character() { cout

Cpp 복사 생성자 예제 (explicit, mutable)

출처 : KGCA 게임 아카데미(http://www.kgcaschool.com/). 수업 예제 파일. explicit : 생성자 앞에 붙여서 묵시적 형변환을 금지하는 키워드mutable : 변수 앞에 붙여서 const함수에서도 값이 변경될 수 있도록 하는 키워드. classes.h #pragma once #include class class_A { private: int m_iValue; public: int Get() const { return m_iValue; } class_A(int iValue) : m_iValue(iValue) {} class_A(class_A& copy) : m_iValue(copy.m_iValue) {} ~class_A() { } }; class class_E { privat..

Cpp Friend 클래스 예제 (문자열 쪼개기)

header.h #pragma once #include #include #include #include #include #include #include #define SPA _T(" ") using std::vector; using std::string; using std::stringstream; using std::getline; using std::wcout; using std::endl; using std::cout; value.h #pragma once #include "value.h" //예외 클래스 인듯. class EXP { public: void show(TCHAR* pData) { wcout m_szValue = new TCHAR[iSize]; _tcscpy_s(m_szValue, iS..

Cpp 연산자 오버로딩 예제

출처 : KGCA 게임 아카데미(http://www.kgcaschool.com/). 수업 예제 파일. pt.h #pragma once #include using namespace std; //const char& operator []() const; //char& operator [] (); //두 함수는 별개의 함수가 된다. ... const 함수냐 아니냐의 차이. class pt { static int dynamic; int x, y; public: void SetPosition(int _x, int _y); void Move(int _x, int _y); void Show(); //포인터 연산자 재정의 pt* operator->(); pt& operator* (); //증가 연산자 재정의 pt op..

Cpp 클래스 예제 (2) 사각형 충돌체크

Region.h #pragma once #include //const char* p = 0; //비상수 포인터, 상수 데이터 //char* const p = 0; //상수 포인터, 비상수 데이터 //const char* const p = 0; //상수 포인터, 상수 데이터 //void a() const { } // 상수 함수 : 함수에서 멤버 변수 값 변경 불가능 //const 함수에서는 const 함수만 호출 가능 //const int a() { }// 반환값이 상수, 상수 변수로만 반환 값을 받을 수 있음. class Region { public: enum { POINT_POINT, POINT_SIZE };//시작점과 끝점, 시작점과 길이 int m_iWidth; int m_iHeight; priva..

반응형