问题及代码:
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:宋健
* 完成日期:2015年 5月 26日
* 版 本 号:v1.0
*
* 问题描述: 建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试
* 程序输入:
* 程序输出:
*/
#include <iostream>
using namespace std;
class Point
{
protected:
double x,y;
public:
Point() {};
Point(double ,double);
double getx();
double gety();
friend ostream &operator <<(ostream &output,const Point &p);
};
Point::Point(double m,double n)
{x=m;
y=n;
}
double Point::getx()
{
return x;
}
double Point::gety()
{
return y;
}
ostream &operator <<(ostream &output,const Point &p)
{
output<<"("<<p.x<<","<<p.y<<")"<<endl;
return output;
}
int main()
{ Point p(2.3,8.4);
cout<<"x="<<p.getx( )<<",y="<<p.gety( )<<endl;
cout<<"p:"<<p<<endl;
return 0;
}
运行结果:
知识点总结:
就像建房子先磊地基