Week six project 1-deep copy experience

Source: Internet
Author: User

(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

Related Article

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.