(3) To add a copy constructor for Class A, test with the following main function
int main () { A A ("Good Morning, Code monkeys!"); A.output (); A B (a); B.output (); return 0;}
Reference Answer:
/* Copyright (c) 2014, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Chen Dani * Completion date: April 14, 2015 * Version number: v1.0 * * #include <iostream> #include <cstring>using namespace Std;class a{private: char *a;public: A (char *aa) { a = new Char[strlen (AA) +1]; (a) The meaning of this processing is: _ The actual length of a string of strings in addition to the character, at the end of what has a length of strcpy (A, AA); (b) The relationship between data member A and form parameter AA: _AA is copied to a, the two are equal } ~a () { delete []a; (c) The significance of such processing is that only through the new distribution of space, with delete to release the Recycle address (run once, assign once) if not released, the memory will be full, resulting in a panic. } void output () { cout<<a<<endl; } A (a &b) { a=new char[strlen (B.A) +1]; strcpy (A, B.A);} ; int main () { A A ("Good Morning, Code monkeys!"); A.output (); A B (a); B.output (); return 0;}
Experience: This will be very convenient to use! Feel, we learn the knowledge is more and more efficient, and hope that they can be well mastered! Come on, let's go!
Week six project one-deep copy experience (2)