Friend function and friend class of C + +

Source: Internet
Author: User

With the Friend keyword, we can declare a function that is not part of the current class in the current class, which can be a friend function of the current class.

#include <iostream>
using namespace Std;

Class Book
{
Public
Book ()
{
cout <<this->price << Endl; Here is a member variable that is not assigned to use, which is undesirable
cout << this->title << Endl;

}
Book (char* A, double p);
friend void display (book &b);
Private
Double Price;
char * title;
};

Book::book (char* A, double p)
{
title = A;
Price = P;
}

void display (book &b)
{
cout<< "The price" <<b.title<< "is $" <<b.price<<endl;
}

int main ()
{
Book Hello;
Display (Hello);
Book Alice ("Alice in Wonderland", 29.9);
Display (Alice);
Book Harry ("Harry Potter", 49.9);
Display (Harry);

return 0;
}

result:6.95314e-310

While we know that we can access the private variables in the class with the meta-function, it is also important to remember that member variables that are not initialized cannot be used, which can cause the program to crash.

In this example, display is a top-level function, in the book class, we declare it as a friend function with the Friend keyword, and as a result, in the display function body, we can access the title and price member variables of the private property. This is the function of the friend function. A friend function can access a private member in this class. If the display function is not a friend function of the book class, the member function of the public property must also be called in the body of the function. In this case, it is important to note that the parameter of the friend function is a reference to the class object, and that you must add the object name when accessing the private member variable.

Except that the top-level function can be defined as a friend function, the member functions of other classes can also be declared as friend functions of this class, as shown in Example 2.

Example 2

#include <iostream>
using namespace Std;

Class time;

Class Date
{
Public
Date (int y,int m,int D);
void display (time &t);
Private
int year;
int month;
int day;
};

Class time
{
Public
Time (int s,int m,int h);
friend void Date::d isplay (Time & T);
Private
int second;
int minute;
int hour;
};

Time::time (int s,int m,int h)
{
Second = s;
minute = m;
hour = h;
}

Date::d ate (int y,int m,int D)
{
Year = y;
month = m;
Day = D;
}

void Date::d isplay (Time &t)
{
cout<< "The time is:" <<endl;
cout<<year<< "/" <<month<< "/" <<day<< "";
cout<<t.hour<< ":" <<t.minute<< ":" <<t.second<<endl;
}

int main ()
{
Date d (2015,1,16);
Time t (20,2,30);
D.display (t);
return 0;
}







Friend function and friend class of C + +

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.