Vishal Naidu wrote:
I'm a college student in my second year.
Congratulations!
Is it really possible to write object oriented code in C?
Yes.
And, if yes, how do we achieve
[data] abstraction,
[run-time] polymorhism,
[inheritance],
etc. in C?
There are two kinds of abstraction:
1.) algorithmic abstraction and
2.) data abstraction.
Algorithmic abstraction refers to language features
that allow you to create subprograms
(subroutines, procedures, functions, etc.)
which are independent, reusable modules.
The original Dartmouth BASIC
did not support algorithmic abstraction
but most other high level computer programming languages do.
Data abstraction refers to language features
that allow you to create new, User Defined Types (UDTs).
Standard Fortran 77 does not support data abstraction
but most modern high level computer programming languages do.
The C computer programming language allows you to create UDTs
by *encapsulating* other data types in a struct
but C does *not* support data hiding -- private data members.
Beware -- for some object oriented programmers,
encapsulation implies data hiding.
Data hiding does *not* mean encryption.
There is no way to hide the data representation
which a determined programmer can't expose
with just a few simple tests.
Data hiding is intended only to prevent programmers
from accidently accessing the actual data representation directly.
The C computer programming language does *not* support inheritance.
An explicit caste is required of a pointer to a derived type
to a pointer of the base type is required.
C programmers have been using run-time polymorphism
as long as there have been C programmers.
The standard FILE type, for example, is a polymorphic type.