The c ++ Programming Language Lecture 1 From c to c++
The C++ Programming Language Lecture 1: From C to C++
Brief introduction Requirements
Brief Introduction & Requirements
Coding Style Requirements - Naming Conventions Hungarian Notation o IGlobalPrefix[TypePrefix]-BaseTag-Name [GlobalPrefix] C++ member variables start with: m Global variables start with: g Static data members start with Classes start with: C Interfaces start with: I Standard [PrefIX: A pointer. CHAR* psz g An array. DWORD rgfype[.]i pp-A pointer to a pointer. int* ppTop i h -a handle. HANdle hFile ref -A reference to. char Func(string& refResult
Coding Style Requirements -- Naming Conventions ◼ Hungarian Notation ◼ [GlobalPrefix][TypePrefix]-BaseTag-Name ◼ [GlobalPrefix] ◼ C++ member variables start with: m_ ◼ Global variables start with: g_ ◼ Static data members start with: 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& refResult);
Coding Style Requirements Naming Conventions(cont) Standard“ BaseTag” void BOOL BYTE vfbd UINT >1i CHAR Double float fl WCHAR wch ULONG >u1 LONG NOI dw HRESULT hr function Name-Meaningful and Capitalizing every word NULL str USHORT, SHORT, WORD ■ Example m_pdTopofstack CTypelib -g_fSwitchForDest s SZStartingURL
Coding Style Requirements -- Naming Conventions (cont.) ◼ Standard “BaseTag” void -> 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 Style Requirements Naming Conventions(cont) Let 's have some tries A flag indicates whether a C++ object has been initialized BOOL m iNitialized A Session id: dWord dwsessionID a Pointer to byte buffer BYTE* pbBuffer a global buffer to store logfile filename CHAR g SZLOgEile [MAX NUM] a pointer to a global logfile name CHAR* g ps lOgfIle;
Coding Style Requirements -- Naming Conventions (cont.) ◼ Let’s 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_szLogFile[MAX_NUM]; ◼ A pointer to a global logfile name: ◼ CHAR* g_pszLogFile;
Coding Style Requirements Code Indentation 4 spaces per indent level Braces in Statements Class EXample While( foo) public / /member functions declaration if(NULl = bar) Example( w Example Funcl(ad) private #ifdef DEBUG els void Trace Value( int iValue ) #endif Func2 (bc) Func30;
Coding Style Requirements -- Code Indentation ◼ 4 spaces per indent level Class CExample { public: //member functions declaration Cexample(); ~Cexample(); private: #ifdef _DEBUG void TraceValue( int iValue ); #endif … }; While (foo) { if (NULL == bar) { Func1(ad); } else { Func2(bc); } Func3(); } ◼ Braces in Statements
Useful Concepts Review
Useful Concepts Review
Reviewing c in C++ int functionA(int iX, int iY); int functionA(int iX, int iIi functionA 3,5) The first is a declaration the second is a definition (with null statements), the third is an invoking a Declaration of a variable or function can appear many times, while definition only once iX ir- parameters,3&5- arguments
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 appear many times, while definition only once ◼ iX & iY - parameters, 3 & 5 - arguments
Reviewing C in C++(cont) int iX extern int ix: int function; extern int function BO; Keyword extern? means it is only a declaration, its definition is later or external to this file, asking the compiler not to assign memory or generate code extern int functionB0ti//---invalid
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 assign memory or generate code ◼ extern int functionB() {}; //---invalid
Reviewing C in C++(cont) int funCAO static intⅸ=0 int funCAO static intⅸ=0 a Function can" remember >> ix is a local oloba/ i between calls variable” to the file But iX is unavailable Its scope is only in outside the function its this file scope is in this function
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 this function static int iX = 0; int funcA() { … }