Department of Digital Contents Sang Il Park MFC ์์ํ๊ธฐ Department of Digital Contents Sang Il Park
๊ฐ๋จํ ์ฝ๋ฉ ์ฐ์ต C++๋ฅผ ์ฌ์ฉํ์ฌ, ์ฌ์ฉ์์ ์ ๋ ฅ์ ๋ฐ๋ผ โ1โ์ ์ ๋ ฅํ๋ฉด โSejong Universityโ๋ฅผ ์ถ๋ ฅ โ2โ๋ฅผ ์ ๋ ฅํ๋ฉด โDigital Contentsโ๋ฅผ ์ถ๋ ฅ โ3โ์ ์ ๋ ฅํ๋ฉด โBye~โ๋ฅผ ์ถ๋ ฅํ๊ณ ์ข ๋ฃ ์์ ๊ณผ์ ์ ๋ฌดํ ๋ฐ๋ณต
๊ฐ๋จํ ๋ฉ์์ง ๊ธฐ๋ฐ ํ๋ก๊ทธ๋๋ฐ์ ์ Message Message Loop Message Handler #include <iostream> using namespace std; int main() { int i; while(true) cout<<"Input: "; cin>>i; switch(i) case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; case 3: cout<<"Bye!"<<endl; return 0; default: } Message Loop Message Message Handler
์ข ๋ ๋ฉ์๊ฒโฆ void procedure(int msg) { switch(msg) case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; case 3: cout<<"Bye!"<<endl; exit(0); default: } int main() { int i; while(true) cout<<"Input: "; cin>>i; procedure(i); } return 0;
Windows API API (Application Programming Interface) Win32 ์ด์์ฒด์ ๋ฑ์ ์ ์ดํ๊ธฐ ์ํ ๊ธฐ๋ฅ๋ค์ ๋ชจ์๋์ Library์ ์ผ์ข ์ฃผ๋ก C ํจ์์ ํํ๋ก ๋์ด ์์. Win32 Windows์ฉ API์ ์ด๋ฆ ์ฆ, ์๋์ฐ์์ ๋์๊ฐ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค๊ธฐ ์ํ ๊ธฐ๋ฅ๋ค์ ๋ชจ์๋์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ library Ex.) ์ฐฝ๋ง๋ค๊ธฐ, ๋ฒํผ ๋ฌ๊ธฐ, ๋ฉ๋ด๋ง๋ค๊ธฐ ๋ฑโฆ
A Simple win32 code // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // PURPOSE: Processes messages for the main window. // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) case IDM_ABOUT: // MessageBox(hWnd, _T("haha"), _T("about"), MB_OK); DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); default: return DefWindowProc(hWnd, message, wParam, lParam); } case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... RECT rect; GetClientRect(hWnd, &rect); DrawText(hdc, _T("hello, Windows"), -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); EndPaint(hWnd, &ps); case WM_DESTROY: PostQuitMessage(0); return 0; // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) UNREFERENCED_PARAMETER(lParam); case WM_INITDIALOG: return (INT_PTR)TRUE; if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)FALSE; // // FUNCTION: MyRegisterClass() // PURPOSE: Registers the window class. // COMMENTS: // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTWIN32)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TESTWIN32); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // FUNCTION: InitInstance(HINSTANCE, int) // PURPOSE: Saves instance handle and creates main window // COMMENTS: // In this function, we save the instance handle in a global variable and // create and display the main program window. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; // testWin32.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "testWin32.h" #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN32)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) TranslateMessage(&msg); DispatchMessage(&msg); return (int) msg.wParam;
Code in short BOOL InitInstance(โฆ) { hWnd = CreateWindow(โฆ); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(โฆ) switch (message) case WM_COMMAND: break; case WM_PAINT: case WM_DESTROY: PostQuitMessage(0); default: return; return 0; int APIENTRY _tWinMain(โฆ) { // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) return FALSE; } // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) if (!TranslateAccelerator(โฆ)) TranslateMessage(&msg); DispatchMessage(&msg); return (int) msg.wParam;
Code in short BOOL InitInstance(โฆ) { hWnd = CreateWindow(โฆ); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(โฆ) switch (message) case WM_COMMAND: break; case WM_PAINT: case WM_DESTROY: PostQuitMessage(0); default: return; return 0; int APIENTRY _tWinMain(โฆ) { // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) return FALSE; } // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) if (!TranslateAccelerator(โฆ)) TranslateMessage(&msg); DispatchMessage(&msg); return (int) msg.wParam;
Win32 ํ๋ก๊ทธ๋จ ๊ตฌ์กฐ WinMain(โฆ) ๏ง main ํจ์ { InitInstance(โฆ) ๏ง ์ด๊ธฐํ (ํ์ ๋ง๋ค๊ณ ๋ณด์ฌ์ค) while( GetMessage (โฆ)) ๏ง ๋ฉ์์ง ๋ฃจํ DispatchMessage(โฆ) ๏ง ๋ฉ์ธ์ง์ฒ๋ฆฌ(WinProc) }
MFC ?
MFC ์ฃผ์ ํน์ง (1/3) ์๋์ฐ ์์ฉ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋๋ฐ ๋๋ ์๊ณ ๋ฅผ ํฌ๊ฒ ๋์ด์ค๋ค. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฌ์ฉ AppWizard, ClassWizard API๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ก๊ทธ๋จ๊ณผ ๋๋ฑํ ์๋๋ฅผ ๊ฐ์ง๋ค. ์ธ๋ผ์ธ ํจ์์ ํ์ฉ ์ฝ๋ ํฌ๊ธฐ ์ฆ๊ฐ๋ฅผ ์ต์ํํ๋ค. ๋์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ฉ
MFC ์ฃผ์ ํน์ง (2/3) API ํ๋ก๊ทธ๋๋ฐ์ ๋ํ ๊ธฐ๋ฐ ์ง์์ ์ฌํ์ฉํ ์ ์๋ค. API ํจ์๋ฅผ ์ง์ ํธ์ถํ ์ ์๋ค. (์) ReleaseCapture(); C++ ์ธ์ด๋ฅผ ์ด์ฉํ์ฌ ๊ธฐ์กด์ C ์ธ์ด์ ๋นํด API๋ฅผ ์ข๋ ํธํ๊ฒ ์ฌ์ฉํ ์ ์๋ค. (์) ์ค๋ฒ๋ก๋ฉ ๋ฐ ๋ํดํธ ์ธ์
MFC ์ฃผ์ ํน์ง (3/3) API๋ฅผ ์ง์ ์ฌ์ฉํด์ ๊ตฌํํ ๊ฒฝ์ฐ ๋ณต์ก๋๊ฐ ๋์ ๋ถ๋ถ์ MFC๋ฅผ ์ด์ฉํ๋ฉด ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค. ์ธ์ ๊ธฐ๋ฅ ์ง์, ํด๋ฐ์ ์ํ๋ฐ ์ฒ๋ฆฌ, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ง์, OLE์ ์กํฐ๋ธX, ...
MFC ํ๋ก๊ทธ๋จ ๋ง๋ค์ด ๋ณด๊ธฐ
The simplest MFC application Single Document No Document/View architecture support No database support No ActiveX control No Docking toolbar No Initial status bar
MFC ์์ฉ ํ๋ก๊ทธ๋จ ์์ฑ (8/8) ์คํ
MFC ์์ฉ ํ๋ก๊ทธ๋จ ์์ฑ ์ฝ๋์ ๋ณ๊ฒฝ
Look into the codes
C++? CLASS?
C++ ํฅ์๋ C C + Class C์ ํจ์จ์ฑ + ๊ฐ์ฒด์งํฅ๊ฐ๋ (object-oriented)
์ ์ฐจ์งํฅ๊ณผ ๊ฐ์ฒด์งํฅ ๊ฐ์ฒด์งํฅ์ ๋ฐฐ๊ฒฝ ๊ฐ์ฒด์งํฅ๊ณผ ์ ์ฐจ์งํฅ์ ๋น๊ต ์ํํธ์จ์ด ๋ชจ๋์ ์ฌ์ฌ์ฉ๊ณผ ๋ ๋ฆฝ์ฑ์ ๊ฐ์กฐ ์ ์ฐจ์งํฅ (Procedural-Oriented) : ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๊ทธ ๋ฐ์ดํฐ๋ฅผ ๋ณํ์ํค๋ procedure/function์ผ๋ก ๊ตฌ์ฑ ๊ฐ์ฒด์งํฅ (Object-Oriented) : ๊ฐ์ฒด๋ค์ด ๋ฉ์์ง(message)๋ฅผ ํตํ์ฌ ํต์ ํจ์ผ๋ก์จ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป๋๋ค. ๊ฐ ๊ฐ์ฒด๋ ๊ณ ์ ์ ์์ฑ(attribute)์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋ ๋ฉ์๋(method)๋ก ๊ตฌ์ฑ
์ ์ฐจ์งํฅ๊ณผ ๊ฐ์ฒด์งํฅ ๋น๊ตฌ์กฐ์ ๊ตฌ์กฐ์ /์ ์ฐจ์งํฅ ๊ฐ์ฒด์งํฅ
๊ฐ์ฒด ๊ฐ์ฒด(Object) ํจ์จ์ ์ผ๋ก ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํ์ฌ, ์ฌ๋๋ค์ด ์๋ฏธ๋ฅผ ๋ถ์ฌํ๊ณ ๋ถ๋ฅํ๋ ๋ ผ๋ฆฌ์ ์ธ(๊ฐ๋ ์ ์ธ) ๋จ์ ์ค ์ธ๊ณ์ ์กด์ฌํ๋ ํ๋์ ๋จ์์ ๋ํ ์ํํธ์จ์ด์ ํํ ๊ด๋ จ๋ ๋ณ์์ ํจ์์ ๋ฌถ์ ๊ฐ์ฒด์ ๊ตฌ์ฑ ์์ฑ์ ๊ฐ์ ๋ํ๋ด๋ ๋ฐ์ดํฐ(data) - attribute ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๊ฑฐ๋ ์กฐ์ํ๋ ๊ธฐ๋ฅ(function) โ method
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํน์ง ์บก์ํ (encapsulation) ๋คํ์ฑ (polymorphism) ์์ (inheritance) ๊ฐ๊ฐ์ ์๋ฏธ์ ๋ํด ์ง์์ ์ฑ ์ ์ฐพ์ ๋ณผ ๊ฒ!
๋ฐ์ดํฐ๋ฅผ ๋ฌถ๋ ๋จ์: ๊ตฌ์กฐ์ฒด
๊ตฌ์กฐ์ฒด (structure) ๊ตฌ์กฐ์ฒด: ๊ด๋ จ๋ ์ ๋ณด๋ฅผ ๊ทธ๋ฃนํํ์ฌ ํํ ๊ตฌ์กฐ์ฒด ์ด๋ฆ ํ์ ์ด๋ฆ ํ๋ฒ ์ฃผ์ ์ ํ๋ฒํธ ๋ฉค๋ฒ ๋ณ์
๊ตฌ์กฐ์ฒด์ ์ ์ ๊ตฌ์กฐ์ฒด์ ์ ์ struct ๊ตฌ์กฐ์ฒด์ด๋ฆ { ๋ฉค๋ฒ๋ณ์ํ์ 1 ๋ณ์์ด๋ฆ1; ๋ฉค๋ฒ๋ณ์ํ์ 2 ๋ณ์์ด๋ฆ2; ๋ฉค๋ฒ๋ณ์ํ์ 3 ๋ณ์์ด๋ฆ3; โฆ };
๊ตฌ์กฐ์ฒด์ ์ ์ Example) struct student { char name[30]; int number; float grade; };
๊ตฌ์กฐ์ฒด ํ์ ์ ๋ณ์ ์ ์ธ ๊ตฌ์กฐ์ฒด์ด๋ฆ ๊ตฌ์กฐ์ฒด๋ณ์์ด๋ฆ; struct student { char name[30]; int number; float grade; }; student a, b;
๋ฉค๋ฒ๋ก์ ์ ๊ทผ ๊ตฌ์กฐ์ฒด๋ณ์์ด๋ฆ.๋งด๋ฒ๋ณ์์ด๋ฆ struct score { int korean; int math; }; score a; a.korean = 30; a.math = 80;
๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ ๊ตฌ์กฐ์ฒด๋ฅผ ๋ณ์ํ์ฒ๋ผ ์ฌ์ฉํด์ ํฌ์ธํฐ๋ฅผ ์ ์ ํ ์ ์๋ค struct rectangle { int x,y; int width, height; }; rectangle rc; rectangle * p = &rc;
๊ตฌ์กฐ์ฒด์ ๋งด๋ฒ ๋ณ์ ์ ๊ทผ ๊ตฌ์กฐ์ฒด๋ณ์.๋งด๋ฒ๋ณ์ ๊ตฌ์กฐ์ฒด์์๋: ๊ตฌ์กฐ์ฒดํฌ์ธํฐ๋: (*๊ตฌ์กฐ์ฒดํฌ์ธํฐ).๋งด๋ฒ๋ณ์ ๊ตฌ์กฐ์ฒดํฌ์ธํฐ->๋งด๋ฒ๋ณ์ ๊ตฌ์กฐ์ฒดํฌ์ธํฐ->๋งด๋ฒ๋ณ์
๊ตฌ์กฐ์ฒด์ ๋งด๋ฒ ๋ณ์ ์ ๊ทผ ์ struct rectangle { int x,y; int width, height; }; rectangle rc; rectangle * p = &rc; rc.x=10; (*p).x = 10; p->x = 10; ๋ชจ๋ ๊ฐ๋ค
๋ฐ์ดํฐ์ ๊ทธ ๊ธฐ๋ฅ์ ๋ฌถ๋ ๋จ์:ํด๋์ค
ํด๋์ค์ ์ ์ Point ํด๋์ค๋ฅผ ์ ์ํ๋ ์ // Point ํด๋์ค๋ฅผ ์ ์ํ๋ค. class Point { public: // ๋ฉค๋ฒ ๋ณ์๋ค int x, y; // ๋ฉค๋ฒ ํจ์ void Print() cout << "( " << x << ", " << y << ")\n"; } };
ํด๋์ค์ ์ ์ Point ํด๋์ค๋ฅผ ์ ์ํ๋ ์
๊ฐ์ฒด์ ์์ฑ๊ณผ ์ฌ์ฉ(1) ํด๋์ค ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ฌ์ฉํ๋ ์ // ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. Point pt1, pt2; pt1.x = 100; pt1.y = 100; pt2.x = 200; pt2.y = 200; // pt1, p2์ ๋ด์ฉ์ ์ถ๋ ฅํ๋ค. pt1.Print(); pt2.Print();
๊ฐ์ฒด์ ์์ฑ๊ณผ ์ฌ์ฉ(2) Print() ํจ์์์ ์ฌ์ฉํ๋ x, y์ ์๋ฏธ [21-3]
๋ฉค๋ฒ ํจ์์ ์์น ํด๋์ค์ ์ ์ ๋ฐ๊นฅ์ชฝ์ ๋ฉค๋ฒ ํจ์๋ฅผ ์ ์ํ ์ class Point { public: // ๋ฉค๋ฒ ๋ณ์ int x, y; // ๋ฉค๋ฒ ํจ์ void Print(); }; void Point::Print() cout << "( " << x << ", " << y << ")\n"; }
์์ฑ์์ ์๋ฉธ์ ์์ฑ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์์ด๋ค. ์๋ฉธ์๋ ๊ฐ์ฒด๋ฅผ ์๋ฉธํ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์์ฑ์๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ์ด๊ธฐํ ํ๋ ์ฝ๋๋ฅผ ๋ฃ๊ธฐ์ ์๋ง์ ์ฅ์์ด๋ค. ์๋ฉธ์๋ ๊ฐ์ฒด๋ฅผ ์๋ฉธํ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์๋ฉธ์๋ ๊ฐ์ฒด๊ฐ ์ฌ์ฉํ ๋ฆฌ์์ค๋ฅผ ์ ๋ฆฌํ๋ ์ฝ๋๋ฅผ ๋ฃ๊ธฐ์ ์๋ง์ ์ฅ์์ด๋ค.
๋ํดํธ ์์ฑ์(Default Constructors) ๋ํดํธ ์์ฑ์์ ์ถ๊ฐ class Point { public: int x, y; void Print(); Point(); }; Point::Point() x = 0; y = 0; } // ์ค์ ์คํ ์.. Point pt; // ์์ฑ์๊ฐ ํธ์ถ๋๋ค. pt.Print();
์ธ์๊ฐ ์๋ ์์ฑ์ ์ธ์๊ฐ ์๋ ์์ฑ์์ ์ถ๊ฐ class Point { public: int x, y; void Print(); Point(); Point(int initialX, int initialY); }; Point::Point(int initialX, int initialY) x = initialX; y = initialY; } // ์ค๊ฐ ์๋ต Point pt(3, 5); pt.Print();
์๋ฉธ์ ์๋ฉธ์๋ฅผ ์ฌ์ฉํด์ ํ ๋นํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํด์ ํ๋ ์ class DynamicArray { public: int* arr; DynamicArray(int arraySize); ~DynamicArray(); }; DynamicArray::DynamicArray(int arraySize) // ๋์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๋ค. arr = new int [arraySize]; } DynamicArray::~DynamicArray() // ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํด์ ํ๋ค. delete[] arr; arr = NULL;
๊ฐ์ฒด์ ๋์ ์ธ ์์ฑ ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ์ฌ์ฉํด์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ Point pt(50, 50); Point* p1 = new Point(); // ๋ํดํธ ์์ฑ์ ์ฌ์ฉ Point* p2 = new Point(100, 100); // ์ธ์์๋ ์์ฑ์ ์ฌ์ฉ Point* p3 = new Point( pt); // ๋ณต์ฌ ์์ฑ์ ์ฌ์ฉ p1->Print(); p2->Print(); p3->Print(); delete p1; delete p2; delete p3;
์ ๊ทผ ๊ถํ ์ค์ ํ๊ธฐ(1) ๋ฉค๋ฒ์ ์ ๊ทผ ๊ถํ์ ์ค์ ํ๋ ์ class AccessControl { public: char publicData; void publicFunc() {}; protected: int protectedData; void protectedFunc() {}; private: float privateData; void privateFunc() {}; }; int main() // ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , ๊ฐ ๋ฉค๋ฒ์ ์ ๊ทผํด๋ณด์ AccessControl ac; ac.publicData = 'A'; // ์ฑ๊ณต ac.publicFunc(); // ์ฑ๊ณต ac.protectedData = 100; // ์คํจ ac.protectedFunc(); // ์คํจ ac.privateData = 4.5f; // ์คํจ ac.privateFunc(); // ์คํจ return 0; } ๋ฉค๋ฒ์ ์ ๊ทผ ๊ถํ์ ์ค์ ํ๋ ์
์ ๊ทผ ๊ถํ ์ค์ ํ๊ธฐ(2) ๋ฉค๋ฒ์ ์ ๊ทผ ๊ถํ ์ค์ ํ๊ธฐ ์ ๊ทผ ๊ถํ ํค์๋์ ๋ํ ์์ฝ (๋ค์์ ๋์ฑ ์์ธํ ๋ถ๋ฅ) [21-23] public : ์ธ๋ถ์์์ ์ ๊ทผ์ ํ์ฉํ๋ค. protected, private : ์ธ๋ถ์์ ์ ๊ทผํ ์ ์๋ค. [21-23]
Homework #1 1. C++์์ ์์์ด๋? ๊ฐ์ํจ์๋? 2. ํด๋์ค ๋์์ธ ๋ฐ ์์์ ์ฐ์ต ์ต๋ํ ์์ธํ, ์ฑ์คํ, ์์ ์ ์ธ์ด๋ฅผ ์ด์ฉํ์ฌ ์ค๋ช ์ค์ ์ ์ ํ ์์ ๋ง์ ์(example) 3๊ฐ์ง๋ฅผ ๋ง๋ค์ด ์ค๋ช 2. ํด๋์ค ๋์์ธ ๋ฐ ์์์ ์ฐ์ต ๋ํ(shape) class๋ฅผ ์ ์ํ๊ณ , ์ด๋ฅผ ์์๋ฐ์ ์ฌ๊ฐํ(rectangle), ์(circle) class๋ฅผ ์ ์ํ๊ณ , ๊ฐ๊ฐ์ ํน์ฑ์ ๋ง๋ draw()๋ผ๋ ํจ์๋ฅผ ์ ์ํด ๋ณธ๋ค. ๊ฐ ํด๋์ค๋ง๋ค ์์คํ์ผ๊ณผ ํค๋ํ์ผ์ ๊ฐ๊ฐ(!) ๋ง๋ ๋ค. ๋์ํ๋ code๋ฅผ ์ ์ถ
Homework #1 ํด๋์ค ์์์ ์ฐ์ต MyShape Class ์ ์ํ๊ธฐ MyRect Class ์ ์ํ๊ธฐ ๋งด๋ฒ๋ณ์: float _x,_y ์์ฑ์: MyShape(float x, float y) ๏ _x์ _y์ ๊ฐ์ ์ค์ ๋งด๋ฒํจ์: void Draw() const ์๋์ ๊ฐ์ ๋ด์ฉ ์ถ๋ ฅ [SHAPE] position = ( โ_x๊ฐโ, โ_y๊ฐโ ) MyRect Class ์ ์ํ๊ธฐ MyShape Class๋ก๋ถํฐ ์์ ๋งด๋ฒ๋ณ์: float _width, _height ์์ฑ์: MyRect(float x, float y, float w, float h) ์๋์ ๊ฐ์ ๋ด์ฉ ์ถ๋ ฅ [RECTANGLE] position = ( โ_x๊ฐโ, โ_y๊ฐโ), size = (โ_widthโ, โ_heightโ)
Homework #1 ํด๋์ค ์์์ ์ฐ์ต MyCircle Class ์ ์ํ๊ธฐ MyShape Class๋ก๋ถํฐ ์์ ๋งด๋ฒ๋ณ์: float _radius ์์ฑ์: MyCircle(float x, float y, float radius) ๋งด๋ฒํจ์: void Draw() const ์๋์ ๊ฐ์ ๋ด์ฉ ์ถ๋ ฅ [CIRCLE] position = ( โ_x๊ฐโ, โ_y๊ฐโ), radius = โ_radiusโ
Homework #1 ํด๋์ค ์์์ ์ฐ์ต์ test - ๋ค์๊ณผ ๊ฐ์ main ํจ์๋ฅผ ์ด์ฉ int main() { MyShape* shapes[5] = {NULL}; shapes[0] = new MyCircle( 100, 100, 50); shapes[1] = new MyRect( 300, 300, 100, 100); shapes[2] = new MyRect( 200, 100, 50, 150); shapes[3] = new MyCircle(100, 300, 150); shapes[4] = new MyRect( 200, 200, 200, 200); for (int i = 0; i < 5; ++i) shapes[i]->Draw(); delete shapes[i]; shapes[i] = NULL; } return 0;
Homework #1 ํด๋์ค ๋์์ธ ๋ฐ ์์์ ์ฐ์ต ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋๋ก ํด๋์ค๋ฅผ ๋์์ธํ๋ผ
Homework #1 ์ ์ถ ๋ฐฉ๋ฒ: Moodle ์ ์ถ (moodle.sejong.ac.kr) ๋ค์์ฃผ ์์์ผ ์์ (3์ 21์ผ 23:99)๊น์ง 1. ๋ฆฌํฌํธ ์ ์ถ: ์ ์๋ฌธ์(pdf, ๋๋ doc) 1๋ฒ๊ณผ 2๋ฒ์ ๋ํด ์ต๋ํ ์์ธํ ์ฑ์คํ. 2๋ฒ์ ๊ฒฝ์ฐ ์์ ์ ์ํ ์ฐฉ์ค ๊ณผ์ ๋ฐ ์์ฑ ์ฝ๋์ ๋ํ ์์ธํ ์ค๋ช ์ ํ ๊ฒ 2. Code ์ ์ถ: Project file + source codes๋ฅผ zip์ผ๋ก ์์ถํ์ฌ ์ ์ถ (๋๋ฒ๊ทธ ํด๋๋ฅผ ์ง์ฐ๊ณ ์ ์ถ) ํ ์คํธ ์ build๊ฐ ์๋๋ฉด ์ ์ ์์ Visual Studio .NET 2003, 2005, 2010
Homework #1 ์น๊ตฌ๋ค๊ณผ ํ๋ฐํ ํ ์์ ํ ๋ก ์ ๊ถ์ฅ ๋จ, ๋ณด๊ณ ์๋ ์ฝ๋ฉ์ ์ค์ค๋ก ํ ๊ฒ ๋ณด๊ณ ์๋ code ์ Copy ์ ๋ฐ ์ ์๋ณธ ์ ๊ณต์์ ๋ณต์ฌ์ ๋ชจ๋ ์์ ์ ์ -100%
classes Look into the codes
VC++ Framework CFrameWnd CChildView ์๋์ฐ์ ํ๋ ์(ํ)์ ๊ด๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ฃผ๋ ์๋์ฐ CWinApp : ์์ ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ฌถ์ด ์ฃผ๊ณ , ํ๋ก๊ทธ๋จ์ ๊ตฌ๋ ์ํด(๋์ ์๋ณด์) ๋ฉ์์ง ๋ฃจํ๋ฅผ ๋๋ฆผ
ํ๋ก๊ทธ๋จ ๋ด๋ถ ๊ตฌ์กฐ theApp (CSimpleApp : CWinApp) m_pMainFrame (CMainFrame : CFrameWnd) m_wndView (CChildView : CWnd)
์์ฉ ํ๋ก๊ทธ๋จ ์คํ์์ CSimpleApp theApp; WinMain() // MFC ๋ด๋ถ์ ์จ๊ฒจ์ง { theApp.InitInstance(); // ์ด๊ธฐํ theApp.Run(); // ๋ฉ์์ง ๋ฃจํ theApp.ExitInstance(); // ์ข ๋ฃ }
์์ฉ ํ๋ก๊ทธ๋จ ํด๋์ค (1/4) // Simple.h class CSimpleApp : public CWinApp { virtual BOOL InitInstance(); afx_msg void OnAppAbout(); DECLARE_MESSAGE_MAP() };
์์ฉ ํ๋ก๊ทธ๋จ ํด๋์ค (2/4) MFC ๊ธฐ๋ณธ header file์ ๋ชจ์๋์ #include "stdafx.h" #include "simple.h" #include "MainFrm.hโ // Simple.cpp BEGIN_MESSAGE_MAP(CSimpleApp, CWinApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) END_MESSAGE_MAP() CSimpleApp::CSimpleApp() { } CSimpleApp theApp; MFC ๊ธฐ๋ณธ header file์ ๋ชจ์๋์ // ์์ฉํ๋ก๊ทธ๋จ ์์ ์ ํด๋นํ๋ ์ ์ญ๊ฐ์ฒด
์์ฉ ํ๋ก๊ทธ๋จ ํด๋์ค (3/4) BOOL CSimpleApp::InitInstance() { SetRegistryKey(_T("Local AppWizard-Generated Applications")); CMainFrame* pFrame = new CMainFrame; m_pMainWnd = pFrame; pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL); pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); return TRUE; }
์์ฉ ํ๋ก๊ทธ๋จ ํด๋์ค (3/4) BOOL CSimpleApp::InitInstance() { SetRegistryKey(_T("Local AppWizard-Generated Applications")); CMainFrame* pFrame = new CMainFrame; m_pMainWnd = pFrame; pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL); pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); return TRUE; } 61
์์ฉ ํ๋ก๊ทธ๋จ ์คํ์์ CSimpleApp theApp; WinMain() // MFC ๋ด๋ถ์ ์จ๊ฒจ์ง { theApp.InitInstance(); // ์ด๊ธฐํ theApp.Run(); // ๋ฉ์์ง ๋ฃจํ theApp.ExitInstance(); // ์ข ๋ฃ }
์์ฉ ํ๋ก๊ทธ๋จ ํด๋์ค (4/4) // ๋ํ์์ ๊ด๋ จ ํด๋์ค ์ ์ธ ๋ฐ ์ ์ ๋ถ๋ถ - ์๋ต // ... void CSimpleApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); }
VC++ Framework CFrameWnd CChildView ์๋์ฐ์ ํ๋ ์(ํ)์ ๊ด๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ฃผ๋ ์๋์ฐ CWinApp : ์์ ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ฌถ์ด ์ฃผ๊ณ , ํ๋ก๊ทธ๋จ์ ๊ตฌ๋ ์ํด(๋์ ์๋ณด์) ๋ฉ์์ง ๋ฃจํ๋ฅผ ๋๋ฆผ
ํ๋ ์ ์๋์ฐ ํด๋์ค (1/5) // MainFrm.h class CMainFrame : public CFrameWnd { protected: DECLARE_DYNAMIC(CMainFrame) virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo); virtual ~CMainFrame(); CChildView m_wndView; afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnSetFocus(CWnd *pOldWnd); DECLARE_MESSAGE_MAP() };
ํ๋ ์ ์๋์ฐ ํด๋์ค (2/5) // MainFrm.cpp IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_CREATE() ON_WM_SETFOCUS() END_MESSAGE_MAP() CMainFrame::CMainFrame() { } CMainFrame::~CMainFrame()
ํ๋ ์ ์๋์ฐ ํด๋์ค (3/5) int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL)) TRACE0("Failed to create view window\n"); } return 0;
ํ๋ ์ ์๋์ฐ ํด๋์ค (4/5) BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; cs.dwExStyle &= ~WS_EX_CLIENTEDGE; cs.lpszClass = AfxRegisterWndClass(0); return TRUE; }
ํ๋ ์ ์๋์ฐ ํด๋์ค (4/5) struct CREATESTRUCT{ LPVOID lpCreateParams; HINSTANCE hInstance; HMENU hMenu; HWND hwndParent; int cy; int cx; int y; int x; LONG style; LPCTSTR lpszName; LPCTSTR lpszClass; DWORD dwExStyle; }; 69
ํ๋ ์ ์๋์ฐ ํด๋์ค (5/5) void CMainFrame::OnSetFocus(CWnd* pOldWnd) { m_wndView.SetFocus(); } BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo)) return TRUE; return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
VC++ Framework CFrameWnd CChildView ์๋์ฐ์ ํ๋ ์(ํ)์ ๊ด๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ฃผ๋ ์๋์ฐ CWinApp : ์์ ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ฌถ์ด ์ฃผ๊ณ , ํ๋ก๊ทธ๋จ์ ๊ตฌ๋ ์ํด(๋์ ์๋ณด์) ๋ฉ์์ง ๋ฃจํ๋ฅผ ๋๋ฆผ
๋ทฐ ํด๋์ค (1/4) // ChildView.h class CChildView : public CWnd { public: protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual ~CChildView(); afx_msg void OnPaint(); DECLARE_MESSAGE_MAP() };
๋ทฐ ํด๋์ค (2/4) // ChildView.cpp CChildView::CChildView() { } BEGIN_MESSAGE_MAP(CChildView,CWnd ) ON_WM_PAINT() END_MESSAGE_MAP()
๋ทฐ ํด๋์ค (3/4) BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) { if (!CWnd::PreCreateWindow(cs)) return FALSE; cs.dwExStyle |= WS_EX_CLIENTEDGE; cs.style &= ~WS_BORDER; cs.lpszClass = AfxRegisterWndClass ( CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL); return TRUE; } Style: http://msdn2.microsoft.com/en-us/library/ms632600(VS.85).aspx ExStyle: http://msdn2.microsoft.com/en-us/library/ms632680(VS.85).aspx System Color: http://msdn2.microsoft.com/en-us/library/ms724371.aspx
๋ทฐ ํด๋์ค (4/4) void CChildView::OnPaint() { CPaintDC dc(this); dc.TextOut(100, 100, _T("์๋ ํ์ธ์.โ)); }
์์ฝ ํด๋์ค ์ข ๋ฅ ๋ฒ ์ด์ค ํด๋์ค ์ด๋ฆ ํต์ฌ ํจ์ - ์ฃผ ์ญํ ์์ฉ ํ๋ก๊ทธ๋จ CWinApp InitInstance() - ํ๋ ์ ์๋์ฐ๋ฅผ ์์ฑํ๋ค. Run() - ๋ฉ์์ง ๋ฃจํ๋ฅผ ์ ๊ณตํ๋ค. ํ๋ ์ ์๋์ฐ CFrameWnd OnCreate() - ๋ทฐ๋ฅผ ์์ฑํ๋ค. ๋ทฐ CWnd OnPaint() - ํ๋ฉด์ ์ถ๋ ฅํ๋ค.
Announcement ์ฐ์ต์๊ฐ ์์์ผ ์คํ (6:00~7:30) ์ฅ์: ์จ304ํธ ์ค์ต์ค