Professional Documents
Culture Documents
VC BookExample
VC BookExample
VC BookExample
C++
Programming
:
SDI MDI
TIMER
Thread
ISBN
VC 6
VS 2003
VS 2005
Visual C++
.
Visual C++
?
C
C
?
C + + C
C++
Visio C
C++ 2
C++
C ++
C++ C++
CVSSP
(
) Face Verification Competition on the
XM2VTS Database 2003
http://www.face -rec.org/
2005
Who is Who in the World Who is Who in Science and Engineering (
) 30
II
Visual C++
(Hungarian Notation)
int
nID
char
ch
chMessage
float
fl
flCurrency
double
dSalary
unsigned
uCode
long
lMoney
BOOL
bIsDraw
WORD
wSize
DWORD
dw
dwError
(Pointer)
pButtonOK
m_
m_nID
sz
szName
wnd
wndButtonName
Name
m_pszName
III
C++
C++ C+
+
C++
nTotal int
Visual C++
(Memory Leak)
C++
C ++
C++ C++
_tmain
IV
Visual C++
/
Unicode
2 Ascii 1 Visual
C++ Unicode
_TCHAR
char
CHAR
TCHAR
#ifdef UNICODE
typedef WCHAR TCHAR;
#else
typedef char TCHAR;
#endif
LPVOID
LPCSTR
PCSTR
PSTR
PVOID
LPSTR
BYTE
DWORD
CALLBACK
HANDLE
HDC
HWND
C++
C++ C+
+
C ++
_T( )
Visual C++
1 2551
sanun@mut.ac.th
51 10530
02-9883655, 02-9883666 () 02-9883687
ISBN: 978-974-8242-37-8
VI
Visual C++
Visual C++
Visual C++
C/C++
Visual C++
Visual C++ Visual C++
Visual C++
(Commercial Application) Visual C++
. , 2551
VII
Visual C++
(First Edition)
8
Visual C++
1 C C++
void*
2 this
3
4 /
/
5 C D C
6 TIMER
WM_TIMER
7
C++
C ++
C++ C++
8
http://www.cpe.mut.ac.th
VIII
Visual C++
IX
C++
C++ C+
+
C++
..
Visual C++
C++
C++
C++ C++
1 C++
1.1 C++
1.2 C++
1.3 C C++
1.4 C++
1.5
1.6 (Reference Variable)
1.7 (Double Indirection)
1.8 C++
1.8.1 (Function)
1.8.2
1.8.3
1.8.4 (Pass by Value)
1.8.5 (Pass by Reference)
1.8.6 (Pass using Pointer)
1.8.7 (Function Pointers)
1.8.8 (Inline Function)
1.8.9 (Overloading Function)
1.8.10 void (void*)
2 C++
45
2.1
2.2
2.3
X
2
3
4
5
8
16
18
22
23
24
25
25
27
29
30
37
40
41
46
48
56
Visual C++
2.4 (Default Constructor)
59
2.5 (Overloading Constructors)
61
2.6 new
63
2.7
65
2.8 this
70
2.9
72
2.10 (Friend Class and Function)
73
2.11 (Class Relationship)
75
2.11.1 (Inheritance)
76
2.11.2
79
2.12
88
2.13
90
2.14 (Virtual Function)
93
2.15 (Abstract Base Class)
96
99
100
105
107
107
108
109
110
115
117
119
129
XI
C++
C++ C+
+
C++
3 Visual C++
3.1
3.2 (Handler Pattern)
3.2.1 (The Headless Handlers Pattern)
3.2.2
3.2.3 (Event Queue)
3.2.4 (Messaging Systems)
3.3
(Object-Oriented Event Driven Programming)
3.3.1 (Message and Event)
3.4
3.4.1 Win32 MFC
3.4.2 (The Message Map)
Visual C++
3.4.3
3.4.4
3.4.5
135
138
141
4 /
149
4.1 SDI
4.2 /
4.3 /
4.3.1 (Role of the Document)
4.3.2 (Role of the View)
4.3.3 (Role of Frame Windows)
4.3.4
4.3.5 ID
4.4 / (Document/View Creation)
4.5 (The Document Object)
4.6 SDI MDI
4.7 (The View Object)
4.7.1
4.7.2 CView
4.7.3
4.7.4
4.8 (The Frame Window Object)
4.9 (Command Routing)
150
154
155
155
157
157
158
165
166
167
174
175
180
182
183
186
190
193
C++
C++
C++ C++
5
5.1
5.2
XII
197
198
201
C++
XIII
C++ C+
+
C++
Visual C++
5.2.1 CPaintDC
203
5.2.2 CClientDC CWindowDC
205
5.3 OnDraw
207
5.4
210
5.5 CDC
211
5.5.1
211
5.5.2 (Pixel manipulation)
212
5.5.3
214
5.5.4
218
5.5.5
219
5.5.6 GDI
224
5.5.7 GDI
225
5.5.8 (Pen)
227
5.5.9 (Stock Object)
230
5.5.10 (Brush)
231
5.5.11
235
5.5.12
236
5.5.12.1
238
5.5.12.2
239
5.5.12.3
239
5.5.12.4
240
5.6
242
5.7
244
5.8 (Drawing Mode)
245
5.9 (Mapping Mode)
248
5.9.1 (Windows and Viewports)
250
5.9.2
251
5.9.3
252
Visual C++
5.9.4 (Coordinate Conversion)
5.9.5
254
254
6 TIMER
259
6.1 TIMER
6.2
6.3 WM_TIMER
6.4
6.5
6.5.1
6.5.2
6.5.3
6.5.4
6.5.5
6.5.6
C++
C++
C++ C++
7
7.1
7.2
7.3 (Creating Worker Thread)
7.3.1
7.4 (Creating User Interface Thread)
7.4.1
7.5
7.6
7.7
7.8
7.9 CWinThread
XIV
260
261
266
270
272
273
300
301
302
303
304
307
308
314
316
318
327
329
340
340
341
342
343
Visual C++
7.10
344
7.11 (Thread Synchronization)
345
7.11.1 Critical Sections
346
7.11.2 Mutexes
348
7.11.3 Events
348
7.11.4 Semaphores
350
7.11.5 CSingleLock CMulitLock
351
7.11.6
351
7.11.7 (Thread Safe)
352
7.12
354
372
374
378
379
380
380
381
382
383
385
386
386
387
388
389
390
XV
C++
8.1
8.2
8.3
8.4
8.4.1 (Flynn's Taxonomy)
8.4.1.1 (Single Instruction Single Data-SISD)
8.4.1.2 (Single Instruction Multiple Data-SIMD)
8.4.1.3 (Multiple Instruction Single Data Stream-MISD)
8.4.1.4 (Multiple Instruction Multiple Data-MIMD)
8.5
8.6
8.6.1 (Decomposition)
8.6.1.1 (Data Parallelism)
8.6.1.2 (Task Parallelism)
8.6.2 (Amdahls Law)
8.6.2.1 (Scaling and Speedup)
371
C++ C+
+
C++
392
392
393
396
396
398
405
412
429
438
C++
C++
C++ C++
Visual C++
8.6.2.2 N
8.6.3
8.6.3.1 (Mutual Exclusion and Locks)
8.7
8.7.1
8.7.2 PI
8.7.3
8.7.4
XVI
Chapter
C++
1
C++
C++
void
* &
Part I
1 C++
1.1 C++
C++
C++
C++
C++ C++
C C++
Visual C++ (Compiler)
/ (Document/View)
Visual C++
(Microsoft Foundation Class-MFC)
/
1 C++
C C++ C++ C++
void*
2
1 C++
/
2 GDI
TIMER
C++
C++ [1]
C++
C++ Bjarne Stroustrup 1979
(C with Classes) C++ 1983
C++
C++
C++ 1998 ISO/IEC
14882:1998 C++ ISO/IEC 14882:2003
C++
C++ C++
C C
C C++ C++
(Compiler)
Visual C++
C C++ C++
C (Structure)
C
C++ C+
+
C ++
1.2 C++
1 C++
myRectangle.Left = 100;
myRectangle.Top = 20;
myRectangle.Right = 200;
myRectangle.Bottom = 400;
C++
C++
myRectangle.Width();
Visio C++
(Class) (Encapsulation) (Inheritance)
(Polymorphism) (Overloading Function)
C++
C++
C++ C++
1.3 C C++
C
C
C++
C++ C C
4
1 C++
C++ C
C++
C
C
C
C &
C C++ ()
C++
C++
C++
C C++
C C
Visual C++
Visual C++
(Multi-Tasking)
Visual C++ MFC
Win32
C++ Visual C++
C++ C+
+
C ++
1.4 C++
1 C++
C C
C++
C C++ C++
C C
Visual C++ /
C++
C++ Visual C++
.exe Visual C++
C++ JDK Visual C++
Paintbrush
C++
C++
C++ C++
C++
C++ C
C
C C++
C++
[2]
p = (int *)malloc( 10 * sizeof(int) );
6
1 C++
if( p != NULL) {
}
NULL 0 if( p != NULL)
if( p != 0 ) C C++
0
0
C C++
if( p != NULL ) if( p )
if( p = (int *)malloc( 10 * sizeof(int) ) ) {
C++
C++ C+
+
C ++
malloc p ( p
) p NULL if
malloc
p if
1 C++
(Message)
Visual C++
(Exception Handling)
C++ try catch throw
t r y { .. . }
(NULL pointer exception) (Throw) (Catch)
c a t c h
C++
1.5
C++
C++
C++ C++
C C++
(Pointer) C++
Available Memory = Physical Memory + Virtual Memory
1 C++
= +
(Segment)
(General Protection Faults)
(Unrecoverable Application Errors)
1-1
(Stack)
(
)
(Heap)
Part I: Basic Idea & Concept
C++
Block Started by
Symbol/
1-1
C++ C+
+
C ++
BSS
1 C++
(First in, Last out)
(R e t u r n
Address)
(Local Variable)
(Call
Stack)
(Execution Stack) (Control Stack) (Function
Stack) (Run-Time Stack)
1-2
(Return Address)
(Return Address)
C++
C++
C++ C++
.
.
10
1 C++
(Recursive Function) 1-2 A
B A
B A A
(Unload)
11
C++
C++ C+
+
C ++
1 C++
)
(Dynamic Data Structure)
malloc free C new delete C++
(Run Time)
1-3
(Logical
Memory Model)
A
A
1000000
main
int _tmain(int argc, _TCHAR* argv[]) {
C++
C++
C++ C++
int A[1000000];
}
1-4
12
1 C++
1-4
* ;
int* a;
a
int
malloc.h
malloc()
void *malloc( size_t size );
(int *) void*
Part I: Basic Idea & Concept
13
C++
C++ C+
+
C ++
m a l l o c ( )
void* ( void* )
int* float*
(Explicit Type Casting)
1 C++
int* int* void*
malloc 0 (NULL) malloc
size byte
x
C++ new
malloc
a = new int[10];
10 int
a
*(a + 1) a[1]
1 1 int 4 1
1 4
a a 1
malloc new
1-5
0F005A
0F005A
C++
C++
C++ C++
10
( )
a = new int(10);
int* a;
1-5 int a
int* a int
14
1 C++
a int
a = new int(10);
a a
1900*1200 2,280,000
unsigned char* p;
p = new unsigned char[1900 * 1200];
p unsigned char *
1900*1200
1-5
1-1 NewVector
int *NewVector(int n) {
int *tmp, i;
if( !(tmp = new int[n]) ) {
printf("Not enough memory to allocate buffer\n");
return NULL;
}
for(i=0; i<n; i++) {
tmp[i] = i;
return tmp;
}
Part I: Basic Idea & Concept
15
C++
C++ C+
+
C ++
1 C++
void DeleteVector(int *v) {
delete v;
}
#define N 10
int _tmain(int argc, _TCHAR* argv[]) {
int *v1, *v2, i;
v1 = NewVector(N);
v2 = NewVector(N);
for(i=0; i<N; i++) {
printf("v1[%d] x v2[%d] = %d\n", i, i, (v1[i]*v2[i]) );
}
printf("Press any key to deallocate pointer!\n");
DeleteVector(v1);
DeleteVector(v2);
return 0;
}
1-1 NewVector
NewVector int
C++
C++
C++ C++
1 C++
& = lvalue;
(lvalue) a x[i]
*ptr
int b = 10;
int& a = b;
b a
a (A l i a s
Name) b
int& wrong = 200;
int& opt = (2 + 3 - 5);
int& ref;
int c = 20;
c rc
17
C++
int& ref
ra = c c ra
C++ C+
+
C ++
ra = c;
1 C++
1-6
1-6
int a = 10;
int& x = a;
a
x
10
C + +
C++
C++
C++ C++
int A[1000][1000];
(D i r e c t
Access) &x
x 20
ptr (*ptr) x x
(Indirect) ptr * *ptr ptr
ptr x x
18
1 C++
int x = 20;
x
ptr
int* ptr=NULL;
int** pptr = new int*;
p &x
*pptr ptr
ptr = &x;
*pptr = ptr;
** ;
**
*pptr = ptr *pptr ptr
x
**pptr
1-7
double** A = new double[1000][1000];
Part I: Basic Idea & Concept
19
C++
*ptr
x
x *ptr
x **pptr
C++ C+
+
C ++
1 C++
1-7
20
x
pptr
ptr
20
*ptr
*pptr
20
**pptr
**
float** A;
( R x C)
A = new float*[R];
new
C++
C++
C++ C++
malloc
1-8
20
1 C++
A[0]
A = new float*[R];
1-8
1
A[R-1]
for (int y=0; y<R; y++) {
A[y] = new float[C];
A[y] = (float *) malloc((unsigned) (C)*sizeof(float));
if (!A[y]) {
printf("Cant allocate memory for mat[%d]", y);
}
}
A[y] A[y]
1-9
A[R-1]
21
C++
A[0][C-1]
1-9
C++ C+
+
C ++
A[0]
1 C++
1-2
R+1
1-2
double** newMatrix(int R, int C) {
double* tmp = new double[R*C];
double** M = new double*[R];
for(int y=0; y<R; y++) {
M[y] = tmp + (y * C);
for(int x=0; x<C; x++)
M[y][x] = 0.0;
R*C
R
M[y] tmp + (y * C) y
C
}
return M;
}
1-2 double* tmp = new
double[R*C] R*C tmp
double** M = new double*[R]
t m p C
tmp + (y * C) y tmp C
C++
C++
C++ C++
1.8 C++
C++
22
1 C++
void (void *)
1.8.1 (Function)
(Local Variable)
1-10
1-10
(Header) (Body)
{ }
return_type function_name(type arg1, type arg2, )
23
C++
C++ C+
+
C ++
return_type
function_name
type arg1
1 C++
1.8.2
C C++
;
int factorial(int n);
#include
1-3
int fac(int n);
C++
C++
C++ C++
}
}
1-3
24
1 C++
m a i n
1.8.3
(Compile Time)
fac(5);
fac 5 5
n ( 1-3 ) fac
C + +
(By Value) (By Reference)
(Pointer)
25
C++
C++ C+
+
C ++
1 C++
1-4
int cube(int d) {
d = d * d * d;
return d;
}
int _tmain(int argc, _TCHAR* argv[]) {
int x=3,y;
x
x
y = cube(x);
printf("x=%d, y=%d", x, y);
return 0;
}
cube cube
(Stack Frame) (Push)
(Pop) cube(x)
x d cube d
x x d
1-4 y 27 x 3
1-11
C++
C++
C++ C++
1-11
cube
3
x
d
cube
26
1 C++
void setFlag(bool bFlag = true) {
}
setFlag(false)
bFlag = false setFlag()
bFlag = true
Visual C++
10
20
1-12
27
C++
C++ C+
+
C ++
C++
&
1-5 &
swapnum(a, b)
& i j swapnum
a b
1-12
1 C++
1-5
void swapnum(int &i, int &j);
int _tmain(int argc, _TCHAR* argv[]) {
int a = 10;
int b = 20;
swapnum(a, b);
printf("A is %d and B is %d\n", a, b);
return 0;
}
void swapnum(int &i, int &j) {
int temp = i;
i = j;
j = temp;
}
i a j b
int& maxint(int& x, int& y) {
return (x>y ? x : y);
maxint
}
int a = 10, b = 20 ;
maxint(a, b) = 15 ;
C++
C++
C++ C++
maxint(a, b) += 10;
maxint(a, b)++;
maxint
maxint(a, b) = 15 b 15 maxint(a, b)++
28
1 C++
& *
29
C++
a x (*a)++ a
x
C++ C+
+
C ++
1 C++
1-13
1-13
x
10
Addr: 1000
Inc( &x );
void Inc(int *a) {
(*a)++;
}
C++
C++
C++ C++
C++
C + +
(* ) ( );
1 C++
(*func) int int
2 (*func)
int int 2
typedef
31
C++
C++
(Late Binding)
(Callback Function)
C++
(
)
C++ C+
+
C ++
1 C++
&
&
( )
int average(int x, int y) {
return ((x+y)/2.0);
}
func = &average;
&
func = average;
func average
& &
(* )( );
( );
C++
C++
C++ C++
average
(*func)
32
1 C++
d o I t ( )
()
doIt() doIt()
d o I t ( )
s w i t ch - c as e
switch-case 1-7
switch-case
1-7 switch-case
float Plus(float a, float b) {
printf("do Plus Operator\n");
return a + b;
}
float Minus(float a, float b) {
printf("do Minus Operator\n");
return a - b;
}
float Multiply(float a, float b) {
printf("do Multiply Operator\n");
return a * b;
}
float Divide(float a, float b) {
return a / b;
}
Part I: Basic Idea & Concept
33
C++
C++ C+
+
C ++
1 C++
void doOperatorSelection(float a, float b, char opCode) {
float result;
switch(opCode) { // execute operation
case '+' :
result = Plus(a, b);
break;
case '-' :
result = Minus(a, b);
break;
case '*' :
result = Multiply(a, b);
break;
case '/' :
result = Divide(a, b);
break;
}
printf("(%.2f, %.2f) = %.2f\n", a, b, result);
}
int _tmain(int argc, _TCHAR* argv[]) {
printf("running a doOperatorSelection\n");
doOperatorSelection(2, 5,'+');
doOperatorSelection
return 0;
C++
C++
C++ C++
}
1-7 switch-case opCode
opCode + Plus opCode /
Divide
switch-case
34
1 C++
doOperatorSelection 2
1-14
(*pt2Func)
Divide
35
C++
C++ C+
+
C ++
callPointer2Func(Divide, 2, 5);
1 C++
1-14 callPointer2Func(Divide, 2, 5) (*pt2Func)
0041104B () Divide
p t2 Fu n c(a, b )
0041104B Divide
switch-case
1-8
void doUpper(char* ch, int num) {
(*func)
printf("%s\n", ch);
C++
C++
C++ C++
}
int _tmain(int argc, _TCHAR* argv[]) {
doWordProcessing(doUpper);
}
36
doUpper
1 C++
1-8 (Word Processing)
doWordProcessing
switch-case
1-8
(*func)
doWordProcessing
doUpper doUpper
doWordProcessing
doWordProcessing(doUpper);
doWordProcessing(doLower);
doSpelling doWordProcessing
doWordProcessing(doSpelling);
(Execution Code)
Part I: Basic Idea & Concept
37
C++
C++ C+
+
C ++
Visual C++
1 C++
(Caller)
1-15
main()
func_called()
func_called()
func_called()
func_called_duty
func_called_duty
func_called_duty
func_called()
C++
C++
C++ C++
}
inline
1-16 func_called (Linker)
( func_called_duty) func_called()
38
1 C++
main()
func_called()
func_called_duty
func_called_duty
func_called_duty
1-16
inline
func_called()
func_called_duty
func_called_duty
func_called_duty
func_called()
func_called_duty
func_called_duty
func_called_duty
C C++
int MIN(int a, int b) {
39
C++
C++ C+
+
C ++
(Preprocessor)
printf(minimum is %d, (10<5 ? 10 : 5));
1 C++
C++
C++
C++ C++
a n int
double
double power(double a, double n) {
double ans = 1.0;
for(int i=0; i<n; i++)
40
1 C++
ans *= a;
return ans;
}
power int double
41
C++
i n t * i n t * v o i d *
v o i d *
void (void *) (Universal Type)
C++ C+
+
C ++
1 C++
void* p;
int* a;
p = a;
a = (int *)p;
void
int
p a
a p int *
void
( )
(Genericness)
1-9
typedef int (*END_FN)(void* any, int index);
int arblen(void* any, END_FN is_end) {
int len = 0;
while( !is_end(any, len) )len++;
return len;
}
int int_end(int* a, int i) {
return (a[i] == -1);
}
int str_end(char* a, int i) {
return (a[i] == '\0');
}
int _tmain(int argc, _TCHAR* argv[]) {
char a[ ] = "abcdefg";
C++
C++
C++ C++
int b[ ]={0,1,2,3,4,-1};
int len = arblen(a, END_FN(str_end)); printf("length of string is %d\n", len);
len = arblen(b, END_FN(int_end));
}
42
1 C++
1-9
arblen
arblen (Generic Function) is_end
\0 char -1
int arblen is_end
arblen void*
arblen
( )
END_FN(str_end) str_end END_FN
void* int
43
C++
C++ C+
+
C ++
d*)
voi
1 C++
1. C++ C
2. if
int a = 0;
if( a = 0) printf("a=%d", a);
3.
4.
5. malloc a = (int *)malloc(...)
6.
int *a;
a = (int *)malloc(10*sizeof(int));
7. (Double Pointer)
8.
C++
C++
C++ C++
9.
10. void*
44
Chapter
C++
2
2 C++
2.1
C++
C++
C++
C++ C++
(Procedure Oriented Decomposition)
(Language of Problem)
(Language of Computer)
46
47
C++
C++ C+
+
C ++
2 C++
(Object
-Oriented Decomposition)
(Data Hiding)
public (Public Interface)
(Encapsulation)
C++
C++
Win32 ( )
MFC ()
2 C++
2.2
integer float char double
C++
typedef enum
[3]
C
class
public:
public
C++
C++ C++
C++
...
...
private:
private
...
...
};
48
CImage
(Class Declaration)
classname.h
class
2 C++
C (
Visual C++)
class CMyClass {
int x;
float r = 2.35;
};
(
)
49
C++
(Class Tag)
( )
( )
C++ C+
+
C ++
2 C++
(Header File) .h
.c p p .c p p
(Implementation)
:: ( ) {
}
(Class Implementation)
classname.cpp
2-1 CEmployee
CEmployee
4
2-1
class CEmployee {
public:
CEmployee(string theName, float thePayRate);
string getName() const;
float getPayRate() const;
float pay(float hoursWorked) const;
private:
string name;
float payRate;
};
C++
C++
C++ C++
const
2 C++
string CEmployee::getName() const {
return name;
}
float CEmployee::getPayRate() const {
return payRate;
}
float CEmployee::pay(float hoursWorked) const {
private
p r i v a t e
public
public
51
C++
C++ C+
+
C ++
private protected public [4]
2 C++
protected
protected
C + +
private
protected protected
public public
2-1
2-1
private protected
public
private:
public:
private
protected
protected:
protected:
public:
C++
C++
C++ C++
private
private
private (
(friend) ) public
52
2 C++
private nSalary
private public
nSalary getSalary setSalary
nSalary (Data Validation and
Integrity) private
(
)
p u b l i c
private
private
private (get set)
private
(Accessor)
getDate
(Mutator) setPayRate
(Constructor)
53
C++
C++ C+
+
C ++
2 C++
public public
p u b l i c
private protected friend
private protected
friend
2-1
public protected
public
2-1
public
protected
private
;
* = new ;
C++
C++
C++ C++
new
54
(Instantiation)
(Initialization)
2 C++
2-2
public:
CEmployee()
getName()
getPayRate()
pay()
private:
name
payRate
public:
CEmployee(string theName, float thePayRate)
getName()
getPayRate()
pay(float hoursWorked)
empl.pay(40.0)
55
C++
2-2 ()
CEmployee public
private CEmployee empl("John Burke", 25.0)
e m p l
CEmployee
C++ C+
+
C ++
empl.payRate = 10;
2 C++
"John Burke" theName
name pay empl empl.pay(40.0)
empl CEmployee
private
empl.payRate = 10
payRate
2.3
balance
interest_rate CBankAccount
class CBankAccount {
public:
CBankAccount(int dollars, int cents, double rate);
private:
C++
C++
C++ C++
double balance;
double interest_rate;
};
CBankAccount::CBankAccount(int dollars, int cents, double rate) {
56
2 C++
balance = dollars + 0.01*cents;
interest_rate = rate;
}
CBankAccount
void
CBankAccount acc1(10,50,2.0);
CBankAccount acc2(500,0,4.5);
CBankAccount acc1(10,50,2.0)
CBankAccount(int dollars, int cents, double rate)
acc2.CBankAccount(23, 2, 3);
57
C++
C++ C+
+
C ++
2 C++
class CRectangle {
int *width, *height;
public:
CRectangle (int,int);
~CRectangle ();
delete width;
delete height;
C++
C++
C++ C++
}
2-3
58
2 C++
2-3
2
2.4 (Default Constructor)
class CDefCon {
public:
int x;
int y;
};
59
C++
C++ C+
+
C ++
CDefCon def;
2 C++
class CDefCon {
public:
int x;
int y;
CDefCon(int a, int b){x=a; y=b;}
};
CDefCon def(2, 3);
CDefCon
CDefCon def;
CDefCon
(Copy Constructor)
CDefCon def1(2, 3);
C++
C++
C++ C++
CDefCon def2(def1);
60
2 C++
CDefCon(const CDefCon& c) {
x = c.x;
y = c.y;
}
class CBankAccount {
public:
CBankAccount();
CBankAccount(int dollars, int cents, double
rate);
CBankAccount(int dollars, int cents);
};
CBankAccount::CBankAccount(){
balance = 0.0;
interest_rate = 0.0;
}
CBankAccount::CBankAccount(int dollars, int
cents){
balance = dollars + 0.01*cents;
interest_rate = 0.0;
}
CBankAccount::CBankAccount(int dollars, int
cents, double rate){
interest_rate = rate;
}
Part I: Basic Idea & Concept
61
C++
C++ C+
+
C ++
2 C++
class CDate {
public:
private:
unsigned month = 1;
unsigned day = 1;
};
CDate::CDate(unsigned m = 1, unsigned d = 1, unsigned y = 1955)
: month(m), day(d), year(y) {
C++
C++
C++ C++
month(m)
(Constructor Initialization) unsigned m = 1
m 1
CDate dd1;
62
2 C++
2.6 new
new
* = new ( );
63
C++
1,000,000
C++ C+
+
C ++
CBankAccount account[1000000];
2 C++
Visio
pAcc
CBankAccount* pAcc;
for(int i=0; i<1000000; i++) {
new
delete
}
1,000,000 new
delete pAcc
n ew
2-4
2-4
new
account
1,000,000
1
CBankAccount account[1000000];
pAcc
C++
C++
C++ C++
...
64
CBankAccount* pAcc;
delete
pAcc
CBankAccount
new
2 C++
new
(
)
2-4
count
Ac
ank
CB
new
=
cc
pA
pAcc
pAcc
ory Leak)
em
(M
2-4
2.7
C++
C++
CString s1,s2, s3;
s1 = "This is ";
s1 s2 +
Part I: Basic Idea & Concept
65
C++
s3 = s1 + s2;
C++ C+
+
C ++
s2 = "a Test";
2 C++
s1 s2 ( "This is " "a Test")
s3 s3 = s1 + s2
C++
C++ + - * / >= ++ [ ]
(Operator Functions)
+ - ( int
float long char)
/ << >>
+ - * / << >>
:: operator op(
C++
C++
C++ C++
operator
operator op
operator op operator+ operator +
2-2
C + +
66
2 C++
2-2
/
<
>
+=
-=
*=
/=
%=
^=
&=
|=
<<
>>
<<=
>>=
==
!=
<=
>=
&&
||
++
--
->*
new
delete
&
[]
()
->
?:
::
.*
2-2 CVector +
class CVector {
public:
int x,y;
CVector () {};
x,y
public
67
C++
C++ C+
+
C ++
x = a;
2 C++
CVector CVector::operator + (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
x param.x
y param.y
CVector
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {
CVector a (3,1);
CVector b (1,2);
CVector c, d;
c = a + b;
d = c.operator +(a);
cout << c.x << "," << c.y << endl;
cout << d.x << "," << d.y << endl;
}
2-2 CVector x y public
CVector operator+ (CVector
param) +
CVector +
c = a + b;
C++
C++
C++ C++
a + b c
operator+ c = a + b
c = a.operator +(b);
68
2 C++
b operator+
x y a b c
c = a + b
CVector () { };
CVector (int, int);
CVector vec
2-3
2-3
CVector CVector::operator -(CVector param) {
CVector temp;
c = a - b;
temp.x = x - param.x;
temp.y = y - param.y;
return (temp);
}
CVector CVector::operator ++() {
CVector temp;
++
c = c++;
temp.x = x + 1;
return (temp);
}
Part I: Basic Idea & Concept
69
C++
C++ C+
+
C ++
temp.y = y + 1;
2 C++
CVector CVector::operator *(int i) {
CVector temp;
*
c = a * 2;
temp.x = x * i;
temp.y = y * i;
return (temp);
}
CVector CVector::operator *(double d) {
CVector temp;
*
c = a * 2.9;
2.8 this
this
this this
2-4
class CDummy {
public:
bool IsKindOf(CDummy& param);
};
bool CDummy::IsKindOf(CDummy& param) {
if (this == ¶m) return true;
C++
C++
C++ C++
2 C++
CDummy a;
CDummy* b = &a;
if ( b->IsKindOf(a) ) printf("Yes, &a is kind of b\n");
else printf("No, It is not me\n");
return 0;
}
this
this
this
this
this
2-4 IsKindOf
CDummy
this
CVector& CVector::operator = (const CVector& param) {
x = param.x;
y = param.y;
return *this;
71
C++
C++ C+
+
C ++
2 C++
2.9
(Class Variable)
(S h a r e d
Variable)
class CDummy {
public:
static int n;
CDummy () { n++; };
~CDummy () { n--; };
};
int CDummy::n = 0;
int main () {
CDummy a;
CDummy b[5];
CDummy * c = new CDummy;
cout << a.n << endl;
delete c;
cout << CDummy::n << endl;
return 0;
C++
C++
C++ C++
}
n CDummy
n
72
2 C++
CDummy::n
a.n
n n
class CMyClass {
int num;
public:
private
CMyClass(int x) {
num = x;
}
friend int isneg(CMyClass ob);
};
isneg
CMyClass
73
C++
C++ C+
+
C ++
2 C++
int _tmain(int argc, _TCHAR* argv[]) {
CMyClass a(-1);
if(isneg(a))
printf("it is negative!\n");
else printf("it is positive!\n");
return 0;
}
isneg CMyClass
num private isneg
CMyClass isneg num
private protected
class CTwoValues {
int a, b;
public:
CTwoValues(int _a, int _b) { a = _a; b = _b;}
friend class CMin;
};
class CMin {
public:
int findMin(CTwoValues x);
};
C++
C++
C++ C++
int CMin::findMin(CTwoValues x) {
return x.a < x.b ? x.a : x.b;
}
74
CMin
CTwoValues
2 C++
int _tmain(int argc, _TCHAR* argv[]) {
CTwoValues two(7, 13);
CMin m;
printf("min is %d", m.findMin(two));
return 0;
}
CMin CTwoValues CMin
CTwoValues private protected
)
ship
end
(fri
ed
tect
pro
ate
priv
75
C++
: public
C++ C+
+
C ++
CRectangle
2 C++
2. "has-a"
(Containment)
CCar
CEngine
3. "uses-a"
(CImageProc) (CImage)
CImage
CImageProc
CImage img;
doHistogram()
C++
C++
C++ C++
2.11.1 (Inheritance)
C# C++
76
2 C++
(Derived Class)
(Sub Class) (Child Class)
(Base Class) (Super Class)
(Parent Class)
C + +
(Multiple Inheritance) (
(Interface))
(
)
(R e u s a b l e
Code)
C E d i t
CEdit
() (-)
()
CPhoneEdit CEdit
77
C++
C++ C+
+
C ++
C++
C++
C++ C++
2 C++
CEdit CPhoneEdit CPhoneEdit CEdit
CPhoneEdit
CPhoneEdit CEdit
CPhoneEdit
CEdit
(is a kind of)
(CLecturer)
(CEmployee) (CRectangle)
(CShape) (Square Matrix) (Matrix)
(CCat) (CAnimal) (CMammal)
(CHuman)
(Compatible Type)
(Polymorphism)
C++
2-5
78
2 C++
2-5 : p u b l i c
( public
protected)
2-5
class
: public
...
...
};
: public
operator = ( )
private
Part I: Basic Idea & Concept
79
C++
C++ C+
+
C ++
2.11.2
2 C++
(Reusable Code)
CPhoneEdit
(Extensible Feature)
(Path Name)
CPathName CString
is-a has-a 2-6
CString
reverseString()
2-6
: public
CPathName
(a)
is-a
reverseString()
(b)
has-a
CPathName path;
reverseString()
CPathName
private : CString
reverseString()
CPathName path;
reverseString()
path.reverseString()
C++
C++
C++ C++
80
CPathName
\
CString
2 C++
"is-a" CPathName
CString (CPathName is a CString)
CPathName
CPathName
CString CString CPathName (String
Reversal) CPathName
( 2-6 ()) "C:\WINDOWS"
"SWODNIW\:C"
has-a CString
private CPathName
CString CPathName
CString CPathName
CPathName reverseString
( 2-6 ())
has-a
81
C++
(Employee)
(Manager)
(Supervisor)
C++ C+
+
C ++
2 C++
(Name)
(Pay Rate)
( )
(getName)
(getPayRate)
(pay)
2-5
2-5
class CEmployee {
public:
CEmployee(string theName, float thePayRate);
string getName() const;
float getPayRate() const;
float pay(float hoursWorked) const;
protected:
string name;
float payRate;
};
C++
C++
C++ C++
2 C++
string CEmployee::getName() const {
return name;
}
float CEmployee::getPayRate() const {
return payRate;
}
float CEmployee::pay(float hoursWorked) const {
(salaried)
83
C++
C++ C+
+
C ++
2 C++
(getSalaried)
2-6
2-6
class Manager {
public:
Manager(string theName, float thePayRate, bool isSalaried);
string getName() const;
float getPayRate() const;
bool getSalaried() const;
float pay(float hoursWorked) const;
protected:
string name;
float payRate;
bool salaried;
};
Manager::Manager(string theName, float thePayRate, bool isSalaried) {
name = theName;
payRate = thePayRate;
salaried = isSalaried;
}
string Manager::getName() const {
return name;
C++
C++
C++ C++
}
float Manager::getPayRate() const {
return payRate;
}
84
2 C++
bool Manager::getSalaried() const {
return salaried;
}
float Manager::pay(float hoursWorked) const {
if (salaried)
return payRate;
else
(Manager is kind of an Employee)
2-7
class CManager : public CEmployee {
public:
CManager(string theName, float thePayRate, bool isSalaried);
CManager
CEmployee
85
C++
C++ C+
+
C ++
2 C++
CManager::CManager(string theName, float thePayRate, bool isSalaried)
: CEmployee(theName, thePayRate) {
salaried = isSalaried;
}
C++
C++
C++ C++
(salaried FALSE)
return CEmployee::pay(hoursWorked);
pay CEmployee ::
86
2 C++
CManager
CManager mgr("Jan Kovacs", 1200.0, true);
cout << "For Manager:" << endl;
cout << "Name: " << mgr.getName().c_str() << endl;
cout << "Salaried: " << mgr.getSalaried() << endl;
cout << "Pay: " << mgr.pay(40.0) << endl;
2-7
CManager
CEmployee
getName()
getPayRate()
pay()
: public
CManager
salaried getSalaried()
pay()
name
payRate
getName()
getPayRate()
pay()
87
C++
public
public
ate
priv
private
ed
tect
pro
C++ C+
+
C ++
2 C++
2.12
C++
2-8
CPolygon
CRectangle
C++
C++
C++ C++
COutput
CTriangle
(CPolygon)
(COutput)
(CRectangle)
(C T r i a n g l e )
2-8 C R e c t a n g l e
CPolygon COutput
CPolygon COutput CRectangle
CTriangle
2-8
88
2 C++
2-8
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class COutput {
public:
void output (int i);
};
void COutput::output (int i) {
cout << i << endl;
}
class CRectangle: public CPolygon, public COutput {
public:
int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon, public COutput {
public:
int area ()
89
C++
};
C++ C+
+
C ++
2 C++
set_values
CPolygon output COutput
CRectangle rect;
CTriangle tri;
rect.set_values (4,5);
set_values CPolygon
tri.set_values (4,5);
rect.output (rect.area());
output COutput
tri.output (tri.area());
C++
C++
C++ C++
2.13
(Compatible Type)
(Plug Compatible)
C++
(Polymorphic)
90
2 C++
1
C P o l y g o n
CRectangle CTriangle CPolygon CRectangle
CTriangle
CPolygon CRectangle
CTriangle 2-9
2-9
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};
class CRectangle: public CPolygon {
public:
int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area ()
{ return (width * height / 2); }
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {
CRectangle rect;
Part I: Basic Idea & Concept
91
C++
C++ C+
+
C ++
};
2 C++
CTriangle tri;
CPolygon * ppoly1 = ▭
ppoly1 &rect
set_values
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << tri.area() << endl;
}
2-9 CPolygon ppoly1 ppoly2
CRectangle CTriangle
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &tri;
CRectangle CTriangle CPolygon
ppoly1 ppoly2
CPolygon CRectangle CTriangle
CPolygon set_values
ppoly1->set_values (4,5);
C++
C++
C++ C++
ppoly2->set_values (4,5);
area CPolygon area
rect.area tri.area area
CPolygon area CPolygon area
area CPolygon
92
2 C++
(
)
CRectangle CTriangle
CPolygon CPolygon
CRectangle CTriangle CPolygon
Draw
Draw Draw
virtual
Part I: Basic Idea & Concept
93
C++
virtual ( );
C++ C+
+
C ++
()
2 C++
(
)
void Draw(void);
CPolygon CRectangle CTriangle
(Run Time)
2-9
2-9
::vir_func()
.vir_func()
->vir_func()
1::vir_func()
2::vir_func()
3::vir_func()
C++
C++
C++ C++
protected:
int width, height;
public:
void set_values (int a, int b)
94
2 C++
{ width=a; height=b; }
virtual int area ()
{ return (0); }
};
class CRectangle: public CPolygon {
public:
int area ()
ppoly1 rect
set_values CPolygon
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout << ppoly1->area() << endl;
area CRectangle
95
C++
C++ C+
+
C ++
2 C++
2-10 CPolygon CRectangle CTriangle width
height set_values area area
ppoly1->set_values (4,5);
CPolygon
ppoly1->area();
CRectangle ppoly1
rect
C++
C++
C++ C++
class CPolygon {
protected:
int width, height;
public:
96
2 C++
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area () = 0;
};
area = 0 (NULL)
(Pure Virtual Function)
CPolygon poly;
CPolygon
CPolygon * ppoly1;
97
C++
C++ C+
+
C ++
CPolygon * ppoly2;
2 C++
1.
2.
3.
4.
OnPaint
5. + -
CMyDate + -
6.
7. private protected
8.
9.
C++
C++
C++ C++
98