The C++ Programming Language.ppt
《The C++ Programming Language.ppt》由会员分享,可在线阅读,更多相关《The C++ Programming Language.ppt(42页珍藏版)》请在麦多课文档分享上搜索。
1、The C+ Programming Language,Lecture 1: From C to C+,Brief Introduction & Requirements,Coding Style Requirements - Naming Conventions,Hungarian Notation GlobalPrefixTypePrefix-BaseTag-Name GlobalPrefix C+ member variables start with: m_ Global variables start with: g_ Static data members start with:
2、s_ Classes start with: C Interfaces start with: I Standard Prefix: p A pointer. CHAR* psz; rg An array. DWORD rgType; pp A pointer to a pointer. int* ppTop; h A handle. HANDLE hFile; ref A reference to. char Func(string,Coding Style Requirements - Naming Conventions (cont.),Standard “BaseTag” void -
3、 v int - i BOOL - f UINT - ui BYTE - b CHAR - ch Double - d float - fl WCHAR - wch ULONG - ul LONG - l DWORD - dw HRESULT - hr fn - function sz - NULL str USHORT, SHORT, WORD - w Name Meaningful and Capitalizing every words Example m_pdTopOfStack CTypeLib g_fSwitchForDest s_szStartingURL,Coding Styl
4、e Requirements - Naming Conventions (cont.),Lets have some tries A flag indicates whether a C+ object has been initialized: BOOL m_fInitialized; A Session ID: DWORD dwSessionID; A Pointer to BYTE buffer: BYTE* pbBuffer; A global buffer to store logfile filename: CHAR g_szLogFileMAX_NUM; A pointer to
5、 a global logfile name: CHAR* g_pszLogFile;,Coding Style Requirements - Code Indentation,4 spaces per indent level,Class CExample public:/member functions declarationCexample();Cexample();private:#ifdef _DEBUGvoid TraceValue( int iValue );#endif ;,While (foo) if (NULL = bar)Func1(ad);elseFunc2(bc);F
6、unc3(); ,Braces in Statements,Useful Concepts Review,Reviewing C in C+,int functionA(int iX, int iY); int functionA(int iX, int iY) ; functionA(3,5);,The first is a declaration, the second is a definition (with null statements), the third is an invoking Declaration of a variable or function can appe
7、ar many times, while definition only once iX & iY - parameters, 3 & 5 - arguments,Reviewing C in C+ (cont.),int iX; extern int iX; int functionB(); extern int functionB();,Keyword extern means it is only a declaration, its definition is later or external to this file, asking the compiler not to assi
8、gn memory or generate code extern int functionB() ; /-invalid,Reviewing C in C+ (cont.),int funcA() static int iX = 0; ,iX is a “local global variable” to the file Its scope is only in this file,Function can “remember ” iX between calls But iX is unavailable outside the function, its scope is in thi
9、s function,static int iX = 0; int funcA() ,Reviewing C in C+ (cont.),int iX = 1024, iY = 4096; int ,Reference rX could be regarded as an alias or another name of iX Could rX represent iY later? - No! pi = /-Only a pointer could do that,Reviewing C in C+ (cont.),int Swap(int iX, int iY); int Swap(int
10、* pX, int* pY); int Swap(int ,The first two are passing by value, the last is passing by reference The first could not modify the value of arguments outside of Swap, while the last two could,Reviewing C in C+ (cont.),3 Reasons to use passing by reference: To modify the objects that passed in the fun
11、ction To avoid the cost of copying large objects To have more than one return values,struct Matrix double a1000010000 ;int f1(Matrix m); int f2(Matrix,bool add_even(int a, int b, int ,Reviewing C in C+ (cont.),The inner mechanism of reference and pointer may be similar, but they are quite different
12、in usage Using Reference when you find It always represents a non-null object And it will not represent any other objects Using Pointer otherwise Pointer could be re-assigned values, while reference could not,Reviewing C in C+ (cont.),int f(int const is also recommended to replace the #define macro,
13、 for the visibility in symbol table during compiling,Reviewing C in C+ (cont.),Overview of the const const int*p =,Reviewing C in C+ (cont.),class CExap class CExap public: public:int m_iX; int m_iX; ; ; CExap v; CExap* v;,v.m_iX = 0; v-m_iX = 0;,. is after an object, - is after a pointer (or iterat
14、or),Reviewing C in C+ (cont.),int main(int argc, char* argv) ;argc passes the number of elements in the argv array, argv are the character sub-sequences separated by spaces from the command line argv 0 is the path and program name, the real arguments start from argv 1 ,Lets compare these,#include vo
15、id main() int j;int v 5 = 1,2,3,4,5;for (j = 0; j 5; j+)printf(“%dn”, v j );,#include #include using namespace std; int main() vector v;for (int j = 0; j 5; j+)v.push_back(j + 1);for (int j = 0; j v.size(); j+)cout v j endl;return 0; ,C Style C+ Style,From C to C+,Say Goodbye To Our Old Friends, , W
16、e prefer the standard C+ library , , scanf(), printf(), fprintf(), We prefer the streaming operators cin , cout , File , malloc(), realloc(), free(), We prefer the C+ operators new & delete, to ensure the calling of constructors and destructors,Namespace,Wrap the library to avoid name collision in g
17、lobal scope, a better way than prefixesint abc_NumOfInstances = 0; namespace abcint abc_Create(int InstNo); int NumOfInstances = 0;int Create(int InstNo); std is the namespace wrapping all standard C+ libraries,Namespace(cont.),3 ways to use namespace Use prefix style to specify every time void f1()
18、 abc:Create(NumOfInstances);abc:NumOfInstances+ Import one symbol to current scope void f2() using abc:NumOfInstances; Create(NumOfInstances); /Error! Only NumOfInstances is visible, Create not importedNumOfInstances+; /OK Import all symbols in a namespace to current scope using directive void f3()
19、using namespace abc; Create(NumOfInstances); /OKNumOfInstances+; /OK ,Namespace(cont.),But we suggest the using directive form to expose all names in standard libraries using namespace std;,Vectors,Lots of legacy codes use array We recommend using the class template Features: A sequential container
20、Can use index to access Be self-aware of size Self-manage memory and increase on demand Frequent used member functions & operators: push_back(), pop_back(), size(), clear(), ,string class,The string class library #include string s; Important ops and member functions: =, +=, assign(), append(), at()
21、empty(), size(), find(), swap() c_str();,Streaming File,The streaming file class library #include ifstream InFile(“in.txt”); - Input file ofstream OutFile(“out.txt”); - Output file ofstream OutFile(“out.txt”, ios_base:app); - Output file (append mode) fstream IOFile(“io.txt”); - In-&Out-put file Usa
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- THECPROGRAMMINGLANGUAGEPPT
