Questions and codes:
(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 such treatment lies in the following: ______________________________ strcpy (A, AA); (b) The relationship between data member A and form parameter AA: ___________________________________ } ~a () { delete []a; (c) The meaning of this processing is: ___________________________________________ } 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? Why? Why does a data member occupy 1 of the storage space on a AA length basis? 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?
(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;}
/* Copyright (c) 2016, College of Computer and Control engineering, Yantai University all rights reserced file name: Test.cpp : Cai Completion Date: May 9, 2016 version number: v1.0 Problem Description: Input Description: program output: */ #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 such treatment lies in the following: ______________________________ strcpy (A, AA); (b) The relationship between data member A and form parameter AA: ___________________________________ } A (a &b) { a=new Char[strlen ( B.A) +1]; strcpy (A,B.A); } ~a () { delete []a; (c) The meaning of this processing is: ___________________________________________ } void Output () {cout<<a< <endl; }};int Main () { A A ("Good Morning, Code monkeys!"); A.output (); A B (a); B.output (); return 0;}
Operation Result:
Summary of Knowledge points:
Learning experience:
Nineth Week on-machine practice Project 1-deep copy experience