我希望能够创建一个具有 3 个浮点数 (x,y,z) 的类型。我试过了:
typedef struct
{
float x;
float y;
float z;
} Vertex;
Run Code Online (Sandbox Code Playgroud)
但这没有用。
这是否必须在可以看到的地方声明main?我将如何为我制作的类型创建 getter 方法和其他方法?
我将如何在 C++ 中做到这一点。请参阅 main() 以获取示例用法。注意这还没有被编译或测试。
#include <iostream>
class Vertex
{
public:
// Construction
Vertex(float x,float y, float z) : x_(x), y_(y), z_(z) {}
// Getters
float getX() const {return x_;}
float getY() const {return y_;}
float getZ() const {return z_;}
// Setters
void setX(float val) {x_ = val;}
void setY(float val) {y_ = val;}
void setZ(float val) {z_ = val;}
private:
float x_;
float y_;
float z_;
};
int main()
{
Vertex v(6.0f,7.2f,3.3f);
v.setZ(7.7f);
std::cout < "vertex components are " << v.getX() << ',' << v.getY() << ',' << v.getZ() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20894 次 |
| 最近记录: |