class Point { public: Point(); Point(double x, double y); double getX(); double getY(); private: double mX,mY; };
时间: 2023-11-21 14:06:05 浏览: 66
这是一个 C++ 的类定义,名为 Point,有两个私有变量 mX 和 mY,分别表示点的 x 和 y 坐标。类中定义了两个公有函数 getX() 和 getY(),用于获取点的坐标值。还有一个构造函数 Point(double x, double y),用于初始化点的坐标值。另外还有一个默认构造函数 Point(),如果没有传入坐标值则会默认将 mX 和 mY 初始化为 0。
相关问题
#include<iostream> using namespace std; class CPoint { private: double x,y; public : CPoint (double i,double j); double Getx(); double Gety(); double Setpoint(double i,double j); }; class CShape { public: CPoint point; virtual double Area() { return 0.0; } }; class CCircle:public CShape { public: float r; CCircle(CPoint p1,float r=2):r(r){} double Area() { return 3.14*r*r; } }; class CRect:public CShape { public: double x; double y; CRect(CPoint p1,double x=2,double y=1):x(x),y(y) {} double Area() { return x*y; } }; class CEll:public CShape { double x; double y; CEll(CPoint p1,double x=2,double y=1); double Area() { return 3.14*x*y; } }; int main() { return 0; }有什么问题?怎么改正?
这段代码存在一个问题,即CEll类的构造函数是私有的,无法在main函数中创建CEll对象。需要将CEll类的构造函数改为公有的。
修改后的代码如下:
```
#include<iostream>
using namespace std;
class CPoint {
private:
double x,y;
public :
CPoint (double i,double j);
double Getx();
double Gety();
double Setpoint(double i,double j);
};
class CShape {
public:
CPoint point;
virtual double Area()
{
return 0.0;
}
};
class CCircle:public CShape {
public:
float r;
CCircle(CPoint p1,float r=2):r(r){}
double Area()
{
return 3.14*r*r;
}
};
class CRect:public CShape {
public:
double x;
double y;
CRect(CPoint p1,double x=2,double y=1):x(x),y(y) {}
double Area()
{
return x*y;
}
};
class CEll:public CShape {
public:
double x;
double y;
CEll(CPoint p1,double x=2,double y=1):x(x),y(y){}
double Area()
{
return 3.14*x*y;
}
};
int main() {
return 0;
}
```
这段代码什么意思class Point { public: Point(double x = 0, double y = 0): x(x), y(y) {} double getX() const { return x; } double getY() const { return y; } private: double x, y; };
这段代码定义了一个名为 Point 的类,表示一个二维平面上的点。其中包含了两个私有成员变量x和y,表示点的横坐标和纵坐标。还有一个构造函数,可以用给定的横纵坐标值初始化点的位置,默认值为0。类中还有两个公有成员函数,getX()和getY(),分别用于获取点的横坐标和纵坐标。这个类可以用于在程序中表示一个二维平面上的点,并且可以获取该点的坐标值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)