标签:友元类 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