定义一个Point类来处理三文点
定义一个Point类来处理三文点points(x,y,z).该类有一默认的constructor,一copy constructor, 一negate()成员函数将point的x,y和z值各乘-1, 一norm()成员函数返回该点到原点(0,0,0)的距离,一个print()成员函数显示x,y,和z的值。
#include<iostream>
#include<cmath>
using namespace std;
class Point{
private:
double x,y,z;
double distance;
public:
Point(double newX,double newY,double newZ){
x=newX;y=newY;z=newZ;
distance=sqrt(x*x+y*y+z*z);
}
Point(Point &p){
x=p.x;y=p.y;z=p.z;
distance=p.distance;
}
void negate(){
x*=-1;y*=-1;z*=-1;
}
double norm(){
return distance;
}
void print(){
cout<<"x="<<x<<endl<<"y="<<y<<endl<<"z="<<z<<endl;
}
};
void main(){
Point dot(5,6,9);
cout<<"the distance is: "<<dot.norm()<<endl;
Point dot1(dot);
cout<<"the distance is: "<<dot1.norm()<<endl;
return;
}
用构造函数初始化坐标.可以根据自己的需要编写主函数.572