Use C ++ class object to construct a function

Source: Internet
Author: User

The so-called C ++ Class Object: all objects of a class correspond to the same class object and want to learn C ++ class objects well. First of all, it is very important to understand the concept of the C ++ language. First, let's talk about what is C ++ language, the so-called C ++ language: it is a widely used computer programming language.

Before learning this chapter, we have learned about class constructor and destructor. For common type objects, copying between them is very simple, the objects of the defined classes are also objects. No one can prevent us from copying them in the following ways, for example:

 
 
  1. include <iostream>    
  2. using namespace std;    
  3.     
  4. class Test    
  5. {    
  6. public:    
  7.     Test(int temp)    
  8.     {    
  9.         p1=temp;    
  10.     }    
  11. protected:    
  12.     int p1;    
  13.     
  14. };    
  15.     
  16. void main()    
  17. {    
  18.     Test a(99);    
  19.     Test b=a;    

Common objects and class objects are the same as C ++ class objects, and their features are similar. class objects have member variables, while common objects do not, when the same replication method occurs on different objects. Then the system performs different operations on them. For Class objects, class objects of the same type are copied through constructors to complete the entire replication process.

 
 
  1. # Include<Iostream>
  2. Using namespace std;
  3. Class Test
  4. {
  5. Public:
  6. Test (int temp)
  7. {
  8. P1=Temp;
  9. }
  10. Test (Test & c_t) // here is the custom copy constructor.
  11. {
  12. Cout<<"Enter the copy constructor"<<Endl;
  13. P1=C_t. P1; // if this statement is removed, the replication cannot be completed.
  14. }
  15. Public:
  16. Int p1;
  17. };
  18. Void main ()
  19. {
  20. Test a (99 );
  21. TestB=A;
  22. Cout<<B. p1;
  23. Cin. get ();
  24. }

We didn't see the copy constructor, and we also completed the copy work. Why? Because when a class does not have a custom copy constructor, the system automatically provides a default copy constructor to complete the copy.

Next, to illustrate the situation (for example, the above Code), the C ++ class object defines a copy constructor that is the same as the default copy constructor of the system, let's see how it works internally! Test (Test & c_t) in the code above is our custom copy constructor. The name of the copy constructor must be consistent with the class name, A function form parameter is a reference variable of this type and must be a reference.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.