关于友元函数(成员友元函数, 普通友元函数)

#include<iostream>
#include<math.h>
using namespace std;
const double PI = 3.141593;
class Circle;
// class Calculate
// {
// public:
// double distance(Circle &p1, Circle &p2);
// };


class Circle
{
private:
double x, y, r;
public:
// 构造函数
Circle(double xx = 0, double yy = 0, double rr = 0)
{
x = xx; y = yy; r = rr;
}


// 显示
void display()
{
cout << "\tCenter point: (" << x << ", " << y << ")" << endl;
cout << "\tRadius: " << r << endl;
cout << "\tArea: " << this->area() << endl;
cout << "\tCirculars: " << this->circular() << endl;
}


// 面积
double area()
{
return PI * r * r;
}


// 周长
double circular()
{
return 2 * PI * r;
}

// 普通友元函数
friend double dist(Circle &p1, Circle &p2);
// 成员友元函数
// friend double Calculate::distance(Circle &p1, Circle &p2);
};
// 忘记了这个分号,结果报错:
//'Circle' followed by a double is illegal.(did you forgot a ';'?)
// double Calculate::distance(Circle &p1, Circle &p2)
// {
// return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
// }
double dist(Circle &p1, Circle &p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}


void main()
{
Circle c1(1, 1, 1), c2(3, 4, 5);
// Calculate cal; // 必须声明一个Calculate 对象
cout << "Circle 1: " << endl;
c1.display();
cout << endl << endl;


cout << "Circle 2: " << endl;
c2.display();
// cout << "The distance is: " << cal.distance(c1, c2) << endl;
cout << "The distance is: " << dist(c1, c2) << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值