In this exercise I'm using Visual Studio 2022 and ISO C11 Standard.
Chapter 27 // Exercise 15
Simulate single inheritance in C. let each "base class" contain a pointer to an array of pointers to functions (to simulate virtual functions as freestanding functions taking a pointer to a "base class" object as their first argument); see section 27.2.3. Implement "derivation" by making the "base class" the type of the first member of the derived class. For each class, initialise the array of "virtual functions" appropriately. To test the ideas, implement a version of "the old shape example" with the base and derived draw() just printing out the name of their class. Use only language features and library facilities available in standard C.
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2027/Exercises/Exercise%2015.c
Jesus christ, I had to get ChatGPT in for this one because I had no idea how start. What a load of faffing about. I used to think inheritance in C++ was confusing, well get a load of inheritance in C! It all kind of seems a bit pointless in C.
I didn't go too far into this exercise, literally just creating the draw() functions for a shape and a circle. Shape can also be created, it's not abstract.
No comments:
Post a Comment