C/C ++ simple notes for beginners-1-

Source: Internet
Author: User

1. object-oriented programming file structure:
1) header file. h, including class definition.
2) source file. cpp, member functions of the class,
3) source file. cpp, write the main function

2. C ++ stores the code of member functions in a public area of the computer memory to save memory, so that all objects of this class can be shared.

3. object initialization: 1) Use the member function putXxx 2) use the constructor.

4. delete usage

Delete pointer name // variable delete * p

Delete [] pointer name // One-dimensional array delete [] p;

Delete [number of rows] pointer name // two-dimensional array delete [5] p;

As long as the new function is used in the program to dynamically allocate space, you must use delete to release the memory space at the end.


5. C ++ has three permissions: public private protect. Only public can be directly accessed outside the class. The default permission is private.


6. ':' indicates that the member variable or member method belongs to the class.


7. About this pointer.

Functions
Void student: put (int id, char * name, student * const this)
{This-> id = id ;...}

This is a read-only pointer to the object and is an implicit parameter. It is automatically set and maintained by the system behind the scenes.

If you want to return the current object, return * this;


8. The constructor can be initialized in the constructor. Note: void cannot be added before the constructor; otherwise, an error occurs!

If the constructor is overloaded, the system will not automatically generate the constructor.

So as long as the constructor is reloaded, it is better to write the constructor without parameters. This is a good habit.


9. The copy function is used to copy objects. The default copy function performs the copy operation.

Copy Function Form
Type: Class Name (Class Name & object alias)
{...}
For example:
A;
A B ();
A c =;

All of the above implement object replication, so I don't know the difference for the moment.


10. The Destructor is automatically called when the object disappears. The default destructor does nothing.
A a1;

A * a2 = new ();

At the end of the program, a1 will automatically call the destructor, while a2 will not automatically call the Destructor!

I think it may be that a1 is in the stack memory. After running, the stack pops up, so the object disappears and will automatically call the destructor.

The declaration of a2 is in the stack memory, while the instantiation of new A () is allocated in the heap. When the stack is played, a2 will disappear, but new A () will not disappear.

Therefore, the Destructor is not called. Delete * a2 is required for release.


11. Pre-compile

# Ifdef DEBUG

Debug testing is supported.

# Endif

The preceding statements can be used for debug testing. If you do not want to execute the statements in debug, you can remove the previous defined values.


12. The statement declared in static mode is executed only once.

For example:

For (I = 0; I <10; I ++)
{

Static int B = 0;

B ++;

Cout <

} // You can see that the value of B is constantly increasing, meaning that the statement modified with static is only executed once!


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: [email protected] 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.