Fifth chapter: Structure of C + + program

Source: Internet
Author: User
Tags class definition function prototype

Main content:

1. Scope and visibility

2. Lifetime of an object

3. Data and Functions

4. Static members

5. Protection of shared data

6. Friends

7. Compiling pre-processing commands

8. Multi-file structure and engineering

Scope: Function prototype scope, block scope, class scope, file scope

Function prototype scope of the variable should be out of the function of the parentheses of the head has no effect???? A bit not understand!!!

The private and protected members can be accessed in the friend function.

Friend function

Friend Classs

Friend is one-way

Friend function Some confused, need to look again???

Const type:

1.const type descriptor & reference name

2. Class Name Const object name

3. Type descriptor const array name [size] ...

4.const type specifier *ptr or type specifier const *PTR

5. Type descriptor * Const PTR

6. Type specifier function name (parameter table) const;

The question is raised

We already know that classes have the properties of encapsulation and information hiding. Only member functions of a class can access private members of a class, and other functions in the program cannot access private members. Non-member functions can access public members in a class, but if the data members are defined as public, this destroys the hidden features. In addition, it should be seen that in some cases, especially when multiple calls are made to certain member functions, time overhead is required due to parameter passing, type checking, and security checking, which affects how efficiently the program runs.

In order to solve the above problems, a scheme is proposed to make the UF element. A friend is a normal function that is defined outside the class, but he needs to be described in the class, in order to distinguish it from the member functions of the class, to be the keyword friend before the description. A friend is not a member function, but he can access a private member in a class. The role of friends is to improve the efficiency of the program, but he destroys the encapsulation and concealment of the class, allowing non-member functions to access the private members of the class.

A friend can be a function, which is called a friend function, and a friend can also be a class, which is called a friend class.

   friend function

A friend function is characterized by the ability to access a non-member function of a private member of a class. The friend function is syntactically identical to the normal function, which is the same as the normal function on the definition and the call. The following example illustrates the application of a friend function.

#include
#include

Class Point
{
Public
Point (double xx, double yy) {x=xx; y=yy;}
void Getxy ();
Friend Double Distance (Point &a, point &b);
Private
Double x, y;
};

void Point::getxy ()
{
cout<< "(" <<< "," <<< ")" <<endl;>< font>
}

Double Distance (Point &a, point &b)
{
Double dx = a.x-b.x;
Double dy = a.y-b.y;
return sqrt (dx*dx+dy*dy);
}

void Main ()
{
Point P1 (3.0, 4.0), P2 (6.0, 8.0);
P1. Getxy ();
P2. Getxy ();
Double d = Distance (P1, p2);
cout<< "Distance is" <<<endl;>< font>
}

Description: A friend function distance () is described in the Point class in the program, and he adds the friend keyword in front of the description, identifying that he is not a member function, but a friend function. His definition method is the same as the normal function definition, and differs from the definition of the member function because he does not need to indicate the class to which it belongs. However, he is able to refer to private members in a class, where a.x,b.x,a.y,b.y are private members of the class, and they are referenced by objects. When you call the META function, it is the same as the normal function, not called as a member function. In this case, p1. Getxy () and P2. Getxy () This is a call to a member function to be represented by an object. While distance (P1, p2) is a friend function call, he calls directly, without object representation, whose arguments are objects. (The function of the program is known two-point coordinates, to find out the distance of two points.) )

   Friend class

Friend In addition to the previous function, friend can also be a class, that is, a class can be a friend of another class. When a class is a friend of another class, this means that any member function of the class is a friend function of another class.

---------------------------------------------------another article

after adopting the mechanism of the class, the data hiding and encapsulation are realized, the data members of the class are generally defined as private members, and the member functions are generally defined as public, and the communication interface between the class and the outside world is provided. However, there are times when you need to define functions that are not part of a class, but that require frequent access to the data members of the class, you can define these functions as friend functions for that function. In addition to the friend function, there are friend classes, which are collectively referred to as friends. The role of friends is to improve the efficiency of the program's operation (that is, reducing the type checking and security checks and so on requires time overhead), but it destroys the encapsulation and concealment of the class, so that non-member functions can access the private members of the class.
friend function :
A friend function is a non-member function that can directly access a private member of a class. It is a normal function that is defined outside the class, it does not belong to any class, but it needs to be declared in the definition of the class, simply by adding a keyword friend to the name of the friend, in the following format:
Friend type function name (formal parameter);

The declaration of a friend function can be placed either in the private part of the class or in the public part, which is no different, and is a friend function of the class.
A function can be a friend function of more than one class, and it needs to be declared separately in each class.
the call of the friend function is consistent with the method and principle of calling the general function.

Friend class :
All member functions of a friend class are friend functions of another class and can access hidden information in another class, including private members and protected members.
When you want a class to be able to access the private members of another class, you can declare the class as a friend of another class. The statement format that defines the friend class is as follows:
friend class name;
Where: friend and class are keywords, and the class name must be a class that has already been defined in the program.

For example, the following statement illustrates that Class B is a friend class of Class A:
class A
{
.....
Public
friend class B;
.....
      };
after the above instructions, all member functions of Class B are friend functions of Class A, which can access the private and protected members of Class A.

Note When using friend classes:
(1) a friend relationship cannot be inherited.
(2) friend relationship is one-way and not commutative. If Class B is a friend of Class A, class A is not necessarily a friend of Class B, it depends on whether there is a corresponding declaration in the class.
(3) friend relationship does not have transitive nature. If Class B is a friend of Class A, Class C is a friend of B, Class C is not necessarily a friend of Class A, but also to see if there is a corresponding declaration in the class

Precautions:

1. Friends can access private members of the class.

2. Only within a class definition, a friend declaration can be placed anywhere in the class, typically at the beginning or end of a class definition.

3. A friend can be a normal non-member function, or a member function of another class previously defined, or an entire class.

4. The class must declare each function in the overloaded function set to be a friend.

5. A friend relationship cannot inherit, and the friend of the base class does not have special access rights to the members of the derived class. If the base class is granted a friend relationship, only the base class has special access rights. The derived class of the base class cannot access the class that grants the friend relationship.

Friend £ º

1. Variable permissions about: Private/protected their way of access

2. The declaration, definition, and invocation of friends are similar to normal functions

Fifth chapter: Structure of C + + program

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: 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.