473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Object orientation.....

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

Nov 15 '05 #1
11 2266
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.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29 July 1999
http://www.cpax.org.uk
Email rjh at the above domain

Nov 15 '05 #2
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.

Nov 15 '05 #3
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.
Nov 15 '05 #4

"Vishal Naidu" <na*********@gmail.com> wrote
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.

Nov 15 '05 #5
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

Nov 15 '05 #6
On 3 Sep 2005 12:02:16 -0700
"sahu" <sw********@gmail.com> wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #7
On 1 Sep 2005 01:43:41 -0700, "Vishal Naidu" <na*********@gmail.com>
wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #8
Vishal Naidu wrote:
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...)
Nov 15 '05 #9
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
but C does *not* support data hiding -- private data members.


Except for singletons, where static data can be used for it.

ImRe
Nov 15 '05 #10
Imre Palik wrote:

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
but C does *not* support data hiding -- private data members.


Except for singletons, where static data can be used for it.


What's a singleton?

--
pete
Nov 15 '05 #11
"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
but C does *not* support data hiding -- private data members.

Except for singletons, where static data can be used for it.


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
Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bruce Adams | last post by:
I am using VB6 SP6. I have an application that prints charts. Specifically, it prints line charts of plant process information nightly. The code is a mix of API calls and Printer methods. I am...
1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
4
by: tvmaly | last post by:
Does anyone know if it is possible to determine if an image is horizontal/vertical and color or black & white using the python image library? I have been searching this news group and the...
65
by: Roger Smythe | last post by:
A means for the progressive decomposition a problem space into increasingly simpler component parts such that these component parts represent higher levels of conceptual abstraction, and are...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: Rick Collard | last post by:
I have been experimenting with the new Printer object in Access 2002 and discovered some unexpected database bloat. Simply assigning the same value to a property of a report's Printer object will...
5
by: Zadkin | last post by:
Does anyone know, if it's possible to set the orientation of the tabpagebuttons to horizontal instead of vertical when the alignment property of my tabcontrol is set to left or right? Thanks in...
4
by: J360 | last post by:
Access 2003. I've set the report orientation to landscape using VB. The report that pops up is in Portrait view, but page orientation is correctly set to Landscape and it prints in landscape...
0
by: CoreyReynolds | last post by:
Hey all, I have a piece of code that dumps a bunch of data into a spreadsheet. Also rearranges it into a pivot table and then graphs the pivot table as well so my boss can get a clear view of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.