C++友元初步

栏目: C++ · 发布时间: 4年前

内容简介:本例对C++友元的使用从代码上作了一个简单的归纳,但不仅限于本问讲到的用法,友元还可以重载等。**/

本例对C++友元的使用从代码上作了一个简单的归纳,但不仅限于本问讲到的用法,友元还可以重载等。

**

/ 全局函数作为友元 /

**

include <iostream>

include <cmath>

using namespace std;

class Point{

public:

Point(double x, double y){
    _x = x;
    _y = y;
}
void getFormatxy();
friend double distance(Point &a, Point &b);

private:

double _x, _y;

};

void Point::getFormatxy(){

cout<<"("<<_x<<","<<_y<<")"<<endl;

}

double distance(Point &a, Point &b){

double dx = a._x - b._x;
double dy = a._y - b._y;
return sqrt(dx*dx + dy*dy);

}

int main()

{

Point p1(3.0, 4.0), p2(6.0, 8.0);
p1.getFormatxy();
p2.getFormatxy();
double d = distance(p1, p2);
cout<<"distance is "<<d<<endl;
return 0;

}

**

/ 一个类的成员函数作友元 /

**

include <iostream>

include <cmath>

using namespace std;

class Point; //前向声明,用于声明。

class ManagerPoint{

public:

double distance(Point &a, Point &b);

};

class Point{

public:

Point(double x, double y){
    _x = x;
    _y = y;
}
void getFormatxy();
friend double ManagerPoint::distance(Point &a, Point &b);

private:

double _x, _y;

};

void Point::getFormatxy(){

cout<<"("<<_x<<","<<_y<<")"<<endl;

}

double ManagerPoint::distance(Point &a, Point &b){

double dx = a._x - b._x;
double dy = a._y - b._y;
return sqrt(dx*dx +dy*dy);

}

int main()

{

Point p1(3.0, 4.0), p2(6.0, 8.0);
p1.getFormatxy();
p2.getFormatxy();
ManagerPoint mp;
float d = mp.distance(p1, p2);
cout<<"distance is"<<d<<endl;
return 0;

}

**

/ 友元类 /

**

include <iostream>

include <cmath>

using namespace std;

class Point{

public:

friend class ManagerPoint;
Point(double x, double y){
    _x = x;
    _y = y;
}
void getFormatxy();

private:

double _x, _y;

};

void Point::getFormatxy(){

cout<<"("<<_x<<","<<_y<<")"<<endl;

}

class ManagerPoint{

public:

double distance(Point &a, Point &b);

};

double ManagerPoint::distance(Point &a, Point &b){

double dx = a._x - b._x;
double dy = a._y - b._y;
return sqrt(dx*dx + dy*dy);

}

int main()

{

Point p1(3.0, 4.0), p2(6.0, 8.0);
p1.getFormatxy();
p2.getFormatxy();
ManagerPoint mp;
float d = mp.distance(p1, p2);
cout<<"distance is"<< d<<endl;
return 0;

}

**

/ 友元小结 /

**

//友元声明以关键字friend开始,它只能出现在类定义中

//因为友元不是类授权的成员,所以它不受其所在区域的public private 和protected 的影响

//通常,把所有友元声明组织在一起并放在类头之后.

//友元利弊:

//友元不是类成员,但是它可以通过对象访问类中的私有成员,友元的作用在于提高程序的运行效率

//但是,它破坏了封装性,使得非成员函数可以访问类的私有成员

//注意: 友元关系不能被继承,友元关系不具有传递性

include <iostream>

using namespace std;

class Time{

public:

friend class Watch;
Time(int hour, int min, int sec){
    m_iHour = hour;
    m_iMinute = min;
    m_iSecond = sec;
}

private:

int m_iHour;
int m_iMinute;
int m_iSecond;

};

class Watch{

public:

Watch(Time t): m_tTime(t){}
void display(){
    cout<<m_tTime.m_iHour<<endl;
    cout<<m_tTime.m_iMinute<<endl;
    cout<<m_tTime.m_iSecond<<endl;
}

public:

Time m_tTime;

};

int main()

{

Time t(6, 30, 20);
Watch w(t);
w.display();
return 0;

}


以上所述就是小编给大家介绍的《C++友元初步》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

设计模式之禅

设计模式之禅

秦小波 / 机械工业出版社 / 2010年3月 / 69.00元

如果说“四人帮”的《设计模式》是设计模式领域的“圣经”,那么之后出版的各种关于设计模式的书都可称之为“圣经”的“注释版”或“圣经的故事”。本书是得道者对“圣经”的“禅悟”,它既不像“圣经”那样因为惜字如金、字字珠玑而深奥、晦涩和难懂,又比“圣经”的“注释版”更深刻和全面、更通俗和生动、更接近开发者遇到的实践场景,更具指导性。本书兼收并蓄、博采众长,也许是设计模式领域里的下一个里程碑之作。 全......一起来看看 《设计模式之禅》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器