C + + class programming (i)

Source: Internet
Author: User

When designing a class, consider the following five points

1. Constructor initialization list

2, the function should not add const

3, parameter transfer as far as possible to consider using reference to pass, consider adding no const

4. Return with no reference

5, the data as far as possible in private, function as far as possible on public

First, the constructor function

① constructor parameter name cannot be the same as class member name

② The compiler provides a default constructor only if no energy and constructors are defined

③ Two types of definitions for default constructors

1. Provide default values for all parameters of the existing constructor. Stock (const string & co= "Error", int n=0,double d=0.0);

2. Through function overloading, define a constructor without parameters. Stock ();

The ④ member Initializes a list of parameters.

Classy::classy (int n,int m): Mem1 (n), Men2 (0), Men3 (n*m+2)

{

//........

}

1. It can only be used for constructors

2, for non-static const data members, must be initialized with a device

3. The reference data member must be initialized with a device.

4. The order in which members are initialized is the same as the order in which they appear in the class declaration, regardless of the order in which they are ordered in the initializer

⑤ Destruction function

The program must have a destructor, and if not, the compiler will generate a destructor by default. When new is used in the constructor to allocate memory, you must use Delete to free memory.

Second, parameter reference

The primary purpose of a reference variable is to use a reference variable as a formal parameter so that the function can work with the original data. A reference is an alias for a defined variable, pointing to the same value and memory unit as the variable. A reference variable is used alone, and it must be initialized when declaring a reference, not as a pointer, but as an assignment of a value first. The following program will see that when the reference is passed, the function will use the raw data, and changing the reference will change the original data, unlike the past pass-by value.

#include <iostream>voidSWAPR (int& A,int&b);intMain () {using namespacestd; intm=3, n=9;    SWAPR (M,n); cout<<"m"<< m <<"N"<< N <<Endl; return 0;}voidSWAPR (int& A,int&b) {    using namespacestd; inttemp; Temp=A; A=C; b=temp; cout<<"a"<< a <<"b"<< b <<Endl;}

The results are as follows

The values of the original data and the formal parameters are exchanged.

Because changing the value of the reference, the original variable is modified at the same time, the information passed for the clear function is used, and the information is not modified, and the colleague uses a constant reference , as opposed to a normal variable, and the const variable belongs to the non-modifiable lvalue.

There are three benefits to declaring a reference parameter as a const reference:

① use const references to avoid programming errors that unintentionally modify data;

② using a const reference is a function that can handle both const and non-const arguments, otherwise it can only accept non-const arguments;

③ using a const reference allows the function to correctly generate and use temporary variables.

Function arguments use references, pointers, and values passed by value selection

For functions that use passed values without modification

1. If the data object is small, such as a built-in data type or small structure, it is passed by value
2. If the data object is an array, use the pointer (the only selection) and declare the pointer as a pointer to the const
3. If the data object is a larger structure, use a const pointer or a const reference to increase efficiency
4. If the data object is a class object, use the const reference

For functions that modify data in a function

1. If the data object is small, such as a built-in data type or small structure, use the pointer
2. If the data object is an array, use the pointer (the only selection)
3. If the data object is a larger structure, use a pointer or reference
4. If the data object is a class object, use the reference

Third, return the reference

Returning references can improve efficiency. Return a reference to the problem : Avoid returning a reference to a memory unit that does not exist when the function terminates (such as a temporary variable for life in a function). You can return a reference passed as a parameter to a function, or you can allocate a new storage space using new, but delete the storage space using Delete.

Generally, if a function is to return a local object, it should return an object instead of a reference, and if the function returns an object of a class that does not use the public copy constructor, this must return the object's reference. If the returned object needs to be modified, a non-const reference is returned, and if not modified, a const reference is returned.

Four, function plus const

As long as the class method does not modify the calling object, it should be declared as Const.

C + + class programming (i)

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.