Why use member functions and friend functions
This issue is crucial and directly affects the following understanding:
- Program data: data is program information, which will be affected by program functions. Encapsulation is a concept in object-oriented programming that binds data and functions that manipulate data together, which can avoid external interference and misuse, so as to ensure safety.
- Data encapsulation leads to another important OOP concept, namely data hiding. Data encapsulation is a mechanism that binds data and functions that manipulate data together. Data abstraction is a mechanism that only exposes the interface to users and hides the specific implementation details. C + + supports encapsulation and data hiding (public, protected, private) by creating classes. We already know that classes contain private, protected, and public members. By default, all items defined in a class are private.
Due to the encapsulation and hiding characteristics of C + +, only the member functions of class definitions can access the private data of class definitions. - Member function is the mechanism of data encapsulation and data hiding.
- Friend is a mechanism provided by C + + to destroy data encapsulation and data hiding.
Use of member functions
First look at the code
class Stock{ //class declaration private: std:: string company; long shares; double share_val; double total_val; void set_tot(){ total_val=shares* share_val;} public: void acquire(const std:: string & co, long n, double pr); void buy(long num, double price); void se11(long num, double price); void update(double price); void show(); };//note semicolon at the end
Note that private can not be written here. If it is not written, it is private by default.
Among them, company and shares are private data members of Stock class. If you attempt to access this data using a non member function, the member compiler prohibits this. If you try to crack the mechanism, friends are another option.
- Implement class member functions
(1) When defining a member function, use the scope resolution operator (::) to identify the class to which the function belongs;
void Stock::update(double price)
(2) Class methods can access the private components of a class.
Friends
C + + is developed from the structured C language and needs to take care of the habits of structured design programmers. Therefore, the accessible scope of private members can not be restricted too much.
C + + designers believe that if some programmers are really afraid of trouble and want to directly access the private members of the object outside the member function of the class, it is better to make a compromise to meet their wishes, which is also a compromise between immediate interests and long-term interests. Therefore, C + + has the concept of friend. For example, this is equivalent to saying that friends are trustworthy, so you can disclose some of your privacy to them.
Friends provide a mechanism for ordinary functions or class member functions to access private or protected members in another class. In other words, there are two forms of friends:
(1) Friend function: an ordinary function accesses private or protected members of a class.
(2) Friend class: member functions in class A access private or protected members in class B.
(3) Friend member function
- friend function
The first step to create a friend function is to put its prototype in the class declaration and add the keyword friend before the prototype declaration:
friend Time operator*(double m,constTime&t);
Note that the declaration can be outside or in a class
In this way, even if the operator is not a member function, it has the same access rights as the member function and can access private variables in the class.
- Friend class
All member functions of Class Y are friend functions of class X
class girl; class girl{ private: char *name; int age; friend boy; //Declare that the boy class is a friend of the girl class }; class boy{ public: void disp(girl &); }; void boy::disp(girl &x) //The function disp() is a member function of the boy class and a friend function of the girl class { //With the help of friends, in boy's member function disp, with the help of girl's object, directly access girl's private variable cout<<"girl's name is:"<<x.name<<",age:"<<x.age<<endl; }
- Member function as friend
A member function of Class Y is a friend function of class X
Objective: to make a member function of class y a friend of class X. specifically, in this member function of Class Y, the private variable of X can be directly used with the help of parameter X
Syntax: declared in public (itself a function)
Declaration: Declaration of friend + member function
Call: first define the object y of Y - use y to call its own member function - friend mechanism is used in its own member function
class Stock{ //class declaration private: std:: string company; long shares; double share_val; double total_val; void set_tot(){ total_val=shares* share_val;} public: void acquire(const std:: string & co, long n, double pr); void buy(long num, double price); void se11(long num, double price); void update(double price); void show(); }; class Market{ friend void Stock::acquire(const std:: string & co, long n, double pr); //acquire under the Stock class can be used as a friend function of the member function and can access the private variables of the class int price; int fiture; public: void stuff(); };
As shown in the code, the acquire function under the Stock class, as a friend function of the market, can access the private variables of the market class.
Difference between friend function and member function of class
The difference between friend functions and class member functions. Member functions have this pointer, while friend functions do not have this pointer. Friend functions cannot be inherited, just as a father's friend may not be a son's friend.