c++ - 在 C++ 中创建具有 2 个 double 值的类

标签 c++ class object

在 C++ 中,我试图创建一个包含两个 double 值的 Point2D 类。所有数据成员和函数都应该是公开的。

对于公共(public)成员应该有

  • 双倍
  • 双y

对于构造函数

  • 默认构造函数应将 x 和 y 初始化为 0.0

  • Point2D(双 in_x,双 in_y)

    • 将 x 和 y 设置为 in_x 和 in_y

对于非成员函数

  • void GetResult(Point2D p1, Point2D p2)

    • 打印两者的 x 和 y 值

这是我目前的代码,有人可以指出我的错误吗?

点2D.h

#ifndef POINT2D_H
#define POINT2D_H

class Point2D
{
public:
    double x;
    double y;

Point2D();
Point2D(double,double);

};

void GetResult(Point2D, Point2D);

#endif

点2D.cpp

#include "Point2D.h"
#include <iostream>
using namespace std;

Point2D::Point2D()
{
    x = 0.0;
    y = 0.0;
}

Point2D::P1(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

Point2D::P2(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

void GetResult(Point2D P1, Point2D P2)
{
    cout << P1.x << " " << P1.y << endl;
    cout << P2.x << " " << P2.y << endl;
}

测试检查点1.cpp

#include <iostream>
#include "Point2D.h"
using namespace std;

int main()
{
    Point2D Point1;
    Point1.x = 1.0;
    Point1.y= 2.0;

    Point2D Point2;
    Point2.x= 1.0;
    Point1.y= 2.0;

    GetResult(Point1, Point2);
}

最佳答案

你很接近,但很明显你对重载构造函数和声明你的类的实例有一点误解。对于初学者,您不需要以下功能:

Point2D::P1(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

Point2D::P2(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

您只需要为您的 Point2D 类构造一个带有两个 double 值的构造函数,例如

Point2D::Point2D(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

然后在 main() 中,您需要声明并初始化 Point2D 类的默认构造两个实例,为 提供所需的值>xy 在调用 GetResult 之前,例如

#include <iostream>
#include "Point2D.h"
using namespace std;

int main()
{
    Point2D Point1 (1.0, 2.0);
    Point2D Point2 (1.0, 2.0);

    GetResult(Point1, Point2);
}

(注意:您可以提供一个初始化列表来允许类成员的初始化,参见Constructors and member initializer lists。您可以为您的构造函数提供一个初始化列表,例如 Point2D() : x(0), y(0) {}; 和重载 Point2D(double, double);。您的构造函数定义将只是 Point2D::Point2D(double in_x, double in_y) : x(in_x), y(in_y) {} 并且编译器会将 x, y 初始化为 0, 0 如果使用 Point2D Point1; 创建或将 x, y 设置为 Point2D Point2 (1.0, 2.0);)

您在 Point2D.h 的内容周围添加了 Header Guards 做得非常好,以防止在包含在多个文件中时出现多重包含。 Point2D 的完整头文件和源文件可能是:

#ifndef POINT2D_H
#define POINT2D_H

class Point2D
{
public:
    double x;
    double y;

    Point2D();
    Point2D(double,double);

};

void GetResult(Point2D, Point2D);

#endif

#include "Point2D.h"
#include <iostream>
using namespace std;

Point2D::Point2D()
{
    x = 0.0;
    y = 0.0;
}

Point2D::Point2D(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

void GetResult(Point2D P1, Point2D P2)
{
    cout << P1.x << " " << P1.y << endl;
    cout << P2.x << " " << P2.y << endl;
}

示例使用/输出

编译运行会得到:

$ ./bin/TestCheckPoint1
1 2
1 2

注意:根本不需要 using namespace std;main() 中,你真的不应该包括整个标准 namespace 的任何地方。只需删除这两个调用并将 std:: 添加到您的两个 cout 调用和两个对 endl 的调用(或者只使用 '\n ' 而不是 std::endl;)。参见 Why is “using namespace std;” considered bad practice?

而是简单地使用:

void GetResult(Point2D P1, Point2D P2)
{
    std::cout << P1.x << " " << P1.y << '\n';
    std::cout << P2.x << " " << P2.y << '\n';
}

检查一下,如果您还有其他问题,请告诉我。

关于c++ - 在 C++ 中创建具有 2 个 double 值的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58776319/

相关文章:

python - Django:将 session 数据存储在类变量中是否合理?

javascript - JavaScript中如何将值存储在对象中?

java - CORBA COS 命名服务存储

c++ - 将源代码注释加载到字符串对象中

C++:将变量从main外部的函数传递到main内部的函数调用

php - 检测服务器过载以限制 mysql 查询

c++ - Thrift C++ Trouble compiling provided 教程 - 无法在 TProcessor 上实例化抽象类

excel - 对于 Excel VBA 中的每个类属性

java - Java 中方法参数内的类定义?

javascript - 使用 Vue3 对产品进行分组并显示产品数组中的数量