Copy constructor

xiaoxiao2021-03-06  22

Copy constructor format is classname (const classname & object) {// do something}

The following program can explain the problem, why the copy constructor is necessary

Class Samp {Char * S; Int Num; Public: SAMP (INT I) {s = '/ 0'; Num = i; cout << Num << endl;}

// Copy constructor SAMP (SAMP & OBL) {INT L = Strlen (obl.s); s = new char [l 1]; strcpy (s, obl.s);}

SAMP () {}

~ SAMP () {if (s) delete [] s; cout << "freeing s [" << Num << "] << endl;}

Void show () {cout << S << "/ n";}

Void Set (Char * STR);

Void Samp :: SET (CHAR * STR) {s = new char [strlen (str) 1]; if (! s) {cout << "allocation error./n"; exit (1);} strcpy (s , STR);

Void main (void) {samp ob; obshow (); return 0;}

转载请注明原文地址:https://www.9cbs.com/read-57232.html

New Post(0)