问题及代码:
/*copyright 计算机与控制工程学院
完成日期:2016年5月6日
作者:马艳艳
问题描述:先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
输入描述:无
输出描述:坐标值 ;
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(double x=0,double y=0); //构造函数
void setPoint(double,double); //设置坐标值
double getX( ) const {return x;} //x坐标
double getY( ) const {return y;} //y坐标
void show();
private: //私有数据成员
double x,y;
};
Point::Point(double a,double b)
{
x=a;
y=b;
}
//设置x和y的坐标值
void Point::setPoint(double a,double b)
{
x=a;
y=b;
}
void Point::show()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
int main( )
{
Point p(3.5,6.4);
p.show();
p.setPoint(8.5,6.8);
p.show();
return 0;
}
运行结果:
知识点总结:
在main函数里,定义类的对象时可进行初始化;