From C language to C + + (i)

Source: Internet
Author: User

1. Input output stream

Using std::cout;//standard output stream
Using std::endl;//line Wrapping


cout << em1.t->a;
cout << Endl;


2. The statement of the structure body

In C + +, no need to add struct


3. Definition of Class

The class name is preceded by C, and the class member is preceded by a m_ if the keyword is not public and ":" and the default is "private", that is, if you are using in main. Call him, you'll get an error.


4. member functions of Class

When a member function calls a member variable, the declaration public of the member variable is not required. If the function is not public before, it cannot be accessed by the outside world. The function does not increase the size of the class, that is, he is stored in memory. When calling a function, you must use the dot operator to fetch him.

Class Cbox

{

Double vol. (void);

}

Double Cbox::vol ()//Type class name scope resolution operator function name

{}


5. Inline function

The compiler implicitly defines a function in a class as an inline function, and if the function declaration is outside, add the keyword inline.

The inline function should first ensure that there are no problems in the scope of the variable, and then it is suitable for short functions.

The inline function refers to the function body code to replace the function of the call, so as to improve the running speed.


6. Class constructors are used to initialize a class. No return value is allowed and void is not available. Named fixed, must be the same as the class name, and then add ()

The procedure is as follows:

Class Cbox

{

int m_a;

Cbox (int a)

{

M_a=a;

}

}

int main ()

{

Cbox box1 (1);

}

You can also write an int a=1 on a constructor, give him a default value, and then write Cbox box1 when you initialize it, but you can't write the default constructor if you write this.

7. Default constructor Cbox ()

By defining this, you can not assign an initial value. Then this requires a very long description will not say, you can see the visual 2008 Introductory Classics, Tsinghua University Press, 304, I just see this.


8. Another class constructor form

Cbox (int a)://Plus Colon
M_a (a)//assign value A, do not need;
{}//function body

9.public and private

In order to ensure the data security of the class, the use of private restrictions, but by constructing a public function, the function can modify the value of the private, to achieve the role of the interface. Before giving a reason not to private, it is best to use private.private the best to put down the face.


10. The friend function, which is not part of the class, is set in the class and becomes an inline function, if not defined in the class. Does not belong to a class and has at least some special permissions. Front plus friend.

Class Cbox

{

Private

int A;

Friend Double Bc (cbox box);

}

Double Bc (cbox box)//is different from a non-friend function, the front does not need to be (inline) Double cbox::

{

return box.a;

}


11. If there are no pointers and arrays in the element, you can use the system's default replication function (which you don't need to write).

Cbox box1 (1);

Cbox Box2=box1;


12.this pointers. This pointer points to the object that is used when calling the function, which is not actually written. (This sentence is difficult to understand)

Class Cbox

{

Public: .........

Private: .......

Public

int Compare (Cbox Boxx)

{

Return This->vol () >boxx. vol ();//Compare the size of the class that calls this function's volume and the parameter of the function, and remove this, the default vol () is The vol () of this class.

}

};


13. destructor

Destructors (destructor), in contrast to constructors, perform destructors automatically when an object ends its lifecycle (for example, the function where the object is called) is completed. Destructors are often used to "clean up" work (for example, when creating an object, a memory space is created with new, and should be released in the destructor before exiting).

As an example of the C + + language: [1] The destructor name should also be the same as the class name, but precede the function name with a bitwise backslash ~, such as ~stud (), to distinguish it from the constructor. It cannot take any parameters, and there is no return value (including void type). There can be only one destructor, not overload (overload, simply, a function or method has the same name, but a parameter list is not the same as a function or method with different parameters of the same name, called an overloaded function or method). If the user does not write a destructor, the compilation system automatically generates a default (default) destructor (even if a destructor is customized, the compiler will always synthesize a destructor for us, and if the destructor is customized, the compiler will invoke the custom destructor and then invoke the synthesized destructor at execution time. Nor does it do anything. So many simple classes do not use the displayed destructor. A truly useful destructor usually does not contain output information. It is generally necessary to write the Declaration and implementation of the class separately (the specific method code), which is also a good programming practice. That is, you can define member functions outside of a class, and only declare them in a class using the prototype of a function.


14. Several important concepts of dynamic memory

New,delete, heap, memory leak, variable array of dynamic memory, released after the assignment "0"


15. Input stream, Cin>>, control output interval SETW


16. If we define a Const class object, then if we want to invoke a member function of the class, we have to add a const like int Compare () const to the name of the function.

You should also precede the function body with the const modifier before the definition of the function is placed outside. In fact, we can define the const and non-const two versions of a function in the class.


17. An array of class objects, each of which will call the default constructor


18. Static data members of the class. Preceded by a static, with Int Cbox:: a=2; initialized, often placed in constructors for counting. Note that the member is shared. In fact, only we want static data members are not 0 values, we will initialize them. Static data members are often used to count classes to be called several times.


19. Static functions, static functions can be invoked without creating a class, and static functions must use static variables. Called by Box1.function () or cbox::function (). It can also be used to see if the object of the class has not been created or created several times.


20. Pointers and references to class objects. Create p=&box1; reference P->vol ();


21. Implement the copy constructor, Cbox (const cbox& INITB); we go directly to the INITB to avoid the situation where you are constantly creating a copy of the argument to initialize the parameter. Without changing the value of the parameter, we are accustomed to adding a const.

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.