Week six project one-deep copy experience (2)

Source: Internet
Author: User

(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)

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.