Nineth Week on-machine practice Project 1-deep copy experience

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.