Skip to content

Instantly share code, notes, and snippets.

@douglasrizzo
Last active May 25, 2020 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasrizzo/b1375881d1afb70cd1fe76fefa47d3f0 to your computer and use it in GitHub Desktop.
Save douglasrizzo/b1375881d1afb70cd1fe76fefa47d3f0 to your computer and use it in GitHub Desktop.
Structs and classes in C++
#include <stdio.h>
struct Vec2 {
float x, y;
};
int main() {
struct Vec2 v;
v.x = 1;
v.y = 2;
printf("%f %f", v.x, v.y);
}
class Vec2 {
public:
float x, y;
void add(Vec2 other) {
x += other.x;
y += other.y;
}
};
class Vec3 : public Vec2 {
public:
float z;
void add(Vec3 other) {
Vec2::add(other);
z += other.z;
}
};
class Vec2 {
public:
float x, y;
void add(Vec2 other) {
x += other.x;
y += other.y;
}
};
struct Vec3 : public Vec2 {
float z;
void add(Vec3 other) {
Vec2::add(other);
z += other.z;
}
};
#include <iostream>
#include <struct_struct.hpp>
// #include <class_class.hpp>
// #include <class_struct.hpp>
// #include <struct_class.hpp>
int main() {
Vec3 v1, v2;
v1.x = 1;
v1.y = 2;
v1.z = 3;
v2.x = 3;
v2.y = 2;
v2.z = 1;
v1.add(v2);
std::cout << v1.x << " " << v1.y << " " << v1.z << std::endl;
}
#include <iostream>
struct Vec2 {
float x, y;
void add(Vec2 other) {
x += other.x;
y += other.y;
}
private:
bool inverted;
};
int main(void) {
Vec2 v;
std::cout << v.inverted;
}
struct Vec2 {
float x, y;
void add(Vec2 other) {
x += other.x;
y += other.y;
}
};
class Vec3 : public Vec2 {
public:
float z;
void add(Vec3 other) {
Vec2::add(other);
z += other.z;
}
};
struct Vec2 {
float x, y;
void add(Vec2 other) {
x += other.x;
y += other.y;
}
};
struct Vec3 : public Vec2 {
float z;
void add(Vec3 other) {
Vec2::add(other);
z += other.z;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment