美文网首页
C++友元初步

C++友元初步

作者: 编程小世界 | 来源:发表于2019-06-22 18:47 被阅读0次

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

**

/全局函数作为友元 /

**

include <iostream>

include <cmath>

using namespace std;

class Point{

public:

Point(doublex,doubley){    _x = x;    _y = y;}voidgetFormatxy();frienddoubledistance(Point &a, Point &b);

private:

double_x, _y;

};

void Point::getFormatxy(){

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

}

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

doubledx = a._x - b._x;doubledy = a._y - b._y;returnsqrt(dx*dx + dy*dy);

}

int main()

{

Pointp1(3.0,4.0),p2(6.0,8.0);p1.getFormatxy();p2.getFormatxy();doubled = distance(p1, p2);cout<<"distance is "<

}

**

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

**

include <iostream>

include <cmath>

using namespace std;

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

class ManagerPoint{

public:

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

};

class Point{

public:

Point(doublex,doubley){    _x = x;    _y = y;}voidgetFormatxy();frienddoubleManagerPoint::distance(Point &a, Point &b);

private:

double_x, _y;

};

void Point::getFormatxy(){

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

}

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

doubledx = a._x - b._x;doubledy = a._y - b._y;returnsqrt(dx*dx +dy*dy);

}

int main()

{

Pointp1(3.0,4.0),p2(6.0,8.0);p1.getFormatxy();p2.getFormatxy();ManagerPoint mp;floatd = mp.distance(p1, p2);cout<<"distance is"<

}

**

/友元类 /

**

include <iostream>

include <cmath>

using namespace std;

class Point{

public:

friendclassManagerPoint;Point(doublex,doubley){    _x = x;    _y = y;}voidgetFormatxy();

private:

double_x, _y;

};

void Point::getFormatxy(){

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

}

class ManagerPoint{

public:

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

};

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

doubledx = a._x - b._x;doubledy = a._y - b._y;returnsqrt(dx*dx + dy*dy);

}

int main()

{

Pointp1(3.0,4.0),p2(6.0,8.0);p1.getFormatxy();p2.getFormatxy();ManagerPoint mp;floatd = mp.distance(p1, p2);cout<<"distance is"<< d<

}

**

/友元小结 /

**

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

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

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

//友元利弊:

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

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

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

include <iostream>

using namespace std;

class Time{

public:

friendclassWatch;Time(inthour,intmin,intsec){    m_iHour =hour;    m_iMinute = min;    m_iSecond = sec;}

private:

intm_iHour;intm_iMinute;intm_iSecond;

};

class Watch{

public:

Watch(Time t): m_tTime(t){}voiddisplay(){cout<

public:

Timem_tTime;

};

int main()

{

Timet(6,30,20);Watchw(t);w.display();return0;

}

看我主页简介免费C++学习资源,视频教程、职业规划、面试详解、学习路线、开发工具

每晚8点直播讲解C++编程技术。非常感谢大家的关注

相关文章

  • C++友元初步

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

  • c++ 友元

    在c++中,通过关键字friend声明为友元。友元可以范围与其有好友关系的类中私有成员。友元包括友元函数和友元类。...

  • C++语言基础(03)

    1.内存分配 C/C++ 内存分区: java 创建对象 2.常函数 3.友元函数和友元类 友元函数 友元类 4....

  • C++中的友元

    友元的介绍 什么是友元?友元是C++中的一种关系友元关系发生在函数与类之间或者类与类之间友元关系是单项的,不能传递...

  • C++友元

    friend类: 如果class A想让class B访问它的成员,只需要在class A中将class B声明称...

  • C++友元

    我承认你是我的朋友,你才是,当有一天,我觉得你有背叛我,除了你的名,你就再也不会进入我的内心。

  • C++ 友元

    ​ 采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一 般定义为公有的,依此提供...

  • C++ 友元

    小项备注时间2020 年 3 月 28 日修改时间2020 年 3 月 28 日城市北京市房山区天气晴心情你猜 0...

  • C++ 友元

    友元 待续

  • 10-C++远征之模板篇-学习笔记

    C++远征之模板篇 将会学到的内容: 模板函数 & 模板类 -> 标准模板类 友元函数 & 友元类 静态数据成员 ...

网友评论

      本文标题:C++友元初步

      本文链接:https://www.haomeiwen.com/subject/zxbvqctx.html