码迷,mamicode.com
首页 > 其他好文 > 详细

【练习】友元类和友元函数

时间:2017-11-08 23:34:05      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:友元类   iostream   blog   poi   1.0   main   ons   4.0   enter   

#include <iostream>
using namespace std;
class Circle;
class Point
{
private:
    float x;
    float y;
public:
    Point();
    Point(float a,float b);
    friend class Circle;
    friend const Point middle(const Point &u,const Point &v);
    void print()const;
};
Point::Point():x(0.0),y(0.0)
{
    cout << "Defalt constraction is called " << endl;
}
Point::Point(float a,float b):x(a),y(b){}
void Point::print()const
{
    cout << " x: " << x << " y " << y <<endl;
}
const Point middle (const Point &u,const Point &v )
{
    return Point ((u.x+v.x)/2.0,(u.y+v.y)/2.0);
}
class Circle
{
private:
    float radius;
    Point center;
public:
    Circle();
    Circle& move(float a,float b);
    void print()const;
};
Circle::Circle():radius(0.0)
{
    center.x = 0.0;
    center.y = 0.0;
}

Circle&Circle::move(float a,float b)
{
    center.x += a;
    center.y += b;
    return *this;
}
void Circle::print()const
{
    center.print();
    cout << "radius: " << radius <<endl;
}
int main()
{
    Point p1(1.0,2.0);
    Point p2(3.0,4.0);
    p1.print();
    p2.print();
    cout << "the diffrence between the two point is:" << endl;
    middle(p1,p2).print();
    Circle c;
    c.print();
    cout << "move the center:"<< endl;
    c.move(10.0,10.0);
    c.print();
    cout << "Hello World!" << endl;
    return 0;
}

 

【练习】友元类和友元函数

标签:友元类   iostream   blog   poi   1.0   main   ons   4.0   enter   

原文地址:http://www.cnblogs.com/666fengmo666/p/7806775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!