/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:隋 鑫
* 完成日期: 2013年 5 月 21 日
* 版本号: v1.0
* 输入描述:无
* 问题描述:
* 程序输出:
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(double x=0,double y=0);
void setPoint(double,double);
double getX( )
{
return x;
}
double getY( )
{
return y;
}
friend ostream & operator<<(ostream &, Point &);//重载运算符“<<”
protected: //受保护成员
double x,y;
};
//Point的构造函数
Point::Point(double a,double b)
{
x=a;
y=b;
}
//设置x和y的坐标值
void Point::setPoint(double a,double b)
{
x=a;
y=b;
}
ostream & operator<<(ostream &output, Point &p)
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
int main( )
{
Point p(4.2,6.9);
cout<<"x="<<p.getX( )<<",y="<<p.getY( )<<endl;
p.setPoint(8.5,6.3);
cout<<"p:"<<p<<endl;
return 0;
}
运行结果: