Object orientation.....

V

Vishal Naidu

i m a college student in my second year.....
my queston is..
is it really possible to write object oriented code in C ?
and if yes how do we achieve abstration, polymorhism , hierarchy etc.
in C
 
R

Richard Heathfield

Vishal Naidu said:
i m a college student in my second year.....
my queston is..
is it really possible to write object oriented code in C ?

Yes. Similarly, it is really possible to build a life-sized replica of the
Statue of Liberty using nothing but matchsticks and chewing gum.
and if yes how do we achieve abstration, polymorhism , hierarchy etc.
in C

system("g++ -o foo foo.cpp");


I don't wish to be 100% negative, so I should point out that you can get
data hiding and encapsulation fairly painlessly by using opaque types.
 
A

ajm

Vishal,

It rather depends on precisely what you mean by "object oriented" but
in the early days of C there was a well defined notion of what was
meant by "object based" programming which has some of the notions of OO
(e.g., data hiding) but could ultimately be subverted by the programmer
if they so wished.

The basic idea was to construct a struct (the data members) and a
function pointer table (the methods) and use void pointers a lot ;) -
you can probably find a detailed description by searching for "object
based" and C but this approach is really not used anymore for obvious
reasons.

hth,
ajm.
 
E

E. Robert Tisdale

Vishal said:
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.
 
M

Malcolm

Vishal Naidu said:
is it really possible to write object oriented code in C ?
and if yes how do we achieve abstration, polymorhism , hierarchy etc.
in C
The advantage of using C rather than an object-oriented language is that you
get to decide the protocol.

For instnace you could define a struct OBJECT
typedef struct
{
void **interfaces;
char **interfacenames;
in Ninterfaces;
} OBJECT;

Then you can have a function void *get_interface(OBJECT *obj, char *name)

This queries the object for an interface, and returns it.

eg
typedef struct
{
void *draw(unsigned char *rgb, int width, int height, int x, int y);
} DRAWABLE;

Thus you build your object from interfaces.
 
S

sahu

hello vishal i too is a coll student in svnit surat second yr comps. i
think it's not possible to use c for oop for using oop u have to use
cpp.by the way it will be nice to discuss with u the probs in c. tell
me how will u create a two-d array with dynamic allocation.like int **t
for a[5][8].ok bye
 
M

Mark Gordon

hello vishal i too is a coll student in svnit surat second yr comps. i
think it's not possible to use c for oop for using oop u have to use

Use real English not stupid contractions like u, yr etc. We don't
expect perfection, but at least make the attempt. The overall effect
of these contractions is that I just can't be bothered to to put in
the work required to read most of your post.
cpp.by the way it will be nice to discuss with u the probs in c. tell
me how will u create a two-d array with dynamic allocation.like int
**t for a[5][8].ok bye

This is a FAQ, so search for and read the FAQ.
 
J

Jack Klein

i m a college student in my second year.....
my queston is..
is it really possible to write object oriented code in C ?
and if yes how do we achieve abstration, polymorhism , hierarchy etc.
in C

What do "polymorphism" (to spell it correctly) and "hierarchy" have to
do with object orientation? These are features provided by some
languages to promote and extend the use of object orientation, but
they are far from the minimum object oriented feature set.

The FILE type in C, declared in <stdio.h>, is a perfect example of
object orientation in C.

It's members are unspecified. There is a function to create an object
of this type, fopen(). There is a function to uncreate an object of
this type when it is no longer needed, fclose().

While you have an object of this type, after a successful fopen() and
prior to fclose(), you can invoke object specific methods on it, such
as fread() or fprintf() or fseek(). You can call other methods to
check its status, such as feof() or ferror().

You confuse many concepts build on top of object orientation with
object orientation itself. Or you are using the term to means
something considerably more than it actually does.
 
G

Guillaume

Vishal said:
is it really possible to write object oriented code in C ?

Of course. I do it all the time.
The real question is "why would it not be possible".

Object-oriented programming is basically a form of programming
that's centered around objects. If you design your programs
around objects, that will come naturally. Some "features" of
some typical OO-languages will be harder to implement; but
they also might be less than desirable (it can lead to a long
debate...)

I'd go as far as claiming that any serious piece of program
implemented in C *should* be object-oriented. As long as you
have data structures and functions that use them, you have
the opportunity to write in an object-oriented maneer.

No one should be allowed to tell you that you should use C++
instead. ;-) Now if you want a really "strong" alternative
to C, you should consider ADA instead. (And here come the
flame wars...)
 
A

Alexei A. Frounze

pete said:
What's a singleton?

There're classes which must have only instance. If I'm not mistaken, this is
what's called singleton, the only instance.

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top