- Hi To all: My question is that what techniques were used for Inheritance, Encapsulation and polymorphism in C++ before the invention of OOP. Please explain and if any link share it.

- 1
- 5
-
Possible duplicate of [How can I simulate OO-style polymorphism in C?](http://stackoverflow.com/questions/524033/how-can-i-simulate-oo-style-polymorphism-in-c) – Matthias Steinbauer Mar 29 '16 at 10:39
-
1"In C++ before the invention of OOP" - isn't that a contradiction? – Frank Puffer Mar 29 '16 at 10:44
1 Answers
Ignoring the question of what you mean by 'in C++ before OOP', then with the C language you'd be looking at
void pointers - the C language allows you to address memory directly, storing addresses in variables with a pointer type. Most pointers point to a known type (e.g.
char *
is a pointer to a character) but a special typevoid *
is a pointer to a raw piece of memory. Many libraries hid internal structures in void pointers, sometimes called 'Handles'.function pointers - as with data pointers, a pointer could hold the address of a function in memory. This allowed for assigning different behaviour by swapping functions in and out of variables.
structs - the primary mechanism for grouping data. Combining structs with function pointers gets a rudimentary form of 'class', albeit without the magic of inheritance or polymorphism. By hiding data behind a
void *
within a struct you get a basic form of encapsulation.preprocessor - lots of magic could be made to happen in the preprocessor by turning one type into another type or unpacking things on the fly. Void pointers would often be accessed using preprocessor macros which unpacked them safely.
Inheritance was not generally an aspect of the C programming idiom so you rarely come across it in C programs. Where I've seen something akin to it it's generally accomplished by using some kind of void pointer hackery where the first field of a struct is a void pointer to a parent and initialisation functions are used to copy function pointers into a makeshift v-table. However, it's cumbersome and fragile and the C language was not really designed to support object-orientation natively - you can do it but the pipework is very visible.
Getting back to the 'in C++ before oop' part - it's worth remembering that there was no C++ before oop. OO concepts, and OO languages, have been around for much longer than C++. C++ isn't even a pure OO language - it's a multi-paradigm language which supports OO.

- 6,174
- 1
- 25
- 35