Last active
May 25, 2020 22:03
-
-
Save douglasrizzo/b1375881d1afb70cd1fe76fefa47d3f0 to your computer and use it in GitHub Desktop.
Structs and classes in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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