//---------a.h------------
#ifndef __A_H
#define __A_H
#include <iostream>
//namespace abc
//{
// void myname();
//}
class PPoint
{
private:
double x,y;
public:
PPoint(double ix,double iy);
double xOffset();
double yOffset();
double angle();
double radius();
};
#endif
//---------a.cpp------------
#include "a.h"
#include <cmath>
//using namespace std;
//
//void abc::myname()
//{
// std::cout<<"abc-myname"<<endl;
//}
PPoint::PPoint(double ix,double iy)
{
x=ix;
y=iy;
}
double PPoint::xOffset()
{
return x;
}
double PPoint::yOffset()
{
return y;
}
//直角坐标转化成极坐标
double PPoint::angle()
{
return ((180.0/3.14159)*atan2(y,x));
}
double PPoint::radius()
{
return (sqrt(x*x+y*y));
}
//---------main.cpp------------
#include <iostream>
#include <string>
#include "a.h"
using namespace std;
int main(int argc,char **argv)
{
for(double x,y;cout<<"Enter x and y,to change an angle and a radius :"&& cin>>x>>y;)
{
PPoint p(x,y);
std::cout<<"the angle is :"<<p.angle()<<" the radius is :"<<p.radius()<<endl;
}
return 0;
}
Enter x and y,to change an angle and a radius :1 1
the angle is :45 the radius is :1.41421
Enter x and y,to change an angle and a radius :
使用构造函数来创建对象,使得对象初始化能够一次性解决。程序员完全可以在构造函数中严格检查对象的初值条件,本程序中的构造函数没有正确性校验,结果的正确性与否完全依赖于输入数据的合法性。
1