class Point
{
friend double Distance(const Point &p1, const Point &p2); //类体中声明
public:
Point(int x, int y):x_(x), y_(y)
{};
private:
int x_;
int y_;
};
//类体外定义友元函数
double Distance(const Point &p1, const Point &p2)
{
double dx = p1.x_ - p2.x_; //直接访问类的私有成员
double dy = p1.y_ - p2.y_;
return sqrt(dx*dx+dy*dy);
}
int main()
{
Point p1(3,4);
Point P2(6,8);
cout << Distance(p1,p2) << endl;
return 0;
}
class Television //电视类
{
friend class TeleController;//友元类声明
public:
Television(int volume, int chanel): volume_(volume), chanel_(chanel){}
private:
int volume_; //音量
int chanel_; //频道
};
class TeleController //遥控器类
{
public: //类中的每个成员函数都是电视类的友元函数,都可以访问电视类的私有数据成员
void VolumeUp(Television &tv)
{
tv.volume_ += 1; //音量加1
}
void VolumeDown(Television &tv)
{
tv.volume_ -= 1;
}
void ChanelUp(Television &tv)
{
tv.chanel_ += 1; //频道加1
}
void ChanelDown(Television &tv)
{
tv.chanel_ -= 1; //频道减1
}
};
int main()
{
Television tv(1,1);
TeleController tc;
tc.ChanelUp(tv);
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有