C ++ basic syntax and basic syntax

Source: Internet
Author: User

C ++ basic syntax and basic syntax
1 Constructor

1. You cannot specify any return value, or even void.

2. Different from Java, c ++ is different from new objects. brackets should be omitted when constructors without parameters declare objects.

 

2 destructor

1. Add ~ before ~, Parameters are not allowed and return values are not allowed.

2. Only one destructor can be declared and public in each class.

 

Combination of Three Types
# Include <iostream. h> class A {public: A (int x) {cout <"class A construing \ t" <x <endl ;}~ A () {cout <"class A destroy" <endl ;}}; class B {public: B () {cout <"class B construing \ t" <endl ;}// you can use this method to initialize the private variable B (int x): B (x) {// B = x; cout <"class B construing \ t" <B <endl ;}~ B () {cout <"class B destroying" <endl ;}private: int B ;}; class C {public: C (int x, int y ): a1 (y) {cout <"Class A with y, C with xy construing, but B with nothing" <x <y <endl;} C (int x, int a, int B );~ C () {cout <"class C destroying" <endl;} private: A a1; B b1 ;}; C: C (int x, int, int B): b1 (B), a1 (a) {cout <"class a B c construing" <x <a <B <endl ;} int main () {C c1 (2, 3); C c2 (4, 5, 6); return 0 ;}

 

When creating a composite class, the constructor of the composite class is called according to the declared sequence of embedded objects in the class, instead of the sequence in the member initialization list. Finally, the constructor of the composite class is called.

The call of the Destructor is the opposite.

The initialization list of a member can be included only when the constructor is defined. If only the constructor is declared, the member initialization list cannot be included.

The member initialization list must also be used to initialize common data members in the class.

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.