11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 23: A New Assignment Operator, Should You Decide to Accept It 309}//delete existing stuff...deleteName();//...before replacing with new stuffcopyName(s.pszName, “ (replaced)”);//return reference to existing objectreturn *this;// very simple access functionchar* out() { return pszName; }protected:void copyName(char* pszN, char* pszAdd);void deleteName();char *pszName;};//copyName() - allocate heap memory to store namevoid Name::copyName(char* pszN, char* pszAdd){pszName = 0;if (pszN){pszName = new char[strlen(pszN) +strlen(pszAdd) + 1];strcpy(pszName, pszN);strcat(pszName, pszAdd);}}//deleteName() - return heap memoryvoid Name::deleteName(){if (pszName){delete pszName;pszName = 0;}}int main(int nNumberofArgs, char* pszArgs[]){// create two objectsName n1(“Claudette”);Name n2(“Greg”);cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!