-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_covariant.cpp
More file actions
32 lines (30 loc) · 869 Bytes
/
Copy pathtest_covariant.cpp
File metadata and controls
32 lines (30 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class IVec3 {
public:
virtual ~IVec3() = default;
virtual float x() const = 0;
virtual float y() const = 0;
virtual float z() const = 0;
virtual IVec3* add(const IVec3& rhs) const = 0;
virtual IVec3* clone() const = 0;
};
class Vec3 final : public IVec3 {
float m_x, m_y, m_z;
public:
Vec3() : m_x(0), m_y(0), m_z(0) {}
Vec3(float x, float y, float z) : m_x(x), m_y(y), m_z(z) {}
float x() const override { return m_x; }
float y() const override { return m_y; }
float z() const override { return m_z; }
Vec3* add(const IVec3& rhs) const override {
return new Vec3(m_x + rhs.x(), m_y + rhs.y(), m_z + rhs.z());
}
Vec3* clone() const override {
return new Vec3(m_x, m_y, m_z);
}
};
int main() {
Vec3 a(1,2,3), b(4,5,6);
IVec3* result = a.add(b);
delete result;
return 0;
}