Last active
February 1, 2021 17:30
-
-
Save menangen/63a4ad485f62a561e9175edae3ea56ec to your computer and use it in GitHub Desktop.
C++ struct Inheritance
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 Perlin { | |
float freq; | |
explicit | |
Perlin (float f = 1.0) { | |
freq = f; | |
} | |
float getValue() { | |
std::cout << "Perlin return " << this -> freq << std::endl; | |
return this -> freq; | |
}; | |
}; | |
struct Module { | |
enum ModuleType { Perlin, Add }; | |
ModuleType type; | |
void* modules[255]; | |
uint8_t index = 0; | |
explicit | |
Module (struct Perlin* noise) { | |
this -> type = Perlin; | |
this -> modules[0] = (void *) noise; | |
} | |
explicit | |
Module (struct Module* module, struct Module* module2) { | |
this -> type = Add; | |
this -> modules[0] = (void *) module; | |
this -> modules[1] = (void *) module2; | |
} | |
float getValue() const { | |
switch (type) { | |
case Perlin: { | |
std::cout << "Case: Perlin" << std::endl; | |
auto Perlin = (struct Perlin *) modules[0]; | |
return Perlin->getValue(); | |
} | |
case Add: { | |
std::cout << "Case: Add" << std::endl; | |
auto m1 = (struct Module *) modules[0]; | |
auto m2 = (struct Module *) modules[1]; | |
auto a = m1->getValue(); | |
std::cout << "a = " << a << std::endl; | |
auto b = m2->getValue(); | |
std::cout << "b = " << b << std::endl; | |
return a + b; | |
} | |
} | |
}; | |
}; | |
int main() { | |
Perlin n0(5); | |
Perlin n1(3); | |
Module per1 (&n0); | |
Module per2 (&n1); | |
Module add (&per1, &per2); | |
std::cout << add.getValue() << std::endl; | |
std::cout << "Hello, Noise!" << std::endl; | |
return 0; | |
} |
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
// | |
// main.cpp | |
// Created by menangen on 22.10.2020. | |
// | |
#include <iostream> | |
struct Noise { | |
float value; | |
float getValue(float x) { | |
return x * value; | |
}; | |
void addNoise(Noise other) { | |
value += other.value; | |
}; | |
void powerTo(float to) { value = pow(value, to); } | |
}; | |
struct Perlin: Noise { | |
Perlin( float a = 1, float b = 2) { value = a + b; }; | |
}; | |
struct Billow: Noise { | |
Billow( float a = 2, float b = 3, float c = 5) { value = a + b + c; }; | |
}; | |
int main(int argc, const char * argv[]) { | |
Perlin p = Perlin(2); | |
Billow n; | |
std::cout << p.value << "\n"; | |
std::cout << n.value << "\n"; | |
p.powerTo(2.0); | |
p.addNoise(n); | |
std::cout | |
<< ".getValue(2) = [ " << p.getValue(2) << " ]\n\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment