Deep Replication 2

Source: Internet
Author: User


  1. *copyright (c) 2016, College of Computer and Control engineering, Yantai University
  2. *all rights reserved
  3. * File name: 123.cpp
  4. * Author: Zong Tao
  5. * Completion Date: May 10, 2016
  6. * Version number: v1.0
  7.  *
  8. * Problem Description: Read program, add copy constructor
  9. * Input Description:
  10. * Program output:
  11. */   
  12. #include <iostream>   
  13. #include <cstring>   
  14. using   namespace std;
  15. class A
  16. {
  17. Private :  
  18. Char  *a;
  19. Public :  
  20. A (char *aa)
  21. {
  22. A = new char[strlen (AA) +1]; //The meaning of this processing is that it can not waste memory space, save many system units
  23. strcpy (A, AA); The relationship between the data member A and the form parameter AA is a and AA are the same as the pointer array, the system allocates the same memory space   
  24. }
  25. A (a &b)
  26. {
  27. a=New char[strlen (B.A) +1];
  28. strcpy (A,B.A);
  29. }
  30. ~a ()
  31. {
  32. Delete      []a; //This means that the memory space pointed to by the pointer can be disposed of, otherwise it will result in dynamically allocated memory
  33. } //cannot be recycled, which makes the program occupy more and more memory
  34. void  output () {cout<<a<<endl;}
  35. };
  36. int Main () {
  37. A A ("Good Morning, code monkeys!"  );
  38. A.output ();
  39. A B (a);
  40. B.output ();
  41. return  0;
  42. }
  43. <pre name= "code" class= "CPP" >/* *copyright (c) 2016, College of Computer and Control engineering, Yantai University *all rights reserved * File name: 123.cpp *    Creator: Wang Lui * Completion Date: April 24, 2016 * Version number: v1.0 * * Problem Description: Read program, add copy constructor * 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];   The significance of this processing is that it can not waste memory space, save a lot of system unit        strcpy (A, AA);     The relationship between the data member A and the form parameter AA is a and AA are the same as the pointer array, the system allocates the same memory space    }    A (a &b)        {            a=new char[strlen (B.A) +1];            strcpy (A,B.A);        }    ~a ()    {        delete []a;      The meaning of this processing is to release the memory space pointed to by the pointer, otherwise it will cause the dynamically allocated memory    }                    //cannot be reclaimed, making the program occupy more and more memory    void output () {cout<<a< <endl; }};int Main () {    A A ("Good Morning, Code monkeys!");    A.output ();    A B (a);    B.output ();    return 0;}
Operation Result:

Deep Replication 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.