473,769 Members | 6,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2299
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*********@gm ail.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 **interfacename s;
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********@gma il.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*********@gm ail.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 "polymorphi sm" (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.l earn.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

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

Similar topics

1
10962
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 currently migrating to a new PC running XP. The charting application is hanging on a single line of code: Printer.Orientation = vbPRORLandscape It throws an error '484', "Problem getting information from the printer. Make sure printer is set up...
1
2608
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 topics to include in a course on object-orientation that I'm going to conduct. (I will later summarize all the replies and discussion, for the
4
7317
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 information was not all clear on this. Best Regards Ty
65
4287
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 completely independent of each other except for their well-defined interfaces. This was an attempt to explain the gist of OOP to programmers accustomed to the structured programming paradigm. I tried to explain OOP in terms of ideals that can be...
9
4804
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
3
2657
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 cause the database to grow. Here's my test code: Public Sub ChangeReportSettings( _ sReport As String, _ sDeviceName As String, _ Optional nIterations As Long = 1)
5
3350
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 advance Zadkin
4
6813
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 orientation. The really odd thing is that if I step through the code using F8, it will open in PrintPreview Mode as Landscape. The relevant code is as follows. rpt.Section(acDetail).Visible = False With rpt .Printer.Orientation =...
0
3434
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 data. This question is two part. One, I seem to be getting the title error at the starting line stating: oSheet.Range(rng).Select With Selection shp.Left = .Left shp.Top = .Top
0
9587
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.