Week 6 Project 1-deep replication experience (2) (3), week 6 experience
Problem
(2) What will happen if I remove the line in which the comment (a) is located? Why? Why does the storage space occupied by a data member increase by 1 Based on the aa length? If pointer a does not point to a character (that is, it is not the address of the string), is it necessary to add 1?
An error is prompted because the address is not initialized for the pointer, and a will change to a wild pointer.
Use '\ 0' to end the string '.
No.
(3) Add A copy constructor for Class A. Use the following main function to test
Int main () {A a ("good morning, code monkeys! "); A. output (); A B (a); B. output (); return 0 ;}
Code:
# 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 () {delete [] a;} A (A & c) {a = new char [strlen (c. a) + 1]; strcpy (a, c. a) ;}void output () {cout <endl ;}}; int main () {a ("good morning, code monkeys! "); A. output (); A B (a); B. output (); return 0 ;}
Running result:
Knowledge Point summary:
Copy constructor
Learning Experience:
Study hard every day