C++ dot class

First define a point class, the class name is point, define its coordinates as a private member, and define five public member functions to complete the input, output, return x coordinate, return y coordinate and return z coordinate of the point. Define an object of this class in the main program, so that the coordinates can be input, the coordinates can be output, and the distance to the origin can be output.

#include <iostream>
#include<math.h>
using namespace std;
class Point{
    private:
      double x;
      double y;
      double z;
    public:
      Point(double xv=0,double yv=0,double zv=0);//有参构造
      Point(const Point &p);         //拷贝构造
      ~Point();                      //析构函数
      void show() const;             //显示Point信息
      double area()const;            //面积=0
      void setX(double xv);          //设置X坐标
      void setY(double yv);          //设置Y坐标
       void setZ(double zv);          //设置Z坐标
      double getX() const;           //获取X坐标
      double getY() const;           //获取Y坐标
       double getZ() const;           //获取Z坐标
};
//有参构造
Point::Point(double xv,double yv,double zv){
    x=xv;
    y=yv;
    z=zv;
   cout<<"Point Constructor run"<<endl;
   cout<<"主程序设置x,y,z的坐标分别是"<<x<<","<<y<<","<<z<<endl;

}
//拷贝构造
Point::Point(const Point &p){
    x=p.x;
    y=p.y;
    z=p.z;
   cout<<"Point CopyConstructor run"<<endl;
}
//析构函数
Point::~Point(){
    cout<<"Point Destructor run"<<endl;
}
//显示Point
void Point::show() const{
    cout<<"输出x,y,z坐标\n"<<endl;
    cout<<"("<<x<<","<<y<<","<<z<<")";
}
//设置X坐标
void Point::setX(double xv){
    x=xv;
}
//设置Y坐标
void Point::setY(double yv){
    y=yv;
}
//设置Z坐标
void Point::setZ(double zv){
    z=zv;
}
//面积函数
double Point::area() const{
    return 0;
}
//获取X坐标
double Point::getX() const{
    return x;
}
//获取Y坐标
double Point::getY() const{
    return y;
}
//获取Z坐标
double Point::getZ() const{
    return z;
}
int main(){
Point point1(2.0,3.0,4.0),point2(3.0,4.0,5.0);
point1.show();
point2.show();
 double  dis1,dis2;
 dis1=sqrt(2.0*2.0+3.0*3.0+4.0*4.0);
 dis2=sqrt(3.0*3.0+4.0*4.0+5.0*5.0);
 cout<<"\npoint1距原点的距离"<<dis1<<endl;
 cout<<"\npoint2距原点的距离"<<dis2<<endl;
 return 0;

    }

Guess you like

Origin blog.csdn.net/y0205yang/article/details/130208204