//[email protected]: Defines the entry point of the console application. //#include"stdafx.h"#include<assert.h>/************************************************************************//*Copyright (c) kernel_main/* C + + Test Center/* Reprint Please specify the Source:http://www.cnblogs.com/kernel0815//************************************************************************///strcpy Function ImplementationChar* STRCPY (Char* STRDST,Const Char*strsrc) {Assert (NULL!=STRDST) && (NULL!=STRSRC));//assertion string Address not 0 Char* Address =STRDST; while((*strdst++ = *strsrc++)! =' /'); returnaddress;}//strlen Function ImplementationintStrlenConst Char*str) {Assert (NULL! = str);//assertion string Address not 0 intLen =0; while(*str++! =' /') {len++; } returnLen;}//String Implementationclassstring{ Public: String (Const Char* Str =null);//constructor FunctionString (string & str);//copy Constructor~string (void);// Destructors Public: voidprint (); String&operator=(ConstString &str);//Assignment FunctionPrivate: Char* M_DATA;//used to save strings}; String::string (Const Char*str) { if(NULL = =str) {m_data=New Char[1];//score points: Automatically apply for empty string to hold the end flag 'ASSERT (NULL! = m_data);//Add points: null judgment on M_data*m_data =' /'; } Else { intLen =strlen (str); M_data=New Char[len+1]; ASSERT (NULL! = m_data);//Better if you can add a NULL judgmentstrcpy (m_data, str); }}string::string (String&str) { intLen =strlen (Str.m_data); M_data=New Char[len+1];//Add points: null judgment on M_dataASSERT (NULL! =m_data); strcpy (m_data,str.m_data);} String::~String () {if(m_data) {delete[] m_data; M_data=NULL; }}string& String::operator=(Conststring&str) { if( This= = &str)//score points: Check self-assignment { return* This; } //scoring points: releasing the original memory resources if(m_data) {delete[] m_data; } intLen =strlen (Str.m_data); M_data=New Char[len+1];//Add points: null judgment on M_datastrcpy (m_data, str.m_data); return* This;//score points: Returns a reference to this object}voidString::p rint () {printf ("Pint:%s @ 0x%08x\n", m_data);}//detects if the CPU is Little_endian or Big_endian//in real memory://0x12345678 Four bytes (equivalent to a ulong, int) in the 32bit CPU is stored in the following form://Big_endian:12 34 56 78 (High-order bytes are stored in the start address, low byte high address, high-byte low address)//little_endian:78 56 34 12 (the low-order byte is stored in the start address, low-byte lower address, high-byte address in high)intCheckcpu () {Union u {intA; Charb; C // xx xxC.A =1;//Example: If we write 0X1234ABCD to memory starting with 0x0000, the result is//Big-endian Little-endian//0x0000 0x12 0xCD//0x0001 0x34 0xAB//0x0002 0xAB 0x34//0x0003 0xCD 0x12//in the same example: Write 1 (that is, 0x00000001)://Big-endian Little-endian//0x0000 0x00 0x01//0x0001 0x00 0x00//0x0002 0x00 0x00//0x0003 0x01 0x00 return(1= = C.B);//--If it's Big_endian,}int_tmain (intARGC, _tchar*argv[]) {String A ("Hello World."); String B (a); String C=b; Char* p =NULL; String d (P); D.print (); A.print (); B.print (); C.print (); printf ("This computer is%s cpu\n", Checkcpu ()?"Little_endian":"Big_endian"); return 0;}
"C + + Interview" Review of regular questions