Created
February 3, 2020 13:53
-
-
Save edubart/8f55472f60c45e762648076775aafefb to your computer and use it in GitHub Desktop.
inheretance_example.cpp
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 <stdint.h> | |
#include <stdio.h> | |
struct Shape { | |
Shape(int64_t x, int64_t y): x(x), y(y) {} | |
virtual int64_t area() {return 0;} | |
int64_t x, y; | |
}; | |
struct Rectangle : public Shape { | |
Rectangle(int64_t x, int64_t y, int64_t w, int64_t h): Shape(x, y), w(w), h(h) {} | |
virtual int64_t area() {return w*h;} | |
int64_t w, h; | |
}; | |
struct Circle : public Shape { | |
Circle(int64_t x, int64_t y, int64_t r): Shape(x, y), r(r) {} | |
virtual int64_t area() {return 3*r*r;} | |
int64_t r; | |
}; | |
int main(int argc, char **argv) { | |
int64_t sum = 0; | |
Rectangle *r = new Rectangle(1,2,3,4); | |
Circle *c = new Circle(5,6,7); | |
Shape* sr = static_cast<Shape*>(r); | |
Shape* sc = static_cast<Shape*>(c); | |
for(int64_t i=1;i<=100000000;++i) { | |
sum += sr->area() + sc->area(); | |
} | |
delete r; | |
delete c; | |
printf("%ld\n", sum); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment