Relationship between common objects and common member functions

Source: Internet
Author: User

(1) Common objects
 

Objects modified with const are called object constants in the following format:
 

<Class Name> const <Object Name> or const <Class Name> <Object Name> 〉
 

When declared as a common object, it must be initialized and cannot be rewritten to the data member of the object.

 

 

Example:Analyze the following errors and explain the reasons;

# Include <iostream. h>

Class Point

{Int X, Y;

Public:

 

Point (int A, int B) {x = A; y = B ;}

Void movepoint (int A, int B) {x + = A; y + = B ;}

 

Void print () {cout <"x =" <x <"Y =" <Y <Endl ;}

};

Void main ()
{

 

Const point point1 (10, 10); // constant object

Point1.movepoint (); // the data of the Common Object point1 cannot be changed, so the error occurs.

Point1.print (); // The this pointer of the print function cannot point to a common object.

}

The cause of the error is added. 
 

Common objects are often used in object reference. The so-called frequent reference refers to the useConst modifier. The referenced object cannot be changed.

 

New. It is generally used as a form parameter. Format:
 

Const <type description> & reference Name> 〉 

Example:According to the class in Example 5-11ProgramResult:

# Include <math. h>

Int length (const point & P1, const point & p2)

{Int L;

L = SQRT (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));

Return L;

}

Void main ()

{

Point A (1, 1), B (4, 5 );

Cout <"the distance between two points is:" <length (a, B) <Endl;

}

Output of this program:

The distance between two points is: 5 

(2) common member functions

 

The function described in the const keyword is a common member function. The format is as follows:

<Type> <Function Name> (parameter table>) const;

Functions and functions of common member functions:

Common member functions cannot update object data or call non-const modified member functions. A common object can only call common member functions of a class.

And static member functions of the class. 

Example:The execution results of the following programs are given:

# Include <iostream. h>

Class Point

{Int X, Y;

Public:

Point (int A, int B) {x = A; y = B ;}

Void movepoint (int A, int B) {x + = A; y + = B ;}

Void print () const {cout <"x =" <x <"Y =" <Y <Endl ;}

};

Void main ()

{

Point point1 (1, 1 );

Const point point2 (2, 2); // constant object

Point1.print (); // a common object can call a common member function.

Point2.print (); // call a common member function for a Common Object

}

Execution result of this program:

X = 1 y = 1

X = 2 y = 2 

A member function described by the const keyword is a member function. The description format of a member function is as follows: 

<Return type description> <member function name> (<parameter table>) const;

 

Note the following when using common member functions: 

(1) const is an integral part of the function type. Therefore, the const keyword must be included in the function implementation part. 

(2) A common member function does not update the data member of an object, nor can it call a member function without const modification in this class. 

(3) A common object can only call its common member functions, but cannot call other member functions. The operation Relationship between member functions and objects is shown in figure

Table 12-1. 

(4) The const keyword can be used to distinguish between overloaded functions. For example: 
Void print (); 

Void print () const; 

These two functions can be used for overloading. The principle of overloading is that a common object calls a common member function, and a general object calls a common member function. 

(5) In the const member function: 

 

Member variables defined as mutable can be modified.

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.