/ V 0.9 V 1.0 2001-7-1 2001-7-18 2001-7-18 2001-7-24 V 0.9 2001 Page 2 of 101



Similar documents
为提高我们的程序品质特将林博士的资料作为我公司程序员的学习教材,请各位读读,我把我的看法用红字表出,供大家探讨

<4D F736F F D20B8DFD6CAC1BF432B2BB1E0B3CCD6B8C4CF2E646F63>

FY.DOC

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

untitled

新版 明解C++入門編

Chapter12 Derived Classes

c_cpp

提问袁小兵:

新・解きながら学ぶJava

新版 明解C言語入門編

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2

untitled

C/C++程序设计 - 字符串与格式化输入/输出

untitled

02

nooog

CC213

C/C++ - 函数

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

全国计算机技术与软件专业技术资格(水平)考试

Microsoft Word - 第3章.doc

C++ 程式設計

新・解きながら学ぶC言語

Microsoft Word - 01.DOC

_汪_文前新ok[3.1].doc

新・明解C言語入門編『索引』

,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : : : /16 : : 96 : : : ISBN 7

untitled

Microsoft Word cppFinalSolution.doc

extend

C/C++ 语言 - 循环

C/C++语言 - C/C++数据

C/C++ - 文件IO

C/C++ - 字符输入输出和字符确认

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

C/C++语言 - 运算符、表达式和语句

C/C++ - 字符串与字符串函数

Java

CHAPTER VC#

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

相 应 功 能 (5) 再 将 Boy 类 作 为 Girl 类 的 友 元 类, 在 Boy 类 的 某 成 员 函 数 VisitGirl(Girl & ) 中 访 问 Girl 类 的 私 有 成 员, 观 察 编 译 器 给 出 的 信 息 ( 6 ) 删 除 两 个 类 中 的 函 数 V

CC213

C

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

Microsoft Word - 物件導向編程精要.doc

C 1

untitled

概述

untitled

zt

中 国 中 西 医 结 合 杂 志 年 月 第 卷 第 期!" 通 透 性 增 加 产 生 蛋 白 水 解 酶 促 进 血 管 内 皮 细 胞 有 丝 分 裂 内 皮 细 胞 从 基 底 膜 上 迁 移 到 血 管 周 围 间 隙 粘 附 聚 集 重 构 为 三 维 管 腔 并 与 周 围 血 管

Microsoft Word - ch04三校.doc

CHAPTER 1

Strings

四川省普通高等学校

Ps22Pdf

ebook39-5

<4D F736F F D D342DA57CA7DEA447B14D2DA475B57BBB50BADEB27AC3FEB14DA447B8D5C344>

Strings

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

! "#$ %$ $ 资 料 与 方 法 $ 调 查 对 象 全 国 东 北 华 北 华 东 西 北 西 南 和 中 南 六 个 大 区 个 省 自 治 区 直 辖 市 * 个 城 市 中 的 & 所 医 院 参 加 了 本 次 调 查 各 省 省 会 城 市 的 医 学 院 校 附 属 医 院 省

Microsoft Word - 11.doc

Chapter 9: Objects and Classes

Microsoft PowerPoint - OPVB1基本VB.ppt

C C


Microsoft Word 年9月二级C真卷.doc

51 C 51 isp 10 C PCB C C C C KEIL

第5章修改稿

Microsoft PowerPoint - string_kruse [兼容模式]

ebook8-30

Microsoft Word - CPE考生使用手冊 docx

議 程 前 發 言 冀 新 官 上 任 燒 好 三 把 火 用 好 三 盆 水 陳 明 金 2014 年 11 月 18 日 澳 門 特 區 政 府 即 將 換 屆, 各 種 傳 聞 令 廣 大 居 民 感 覺 到, 絕 大 部 份 主 要 官 員 都 會 換 人 雖 然 他 們 對 人 選 無 話

得 到 了 補 償. 對 於 武 姜 而 言, 莊 公 與 自 己 的 關 係 並 不 親 密, 而 共 叔 段 又 是 自 己 向 來 疼 愛 有 加 的 兒 子, 所 以, 對 莊 公 提 出 再 怎 麼 無 理 的 要 求, 武 姜 也 不 會 覺 得 有 什 麼 不 妥 之 處, 而 對 共

一 耀 州 青 瓷 的 裝 飾 手 法 與 紋 飾 種 類 耀 州 窯 的 裝 飾 紋 樣, 豐 富 多 變, 而 且 題 材 內 容 廣 泛, 組 合 形 式 多 樣, 圖 案 形 象 優 美, 令 人 賞 心 悅 目, 並 且 反 映 了 當 時 社 會 的 審 美 趣 味 和 理 想 裝 飾

1911 年 武 汉 起 义, 广 东 独 立 胡 汉 民 任 总 督, 陈 任 广 东 军 政 府 外 交 部 副 部 长 陈 不 愿 做 官, 几 个 月 后 即 辞 职 1915 年 与 李 煜 堂 设 立 上 海 保 险 公 司, 陈 任 主 席 1921 年 孙 中 山 就 任 非 常 大

WWW PHP

第七讲 继承与多态

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

4 中 南 大 学 学 报 医 学 版 摘 要 目 的 探 讨 早 发 性 精 神 分 裂 症 患 者 在 静 息 状 态 下 是 否 存 在 脑 功 能 连 接 异 常 以 及 异 常 区 域 的 定 位 方 法 采 用 第 版 美 国 精 神 障 碍 诊 断 与 统 计 手 册 ( * ) (

! # % % & # # % #!& % &# % &# % % % # %& ( (!& (! & & % % #!! ) %&! *& % %! % %!! # % %!! %*!& % &# % &# ) ) ( % # # ) % ( (!& (! (!! # % % #!! # ( &!

## $%& %& ## () #) (( * (+++ () #) #) (+ (+ #) #) ( #, - #)). #))- # ( / / / / $ # ( * *..# 4 #$ 3 ( 5 ) ### 4 $ # 5, $ ## # 4 $# 5 ( %

untitled

C/C++ - 数组与指针

C

C/C++语言 - 分支结构

《C语言程序设计》教材习题参考答案

2007

第3章.doc

IO

科学计算的语言-FORTRAN95

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco

立 志 于 打 造 最 贴 近 考 生 实 际 的 辅 导 书 计 算 机 考 研 之 数 据 结 构 高 分 笔 记 率 辉 编 著 周 伟 张 浩 审 核 讨 论 群 :


3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

第 期 周 超 英 等 甜 瓜 黑 斑 病 菌 的 生 物 学 特 性 及 室 内 药 剂 筛 选 * /3 / /5%.5 %: ' ' &-20 - &% /%&% /.&.. 5 /05/ 07 /0/%&% /.&.38 /% -/.&.. 5 /05/ 07 05/ /8 00

Transcription:

C++/C [ ] 1.0 [ ] [ ] 2001 7 24

/ V 0.9 V 1.0 2001-7-1 2001-7-18 2001-7-18 2001-7-24 V 0.9 2001 Page 2 of 101

...6 1...8 1.1...8 1.2...8 1.3...8 1.4...8 1.5...8 2...8 2.1...8 2.2...8 2.3...8 2.4...8 2.5...8 2.6...8 2.7...8 2.8...8 3...8 3.1...8 3.2 WINDOWS...8 3.3 UNIX...8 4...8 4.1...8 4.2...8 4.3 IF...8 4.4...8 4.5 FOR...8 4.6 SWITCH...8 4.7 GOTO...8 5...8 5.1...8 5.2 CONST #DEFINE...8 5.3...8 5.4...8 6...8 2001 Page 3 of 101

6.1...8 6.2...8 6.3...8 6.4...8 6.5...8 6.6...8 7...8 7.1...8 7.2...8 7.3...8 7.4...8 7.5 FREE DELETE...8 7.6...8 7.7...8 7.8 MALLOC/FREE NEW/DELETE...8 7.9...8 7.10 MALLOC/FREE...8 7.11 NEW/DELETE...8 7.12...8 8 C++...8 8.1...8 8.2...8 8.3...8 8.4...8 8.5...8 8.6...8 9...8 9.1...8 9.2...8 9.3...8 9.4 STRING...8 9.5...8 9.6 STRING...8 9.7...8 9.8...8 9.9...8 10...8 2001 Page 4 of 101

10.1...8 10.2...8 11...8 11.1 CONST...8 11.2...8 11.3...8...8 A C++/C...8 B C++/C...8 C C++/C...8 2001 Page 5 of 101

C++/C N-1 Internet (1) (2) (3) (4) (5) (6) (7) (8) (9) 30 (10) 9:00 5:00 9:00 C++/C CAD 3D 2001 Page 6 of 101

1995 2000 10 C++ 3D 3D strcpy 1 2 3 IT IT 2001 Page 7 of 101

1 2 10 if 14 C++/C IT 1 2 1 2 3 2001 Page 8 of 101

B C++/C 1 2 3 85 C++/C 4 C++/C Bug CMMI CMMI 2001 Page 9 of 101

C++/C 1 2 3 2002 7 CMMI 3 1000 2001 7 2001 Page 10 of 101

1 C++/C declaration implementation definition C++/C.h C.c C++.cpp.cc.cxx 1.1 1-1 1 2 3 / 4 /* * Copyright (c) 2001, * All rights reserved. * * filename.h * * * * 1.1 * * 2001 7 20 * * 1.0 * * 2001 5 10 */ 1-1 2001 Page 11 of 101

1.2 1 1-1 2 3 graphics.h 1-2 1-2-1 ifndef/define/endif 1-2-2 #include <filename.h> 1-2-3 #include filename.h 1-2-1 C++ 1-2-2 extern int value // 1-1 #ifndef #define GRAPHICS_H // graphics.h GRAPHICS_H #include <math.h> // #include myheader.h // void Function1( ); // class Box // ; #endif 1-2 C++/C 2001 Page 12 of 101

1.3 1 1-1 2 3 graphics.cpp 1-3 // 1-1 #include graphics.h // // void Function1( ) // void Box::Draw( ) 1-3 C++/C 1.4 Basic Fortran C++/C 1 2 2001 Page 13 of 101

1.5 include source 2001 Page 14 of 101

2 2.1 2-1-1 2-1 a 2-1-2 2-1 b // void Function1( ) // void Function2( ) // void Function3( ) // while (condition) statement1; // if (condition) statement2; else statement3; // statement4; 2-1(a) 2-1(b) 2001 Page 15 of 101

2.2 2-2-1 2-2-2 if for while do 2-2 a 2-2 b int width; // int height; // int depth; // x = a + b; y = c + d; z = e + f; if (width < height) dosomething(); for (initialization; condition; update) dosomething(); // other(); int width, height, depth; // X a + b; y = c + d; z = e + f; if (width < height) dosomething(); for (initialization; condition; update) dosomething(); other(); 2-2(a) 2-2(b) 2-2-1 int width = 10; // width int height = 10; // height int depth = 10; // depth 2001 Page 16 of 101

2.3 2-3-1 const virtual inline case if for while 2-3-2 2-3-3 ; 2-3-4 Function(x, y, z) ; for (initialization; condition; update) 2-3-5 = +=>= <= + * % && <<, ^ 2-3-6! ~ ++ -- & 2-3-7. -> 2-3-1 for if for (i=0; i<10; i++) if ((a<=b) && (c<=d)) void Func1(int x, int y, int z); void Func1 (int x,int y,int z); if (year >= 2000) if(year>=2000) if ((a>=b) && (c<=d)) if(a>=b&&c<=d) for (i=0; i<10; i++) for(i=0;i<10;i++) for (i = 0; I < 10; i ++) x = a < b? a : b; x=a<b?a:b; int *x = &y; int * x = & y; array[5] = 0; a.function(); b->function(); // // // // // // // // // // // // // // array [ 5 ] = 0; // a. Function(); // b -> Function(); 2-3 2001 Page 17 of 101

2.4 2-4-1 2-4-2 2-4 a 2-4 b void Function(int x) // program code if (condition) // program code else // program code for (initialization; condition; update) // program code While (condition) // program code void Function(int x) // program code if (condition) // program code else // program code for (initialization; condition; update) // program code while (condition) // program code 2-4(a) 2-4(b) 2001 Page 18 of 101

2.5 2-5-1 70 80 2-5-2 if ((very_longer_variable1 >= very_longer_variable12) && (very_longer_variable3 <= very_longer_variable14) && (very_longer_variable5 <= very_longer_variable16)) dosomething(); virtual CMatrix CMultiplyMatrix (CMatrix leftmatrix, CMatrix rightmatrix); for (very_longer_initialization; very_longer_condition; very_longer_update) dosomething(); 2-5 2.6 * * int* x; x int int* x, y; y x y 2-6-1 * char *name; int *x, y; // y 2001 Page 19 of 101

2.7 C /* */C++ /* */ // 1 2 3 2-6 2-7-1 2-7-2 i++; // i 1 2-7-3 2-7-4 2-7-5 2-7-6 2-7-8 /* * * * * */ void Function(float x, float y, float z) if ( ) while ( ) // end of while // end of if 2-6 2001 Page 20 of 101

2.8 public protected private 1 private public 8-3 a 2 public private 8.3 b C++ Biarne Stroustrup class A private: int i, j; float x, y; public: void Func1(void); void Func2(void); class A public: void Func1(void); void Func2(void); private: int i, j; float x, y; 8.3(a) 8.3(b) 2001 Page 21 of 101

3 Microsoft ch p ppch int i, j, k; float x, y, z; int ii, ij, ik; // i int float fx, fy, fz; // f float 3.1 3.2 3-1-1 CurrentValue NowValue 3-1-2 min-length && max-information ANSI C 6 C++/C! maxval maxvalueuntiloverflow i,j,k,m,n,x,y,z 3-1-3 Windows AddChild Unix add_child 2001 Page 22 of 101

3-1-4 int x, X; // x X void foo(int x); // foo FOO void FOO(float x); 3-1-5 3-1-6 float value; float oldvalue; float newvalue; 3-1-7 DrawBox(); // box->draw(); // 3-1-8 int minvalue; int maxvalue; int SetValue( ); int GetValue( ); 3-1-1 Value1,Value2 3.2 Windows Windows 2001 Page 23 of 101

3-2-1 class Node; // class LeafNode; // void Draw(void); // void SetValue(int value); // 3-2-2 BOOL flag; int drawmode; 3-2-3 const int MAX = 100; const int MAX_LENGTH = 100; 3-2-4 s_ static void Init( ) static int s_initvalue; // 3-2-5 g_ global int g_howmanypeople; // int g_howmuchmoney; // 3-2-6 m_ member void Object::SetValue(int width, int height) m_width = width; m_height = height; 2001 Page 24 of 101

3-2-7 OpenGL gl GL 3.3 Unix 2001 Page 25 of 101

4 if for while goto switch C++/C 4.1 C++/C 4-1 + - * ( ) [ ] ->.! ~ ++ -- sizeof + - * & * / % + - << >> < <= > >= ==!= & ^ &&?: = += -= *= /= %= &= ^= = <<= >>= 4-1 4-1-1 4-1 word = (high << 8) low 2001 Page 26 of 101

if ((a b) && (a & c)) 4.2 a = b = c = 0 1 2 4-2-1 i = a >= b && c < d && c + f <= g + h ; // 4-2-2 d = (a = b + c) + r ; a d a = b + c; d = a + r; 4-2-3 if (a < b < c) // a < b < c if ((a<b) && (b<c)) if ( (a<b)<c ) 4.3 if if C++/C if 4.3.1 4-3-1 TRUE FALSE 1 0 FALSE TRUETRUE Visual C++ TRUE 1 Visual Basic TRUE -1 flag if if (flag) // flag 2001 Page 27 of 101

if (!flag) // flag if (flag == TRUE) if (flag == 1 ) if (flag == FALSE) if (flag == 0) 4.3.2 4-3-2 == = 0 value if if (value == 0) if (value!= 0) if (value) // value if (!value) 4.3.3 4-3-3 == = float double == = >= <= x if (x == 0.0) // if ((x>=-epsinon) && (x<=epsinon)) EPSINON 4.3.4 4-3-4 == = NULL NULL 0 p if if (p == NULL) // p NULL p if (p!= NULL) if (p == 0) // p if (p!= 0) if (p) if (!p) // p 4.3.5 if 2001 Page 28 of 101

if (NULL == p) if (p == NULL) if (p = NULL) p NULL if (p = NULL) if (NULL = p) NULL if/else/return if (condition) return x; return y; if (condition) return x; else return y; return (condition? x : y); 4.4 C++/C for while do 4-4-1 CPU 4-4(b) 4-4(a) for (row=0; row<100; row++) for (col=0; col<5; col++ ) for ( col=0; col<5; col++ ) for (row=0; row<100; row++) sum = sum + a[row][col]; sum = sum + a[row][col]; 4-4(a) 4-4(b) 4-4-2 2001 Page 29 of 101

4-4(c) 4-4(d) N-1 N 4-4(d) N 4-4(c) for (i=0; i<n; i++) if (condition) if (condition) for (i=0; i<n; i++) DoSomething(); DoSomething(); else DoOtherthing(); else for (i=0; i<n; i++) DoOtherthing(); 4-4(c) 4-4(d) 4.5 for 4-5-1 for for 4-5-1 for 4-5(a) x 0 =< x < N N 4-5(b) x 0 =< x <= N-1 N 4-5(a) for (int x=0; x<n; x++) for (int x=0; x<=n-1; x++) 4-5(a) 4-5(b) 4.6 switch if switch 2001 Page 30 of 101

switch if if switch switch switch (variable) case value1 : break; case value2 : break; default : break; 4-6-1 case break 4-6-2 default default default : break; default 4.7 goto goto goto goto goto state; String s1, s2; // goto int sum = 0; // goto state: goto C++/C goto goto goto break ; goto error; 2001 Page 31 of 101

error: goto 2001 Page 32 of 101

5 C #define C++ #define const const 5.1 1 2 3 5-1-1 #define MAX 100 /* C */ const int MAX = 100; // C++ const const float PI = 3.14159; // C++ const 5.2 const #define C++ const #define 1 const 2 const 5-2-1 C++ const const 5.3 5-3-1 2001 Page 33 of 101

5-3-2 const float RADIUS = 100; const float DIAMETER = RADIUS * 2; 5.4 #define const const const const const SIZE class A const int SIZE = 100; // const int array[size]; // SIZE ; const class A A(int size); // const int SIZE ; ; A::A(int size) : SIZE(size) // A a(100); // a SIZE 100 A b(200); // b SIZE 200 const class A enum SIZE1 = 100, SIZE2 = 200; // 2001 Page 34 of 101

; int array1[size1]; int array2[size2]; PI=3.14159 2001 Page 35 of 101

6 C++/C C pass by value pass by pointerc++ pass by reference 6.6 6.1 6-1-1 void void SetValue(int width, int height); // void SetValue(int, int); // float GetValue(void); // float GetValue(); // 6-1-2 StringCopy str1 str2 void StringCopy(char *str1, char *str2); str1 str2 strsource strdestination strsource strdestination void StringCopy(char *strsource, char *strdestination); char str[20]; StringCopy(str, Hello World ); // 6-1-3 const 2001 Page 36 of 101

void StringCopy(char *strdestination const char *strsource); 6-1-4 const & 6-1-1 5 6-1-2 C printf int printf(const chat *format[, argument] ); 6.2 6-2-1 C void C++ C C++/ C void 6-2-2 C getchar char c; c = getchar(); if (c == EOF) getchar c char getchar char int int getchar(void); c char [-128 127] EOF char if getchar 6-2-3 return C getchar int 2001 Page 37 of 101

getchar getchar EOF 1 getchar int return getchar BOOL GetChar(char *c); gechar GetChar putchar(getchar()); getchar 6-2-1 strcpy char *strcpy(char *strdest const char *strsrc); strcpy strsrc strdest strdest char str[20]; int length = strlen( strcpy(str, Hello World ) ); 6-2-2 class String // String & operate=(const String &other); // friend friend String operate+( const String &s1, const String &s2); private: char *m_data; String operate = String & String::operate=(const String &other) if (this == &other) return *this; delete m_data; 2001 Page 38 of 101

m_data = new char[strlen(other.data)+1]; strcpy(m_data, other.data); return *this; // *this String return *this String a,b,c; a = b; // *this a = b = c; // *this String operate + String operate+(const String &s1, const String &s2) String temp; delete temp.data; // temp.data 0 temp.data = new char[strlen(s1.data) + strlen(s2.data) +1]; strcpy(temp.data, s1.data); strcat(temp.data, s2.data); return temp; String temp temp c = a + b; a + b c 6.3 6-3-1 assert 6.5 2001 Page 39 of 101

6-3-2 return return return return 1 return char * Func(void) char str[] = hello world ; // str return str; // 2 3 return return String(s1 + s2); temp String temp(s1 + s2); return temp; temp temp temp return int(x + y); // int temp = x + y; return temp; int,float,double 6.4 6-4-1 6-4-2 50 6-4-3 2001 Page 40 of 101

C/C++ static static 6-4-4 6-4-5 6.5 Debug Release Debug Release assert Debug 6-5 assert assert void *memcpy(void *pvto, const void *pvfrom, size_t size) assert((pvto!= NULL) && (pvfrom!= NULL)); // byte *pbto = (byte *) pvto; // pvto byte *pbfrom = (byte *) pvfrom; // pvfrom while(size -- > 0 ) *pbto ++ = *pbfrom ++ ; return pvto; 6-5 assert Debug Release assert assert assert assert assert assert [Maguire, p8-p30] 2001 Page 41 of 101

6-5-1 6-5-2 6-5-1 6-5-2 6.6 C++ n m referencem referent int m; int &n = m; n m n m n m m n m 1 2 NULL NULL 3 k i k = j k j k 6 k i i 6 int i = 5; int j = 6; int &k = i; k = j; // k i 6; C++ Func1 x n x n, n 0 void Func1(int x) x = x + 10; 2001 Page 42 of 101

int n = 0; Func1(n); cout << n = << n << endl; // n = 0 Func2 x n n n 10 void Func2(int *x) (* x) = (* x) + 10; int n = 0; Func2(&n); cout << n = << n << endl; // n = 10 Func3 x n x n x n n 10 void Func3(int &x) x = x + 10; int n = 0; Func3(n); cout << n = << n << endl; // n = 10 2001 Page 43 of 101

7 Bill Gates 640K ought to be enough for everybody Bill Gates 1981 7.1 1 static 2 3 malloc new free delete 7.2 NULL p assert(p!=null) malloc new if(p==null) if(p!=null) 2001 Page 44 of 101

1 1 for malloc free new/delete 1 2 return 3free delete NULL 7-2-1 malloc new NULL NULL 7-2-2 7-2-3 1 7-2-4 7-2-5 free delete NULL 7.3 C++/C 2001 Page 45 of 101

7.3.1 7-3-1 a 6 hello\0 a a[0]= X p world world\0 p[0]= X char a[] = hello ; a[0] = X ; cout << a << endl; char *p = world ; p[0] = X ; cout << p << endl; // p // 7-3-1 7.3.2 7-3-2 a b b = a strcpy b a if(b==a) strcmp p = a a p a p a malloc p strlen(a)+1 strcpy if(p==a) strcmp // char a[] = "hello"; char b[10]; strcpy(b, a); // b = a; if(strcmp(b, a) == 0) // if (b == a) // int len = strlen(a); char *p = (char *)malloc(sizeof(char)*(len+1)); strcpy(p,a); // p = a; if(strcmp(p, a) == 0) // if (p == a) 7-3-2 2001 Page 46 of 101

7.3.3 sizeof 7-3-3 a sizeof(a) 12 0 p a sizeof(p) 4 sizeof(p) sizeof(char*) p C++/C 7-3-3 b a sizeof(a) sizeof(char *) char a[] = "hello world"; char *p = a; cout<< sizeof(a) << endl; // 12 cout<< sizeof(p) << endl; // 4 7-3-3 a void Func(char a[100]) cout<< sizeof(a) << endl; // 4 100 7-3-3 b 7.4 7-4-1 Test GetMemory(str, 200) str str NULL void GetMemory(char *p, int num) p = (char *)malloc(sizeof(char) * num); void Test(void) char *str = NULL; GetMemory(str, 100); // str NULL strcpy(str, "hello"); // 7-4-1 2001 Page 47 of 101

GetMemory p _p = p _p p _p _p p GetMemory GetMemory free 7-4-2 void GetMemory2(char **p, int num) *p = (char *)malloc(sizeof(char) * num); void Test2(void) char *str = NULL; GetMemory2(&str, 100); // &str strcpy(str, "hello"); cout<< str << endl; free(str); 7-4-2 7-4-3 char *GetMemory3(int num) char *p = (char *)malloc(sizeof(char) * num); return p; void Test3(void) char *str = NULL; str = GetMemory3(100); strcpy(str, "hello"); cout<< str << endl; free(str); 7-4-3 2001 Page 48 of 101

return return 7-4-4 char *GetString(void) char p[] = "hello world"; return p; // void Test4(void) char *str = NULL; str = GetString(); // str cout<< str << endl; 7-4-4 return Test4 str = GetString str NULL str hello world 7-4-4 7-4-5 char *GetString2(void) char *p = "hello world"; return p; void Test5(void) char *str = NULL; str = GetString2(); cout<< str << endl; 7-4-5 return Test5 GetString2 GetString2 hello world GetString2 2001 Page 49 of 101

7.5 free delete free delete delete 7-5 p free NULL p p NULL p p p if (p!= NULL) if p NULL char *p = (char *) malloc(100); strcpy(p, hello ); free(p); // p p if(p!= NULL) // strcpy(p, world ); // 7-5 p 7.6 7-6 p void Func(void) char *p = (char *) malloc(100); // 7-6 1 2 NULL 2001 Page 50 of 101

NULL 7.7 NULL NULL if if 1 NULL NULL char *p = NULL; char *str = (char *) malloc(100); 2 p free delete NULL p 7.5 3 class A public: void Func(void) cout << Func of class A << endl; ; void Test(void) A *p; A a; p = &a; // a p->func(); // p Test p->func() a p a p 2001 Page 51 of 101

7.8 malloc/free new/delete malloc free C++/C new/delete C++ maloc/free malloc/free malloc/free C++ new delete new/delete malloc/free new/delete 7-8 class Obj public : Obj(void) cout << Initialization << endl; ~Obj(void) cout << Destroy << endl; void Initialize(void) cout << Initialization << endl; void Destroy(void) cout << Destroy << endl; ; void UseMallocFree(void) Obj *a = (obj *)malloc(sizeof(obj)); // a->initialize(); // // a->destroy(); // free(a); // void UseNewDelete(void) Obj *a = new Obj; // // delete a; // 7-8 malloc/free new/delete Obj Initialize Destroy UseMallocFree malloc/free Initialize Destroy UseNewDelete 2001 Page 52 of 101

malloc/free new/delete malloc/free new/delete new/delete malloc/free C++ malloc/free C++ C C malloc/free free new delete malloc new/delete malloc/free 7.9 malloc new NULL 1 NULL return void Func(void) A *a = new A; if(a == NULL) return; 2 NULL exit(1) void Func(void) A *a = new A; if(a == NULL) cout << Memory Exhausted << endl; exit(1); 3 new malloc Visual C++ _set_new_hander 2001 Page 53 of 101

new malloc new C++ 12 1 2 exit(1) exit(1) 32 malloc new Windows 98 Visual C++ 7-9 32 Window 98 32 Unix Windows void main(void) float *p = NULL; while(true) p = new float[1000000]; cout << eat memory << endl; if(p==null) exit(1); 7-9 7.10 malloc/free malloc void * malloc(size_t size); malloc length 2001 Page 54 of 101

int *p = (int *) malloc(sizeof(int) * length); sizeof malloc void * malloc void * malloc int, float int 16 2 32 4 float 16 4 32 4 cout << sizeof(char) << endl; cout << sizeof(int) << endl; cout << sizeof(unsigned int) << endl; cout << sizeof(long) << endl; cout << sizeof(unsigned long) << endl; cout << sizeof(float) << endl; cout << sizeof(double) << endl; cout << sizeof(void *) << endl; malloc () sizeof p = malloc(sizeof(p)) free void free( void * memblock ); free malloc p free(p) p NULL free p p NULL free p 7.11 new/delete new malloc int *p1 = (int *)malloc(sizeof(int) * length); int *p2 = new int[length]; new sizeof new new class Obj public : Obj(void); // 2001 Page 55 of 101

Obj(int x); // void Test(void) Obj *a = new Obj; Obj *b = new Obj(1); // 1 delete a; delete b; new Obj *objects = new Obj[100]; // 100 Obj *objects = new Obj[100](1);// 100 1 delete [] delete []objects; // delete objects; // delete objects[0] 99 7.12 C++/C C 1 C 1 2 2001 Page 56 of 101

8 C++ C C++ overloaded inline const virtual const virtual C++ 8.1 8.1.1 C++ C++ 8-1-1 EatBeef,EatFish,EatChicken Eat void EatBeef( ); void EatFish( ); void EatChicken( ); // void Eat(Beef ); // void Eat(Fish ); // void Eat(Chicken ); 8-1-1 Eat C++ 9 8.1.2 2001 Page 57 of 101

void Function(void); int Function (void); int int x = Function (); Function C++/C Function 8-1-1 Eat _eat_beef _eat_fish _eat_chicken C++ C C void foo(int x, int y); C _foo C++ _foo_int_int C++ C C++ C extern C extern C void foo(int x, int y); // extern C #include myheader.h // C C++ foo C _foo _foo_int_int C++ C extern C include void Print( ); // class A void Print( ); // 2001 Page 58 of 101

Print Print :: ::Print( ); // Print 8.1.3 8-1-3 output int output float output(0.5) int float # include <iostream.h> void output( int x); // void output( float x); // void output( int x) cout << " output int " << x << endl ; void output( float x) cout << " output float " << x << endl ; void main(void) int x = 1; float y = 1.0; output(x); // output int 1 output(y); // output float 1 output(1); // output int 1 // output(0.5); // error! ambiguous call, output(int(0.5)); // output int 0 output(float(0.5)); // output float 0.5 8-1-3 2001 Page 59 of 101

8.2 override C++ 8.2.1 1 2 3 4 virtual 1 2 3 4 virtual 8-2-1 Base::f(int) Base::f(float) Base::g(void) Derived::g(void) #include <iostream.h> class Base public: void f(int x) cout << "Base::f(int) " << x << endl; void f(float x) cout << "Base::f(float) " << x << endl; virtual void g(void) cout << "Base::g(void)" << endl; ; class Derived : public Base public: virtual void g(void) cout << "Derived::g(void)" << endl; ; void main(void) Derived d; Base *pb = &d; pb->f(42); // Base::f(int) 42 2001 Page 60 of 101

pb->f(3.14f); // Base::f(float) 3.14 pb->g(); // Derived::g(void) 8-2-1 8.2.2 C++ 1 virtual 2 virtual 8-2-2 a 1 Derived::f(float) Base::f(float) 2 Derived::g(int) Base::g(float) 3 Derived::h(float) Base::h(float) #include <iostream.h> class Base public: virtual void f(float x) cout << "Base::f(float) " << x << endl; void g(float x) cout << "Base::g(float) " << x << endl; void h(float x) cout << "Base::h(float) " << x << endl; ; class Derived : public Base public: virtual void f(float x) cout << "Derived::f(float) " << x << endl; void g(int x) cout << "Derived::g(int) " << x << endl; void h(float x) cout << "Derived::h(float) " << x << endl; ; 8-2-2 a C++ 8-2-2 b bp dp 2001 Page 61 of 101

void main(void) Derived d; Base *pb = &d; Derived *pd = &d; // Good : behavior depends solely on type of the object pb->f(3.14f); // Derived::f(float) 3.14 pd->f(3.14f); // Derived::f(float) 3.14 // Bad : behavior depends on type of the pointer pb->g(3.14f); // Base::g(float) 3.14 pd->g(3.14f); // Derived::g(int) 3 (surprise!) // Bad : behavior depends on type of the pointer pb->h(3.14f); // Base::h(float) 3.14 (surprise!) pd->h(3.14f); // Derived::h(float) 3.14 8.2.3 8-2-2 b 8-2-3 pd->f(10) Base::f(int) Base::f(int) Derived::f(char *) 10 class Base public: void f(int x); ; class Derived : public Base public: void f(char *str); ; void Test(void) Derived *pd = new Derived; pd->f(10); // error 8-2-3 2001 Page 62 of 101

8-2-3 pd->f(10) Derived::f(char *) Derived f pd->f(10) f 8-2-3 pd->f(10) Base::f(int) Derived class Derived : public Base public: void f(char *str); void f(int x) Base::f(x); ; 8.3 C++ 8-3-1 void Foo(int x=0, int y=0); // void Foo(int x=0, int y=0) // 8-3-2 void Foo(int x, int y=0, int z=0); 2001 Page 63 of 101

void Foo(int x=0, int y, int z=0); 8-3-2 output #include <iostream.h> void output( int x); void output( int x, float y=0.0); void output( int x) cout << " output int " << x << endl ; void output( int x, float y) cout << " output int " << x << " and float " << y << endl ; void main(void) int x=1; float y=0.5; // output(x); // error! ambiguous call output(x,y); // output int 1 and float 0.5 8-3-2 8.4 8.4.1 C++ operator Complex Add(const Complex &a, const Complex &b); 2001 Page 64 of 101

Complex operator +(const Complex &a, const Complex &b); Complex a, b, c; c = Add(a, b); // c = a + b; // + [Murray, p44-p47] 8-4-1 = () [] -> += -= /= *= &= = ~= %= >>= <<= 8-4-1 C++ C 1 2 8.4.2 C++ 1 C++ int,float 2.. 3 C++ #,@,$ 4 8.5 8.5.1 2001 Page 65 of 101

C++ C CALL return #define MAX(a, b) (a) > (b)? (a) : (b) result = MAX(i, j) + 2 ; result = (i) > (j)? (i) : (j) + 2 ; + : result = ( (i) > (j)? (i) : (j) ) + 2 ; #define MAX(a, b) ( (a) > (b)? (a) : (b) ) result = MAX(i++, j); result = (i++) > (j)? (i++) : (j); C++ C++ this C++ C++ assert assert Debug Debug Release assert assert Debug Release assert 6.5 8.5.2 inline inline Foo inline void Foo(int x, int y); // inline 2001 Page 66 of 101

void Foo(int x, int y) Foo void Foo(int x, int y); inline void Foo(int x, int y) // inline inline inline inline C++/C class A public: void Foo(int x, int y) // // class A public: void Foo(int x, int y) // inline void A::Foo(int x, int y) 8.5.3 2001 Page 67 of 101

1 2 inline 8.6 C++ C++ 2001 Page 68 of 101

9 A C++ A A(void); // A(const A &a); // ~A(void); // A & operate =(const A &a);// 1 C++ Stroustrup 2 C++ String String class String public: String(const char *str = NULL); // String(const String &other); // ~ String(void); // String & operate =(const String &other); // private: char *m_data; // ; 9.1 C C++ 2001 Page 69 of 101

Stroustrup C++ Stroustrup ~ void [Eekel, p55-p56] 9.2 class A A(int x); // A ; class B : public A B(int x, int y);// B ; B::B(int x, int y) : A(x) // A const 5.4 2001 Page 70 of 101

class A A(void); // A(const A &other); // A & operate =( const A &other); // class B public: B(const A &a); // B private: A m_a; // ; 9-2(a) B A m_a 9-2 (b) B m_a B m_a A A a m_a B::B(const A &a) B::B(const A &a) : m_a(a) m_a = a; 9-2(a) 9-2(b) F class F public: F(int x, int y); // private: int m_x, m_y; int m_i, m_j; 2001 Page 71 of 101

9-2(c) F 9-2(d) F F::F(int x, int y) F::F(int x, int y) : m_x(x), m_y(y) m_x = x; m_i = 0; m_y = y; m_j = 0; m_i = 0; m_j = 0; 9-2(c) 9-2(d) 9.3 [Eckel, p260-261] 9.4 String // String String::String(const char *str) if(str==null) m_data = new char[1]; *m_data = 0 ; else int length = strlen(str); m_data = new char[length+1]; strcpy(m_data, str); 2001 Page 72 of 101

// String String::~String(void) delete [] m_data; // m_data delete m_data; 9.5 String a,b a.m_data hello b.m_data world a b b.m_data = a.m_data b.m_data b.m_data a.m_data a b m_data String a( hello ); String b( world ); String c = a; // c(a); c = b; // String c(a) 9.6 String // String::String(const String &other) 2001 Page 73 of 101

// other m_data int length = strlen(other.m_data); m_data = new char[length+1]; strcpy(m_data, other.m_data); // String & String::operate =(const String &other) // (1) if(this == &other) return *this; // (2) delete [] m_data; // 3 int length = strlen(other.m_data); m_data = new char[length+1]; strcpy(m_data, other.m_data); // 4 return *this; String 9.4 NULL NULL String 1 a = a // b = a; c = b; a = c; // b = &a; a = *b; 2001 Page 74 of 101

delete if if(this == &other) if( *this == other) 2 delete 3 strlen 0 strcpy 0 4 a = b = c return *this return this return other other other return other 9.7 class A private: A(const A &a); // A & operate =(const A &a);// ; A b(a); // b = a; // A 9.8 2001 Page 75 of 101

virtual #include <iostream.h> class Base public: virtual ~Base() cout<< "~Base" << endl ; ; class Derived : public Base public: virtual ~Derived() cout<< "~Derived" << endl ; ; void main(void) Base * pb = new Derived; // upcast delete pb; ~Derived ~Base ~Base class Base public: Base & operate =(const Base &other); // Base private: int m_i, m_j, m_k; ; class Derived : public Base public: 2001 Page 76 of 101

Derived & operate =(const Derived &other); private: int m_x, m_y, m_z; ; // Derived Derived & Derived::operate =(const Derived &other) // 1 if(this == &other) return *this; // 2 Base::operate =(other); // // 3 m_x = other.m_x; m_y = other.m_y; m_z = other.m_z; // 4 return *this; 9.9 C++ Big-Three Big-Three [Cline] [Meyers] [Murry] 2001 Page 77 of 101

10 Object Class Instance C++ Inheritance Composition COM CORBA C++ COM CORBA 10.1 A B A B A class A public: void Func1(void); void Func2(void); ; class B : public A public: void Func3(void); void Func4(void); ; main() B b; b.func1(); b.func2(); b.func3(); b.func4(); // B A Func1 // B A Func2 C++ 2001 Page 78 of 101

10-1-1 A B B B A 10-1-2 B A a kind of B A Man Human Boy Man Human Boy Man class Human ; class Man : public Human ; class Boy : public Man ; 10-1-2 Ostrich Bird Ostrich Bird Ostrich::Fly class Bird public: virtual void Fly(void); ; class Ostrich : public Bird ; Circle Ellipse Circle Ellipse 2001 Page 79 of 101

B A A B B A 10.2 10-2-1 A B a part of B A A B Eye Nose Mouth Ear Head Head Eye Nose Mouth Ear 10-2-1 class Eye public: void Look(void); ; class Nose public: void Smell(void); ; class Mouth public: void Eat(void); ; class Ear public: void Listen(void); ; // class Head public: void Look(void) m_eye.look(); void Smell(void) m_nose.smell(); void Eat(void) m_mouth.eat(); void Listen(void) m_ear.listen(); private: Eye m_eye; Nose m_nose; Mouth m_mouth; Ear m_ear; ; 10-2-1 Head Eye Nose Mouth Ear Head Eye Nose Mouth Ear Head Look 2001 Page 80 of 101

Smell Eat Listen 10-2-2 // class Head : public Eye, public Nose, public Mouth, public Ear ; 10-2-2 Head Eye Nose Mouth Ear 2001 Page 81 of 101

11 11.1 const const C++ const const const const constant const C++ Use const whenever you need 11.1.1 const const const const StringCopy void StringCopy(char *strdestination, const char *strsource); strsource strdestination strsource const strsource const void Func1(int x) void Func1(const int x) void Func2(A a) void Func2(const A a) A void Func(A a) A a void Func(A &a) void Func(A &a) a const void Func(const A &a) void Func(int x) void Func(const int &x) const & 11-1-1 2001 Page 82 of 101

const void Func(A a) void Func(const A &a) const void Func(int x) void Func(const int &x) 11-1-1 const & 11.1.2 const const const const char * GetString(void); char *str = GetString(); const char *str = GetString(); const int GetInt(void) const int GetInt(void) A GetA(void) const A GetA(void) A A GetA(void) const A & GetA(void) 6.2 class A A & operate = (const A &other); // ; A a, b, c; // a, b, c A a = b = c; // 2001 Page 83 of 101

(a = b) = c; // const a = b = c (a = b) = c 11.1.3 const const const const stack GetCount GetCount const GetCount class Stack public: void Push(int elem); int Pop(void); int GetCount(void) const; // const private: int m_num; int m_data[100]; ; int Stack::GetCount(void) const ++ m_num; // m_num Pop(); // const return m_num; const const 11.2 11-2-1 2001 Page 84 of 101

11-2-2 11-2-3 11-2-4 11-2-5 11-2-6 11.3 11-3-1 && <= >= 1 11-3-2 11-3-3 11-3-4 11-3-5 11-3-6 11-3-7 I/O 11-3-8 11-3-9 11-3-10 2001 Page 85 of 101

11-3-11 11-3-12 11-3-13 11-3-14 PC-Lint LogiScope 2001 Page 86 of 101

[Cline] Marshall P. Cline and Greg A. Lomow, C++ FAQs, Addison-Wesley, 1995 [Eckel] Bruce Eckel, Thinking in C++ C++ 2000 [Maguire] Steve Maguire, Writing Clean Code 1993 [Meyers] Scott Meyers, Effective C++, Addison-Wesley, 1992 [Murry] Robert B. Murry, C++ Strategies and Tactics, Addison-Wesley, 1993 [Summit] Steve Summit, C Programming FAQs, Addison-Wesley, 1996 2001 Page 87 of 101

A C++/C ifndef/define/endif? If for while do * public, protected, private min-length && max-information 2001 Page 88 of 101

if? 1 TRUE FALSE 1 0 2 == = 3 == = NULL Case break switch default goto? C++ const const const 2001 Page 89 of 101

return assert assert return const const Use const whenever you need malloc new NULL 1 2 free delete NULL malloc/free new/delete malloc new/delete C++ 2001 Page 90 of 101

C++ 1 2 3 4 1 2 3 4 *this 1 2 3 virtual 4 1 B A A B B A 2 A B a part of B A A B 2001 Page 91 of 101

I/O 2001 Page 92 of 101

B C++/C C++/C C++/C C++/C 90 BOOL, float, if 10 0, 0.0, FALSE int n if if ( n == 0 ) if ( n!= 0 ) BOOL flag if float x if char *p if Windows NT 32 C++ sizeof 10 char str[] = Hello ; char *p = str ; int n = 10; sizeof (str ) = void Func ( char str[100]) sizeof( str ) = sizeof ( p ) = sizeof ( n ) = void *p = malloc( 100 ); sizeof ( p ) = 2001 Page 93 of 101

25 1 ifndef/define/endif 2 #include <filename.h> #include filename.h 3 const 4 C++ C extern C 5 for // for (i=0; i<n; i++) if (condition) DoSomething(); else DoOtherthing(); // if (condition) for (i=0; i<n; i++) DoSomething(); else for (i=0; i<n; i++) DoOtherthing(); 2001 Page 94 of 101

20 void GetMemory(char *p) p = (char *)malloc(100); void Test(void) char *str = NULL; GetMemory(str); strcpy(str, "hello world"); printf(str); char *GetMemory(void) char p[] = "hello world"; return p; void Test(void) char *str = NULL; str = GetMemory(); printf(str); Test Test Void GetMemory2(char **p, int num) void Test(void) *p = (char *)malloc(num); char *str = (char *) malloc(100); strcpy(str, hello ); void Test(void) free(str); if(str!= NULL) char *str = NULL; GetMemory(&str, 100); strcpy(str, world ); strcpy(str, "hello"); printf(str); printf(str); Test Test 2001 Page 95 of 101

strcpy 10 strcpy char *strcpy(char *strdest, const char *strsrc); strdest strsrc 1 C++/C strcpy 2 strcpy strsrc strdest char * String 25 String class String public: String(const char *str = NULL); // String(const String &other); // ~ String(void); // String & operate =(const String &other); // private: char *m_data; // ; String 4 2001 Page 96 of 101

C C++/C BOOL, float, if 10 BOOL flag if 3 if ( flag ) if (!flag ) float x if 4 if (flag == TRUE) if (flag == 1 ) if (flag == FALSE) if (flag == 0) const float EPSINON = 0.00001; if ((x >= - EPSINON) && (x <= EPSINON) if (x == 0.0) == = if (x!= 0.0) >= <= char *p if 3 if (p == NULL) if (p == 0) if (p!= NULL) if (p!= 0) if (p) if (!) Windows NT 32 C++ sizeof 10 char str[] = Hello ; char *p = str ; int n = 10; sizeof (str ) = 6 void Func ( char str[100]) sizeof( str ) = 4 2 2 sizeof ( p ) = 4 2 void *p = malloc( 100 ); sizeof ( n ) = 4 2 sizeof ( p ) = 4 2 2001 Page 97 of 101

25 1 ifndef/define/endif 5 2 #include <filename.h> #include filename.h 5 #include <filename.h> filename.h #include filename.h filename.h 3 const 5 1 const 2 const const 4 C++ C extern C 5 C++ C C++ C void foo(int x, int y); C _foo C++ _foo_int_int C++ C extern C 5 for 5 for (i=0; i<n; i++) if (condition) DoSomething(); else DoOtherthing(); if (condition) for (i=0; i<n; i++) DoSomething(); else for (i=0; i<n; i++) DoOtherthing(); N-1 2001 Page 98 of 101

5 20 void GetMemory(char *p) p = (char *)malloc(100); void Test(void) char *str = NULL; GetMemory(str); strcpy(str, "hello world"); printf(str); char *GetMemory(void) char p[] = "hello world"; return p; void Test(void) char *str = NULL; str = GetMemory(); printf(str); Test Test GetMemory GetMemory Test str NULL NULL strcpy(str, "hello world"); void GetMemory2(char **p, int num) void Test(void) *p = (char *)malloc(num); char *str = (char *) malloc(100); strcpy(str, hello ); void Test(void) free(str); if(str!= NULL) char *str = NULL; GetMemory(&str, 100); strcpy(str, world ); strcpy(str, "hello"); printf(str); printf(str); Test Test 1 hello 2 free(str); if(str!= NULL) 2001 Page 99 of 101

strcpy 10 strcpy char *strcpy(char *strdest, const char *strsrc); strdest strsrc 1 C++/C strcpy char *strcpy(char *strdest, const char *strsrc); assert((strdest!=null) && (strsrc!=null)); // 2 char *address = strdest; // 2 while( (*strdest++ = * strsrc++)!= 0 ) // 2 NULL ; return address ; // 2 2 strcpy strsrc strdest char * // 2 int length = strlen( strcpy( strdest, hello world ) ); String 25 String class String public: String(const char *str = NULL); // String(const String &other); // ~ String(void); // String & operate =(const String &other); // private: char *m_data; // ; String 4 // String String::~String(void) // 3 delete [] m_data; // m_data delete m_data; 2001 Page 100 of 101

// String String::String(const char *str) // 6 if(str==null) m_data = new char[1]; // NULL *m_data = 0 ; else int length = strlen(str); m_data = new char[length+1]; // NULL strcpy(m_data, str); // String::String(const String &other) // 3 int length = strlen(other.m_data); m_data = new char[length+1]; // NULL strcpy(m_data, other.m_data); // String & String::operate =(const String &other) // 13 // (1) // 4 if(this == &other) return *this; // (2) // 3 delete [] m_data; // 3 // 3 int length = strlen(other.m_data); m_data = new char[length+1]; // NULL strcpy(m_data, other.m_data); // 4 // 3 return *this; 2001Flyincloud