Interview Questions

Is there a good way of simulating OOP-style inheritance

C Interview Questions and Answers


(Continued from previous question...)

Is there a good way of simulating OOP-style inheritance

Q: Is there a good way of simulating OOP-style inheritance, or other OOP features, in C?

A: It's straightforward to implement simple ``methods'' by placing function pointers in structures. You can make various clumsy, brute-force attempts at inheritance using the preprocessor or by having structures contain ``base types'' as initial subsets, but it won't be perfect. There's obviously no operator overloading, and overriding (i.e. of ``methods'' in ``derived classes'') would have to be done by hand.

Obviously, if you need ``real'' OOP, you'll want to use a language that supports it, such as C++.

(Continued on next question...)

Other Interview Questions