(1) Read the following procedure to make up for outstanding comments
#include <iostream> #include <cstring>using namespace Std;class a{private: char *a;public: A (char *AA) { a = new Char[strlen (AA) +1]; (a) The significance of this approach is to open up a new space for storing data strcpy (A, AA); (b) The relationship between data member A and form parameter AA: Copy to copied relationship } ~a () { delete []a; (c) The significance of this process is to remove the new space in a timely manner and avoid wasting memory leaks } void output () { cout<<a<<endl; }}; int main () { A A ("Good Morning, Code monkeys!"); A.output (); A B ("Good Afternoon, codes!"); B.output (); return 0;}
(2) What happens if you remove the line where the comment (a) is located?
Program Run Error
Why does a data member occupy 1 of the storage space on a AA length basis? and the ending character ' --'
If pointer A is not a pointer to a character (that is, an address that is not a string), is it necessary to add 1? No
(3) To add a copy constructor for Class A, test with the following main function
#include <iostream> #include <cstring>using namespace Std;class a{private: char *a;public: A (char *AA) { a = new Char[strlen (AA) +1]; strcpy (A, AA); } A (a &b) { a = new Char[strlen (B.A) +1]; strcpy (A,B.A); } ~a () { delete []a; } void output () { cout<<a<<endl; }}; int main () { A A ("Good Morning, Code monkeys!"); A.output (); A B (a); B.output (); return 0;}
@ Mayuko
Week six project 1-deep copy experience