Is Polymorphism possible in C? [duplicate] - c

This question already has answers here:
How would one write object-oriented code in C? [closed]
(32 answers)
Closed 8 years ago.
Can I include or declare a function inside a structure? I am trying to achieve polymorphism in C. If defining a function isn't the correct way to do it, what other methods can I use?

Polymorphism as a feature of object-oriented languages is not available in C. Neither are encapsulation and inheritance - the language does not have the corresponding features.
This does not mean, however, that it is impossible to model the corresponding behavior with the regular features of C: it is possible to build a library that lets you produce behavior that looks like polymorphism, for example, by using arrays of function pointers.

Related

How do programmers make a programming language on top of C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am interested in making my own programming language on top of C, but I have no
idea where to start.
So, I researched, this caught my attention:
A lot of languages are C-based.
Popular programming languages like C++ and Objective-C, and possibly C# and Java are all built on top of C. (Not to mention Python)
How did C++ and Objective-C creators managed to make a new language that is C based, but add object oriented programming concept added?
C based does not neccesarally mean that it's interoperable with c. It can just mean, that it uses the same paradigm or similar syntax like egyptian bracets.
Java is C based but also borrows from Smalltalk and it cannot run any of C code. C++ can call C functions because it's compiler produces the same binary code as C code. Once you have linked their binaries they become interoperable. Pythons implementation CPython does not produce any binaries first, it's an interpreted implementation, meaning it is run by an interpreter Programm which holds the whole parsed syntax tree in memory. And because the Interpreter is just a C programm itself, it can call other C functions. So there is no such thing like adding features in top of C. Those are different languages, with differen compilers/interpreters and grammatics which just borrow some of the grammatical rules from c.
The easiest way to start creating an own language, is by using a parser generator like antlr. And i would start creating an interpreter and not a binary compiler. Perhaps a compiler, which compiles to c. Knowledge about language grammatics is essential.

Popular diagramming tools or methodologies for C [duplicate]

This question already has answers here:
Tools to get a pictorial function call graph of code [closed]
(7 answers)
Closed 9 years ago.
Coming from a java (and other OO backgrond ) i got very cosy in with my objects, natural encapsulation and polymorphism.
All this i expected, the one this i didn't expect was to miss my class diagrams!
When the going gets tough or you start to worry about over coupling it was always my first stop.
But i cant seem to find a C style equivalent (that doesn't date from the mid 90's) diagramming system or utility for C.
have i just missed some thing? is there a hidden gem out there some where?
Even just something to show function calls between files so i can get an idea of whats going on where.
In short: Does any one have a suggestion (or tool) for how to model C file sets? function calls, includes, etc.
Thanks.
You can generate C code from class diagrams with UML applications such as IBM Rational Rhapsody or Eclipse-based open source Topcased.
You can generate call graphs, calling graphs and dependency graphs from C code with doxygen, powered by graphviz.

How is C not object oriented? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can you write object oriented code in C?
Object oriented programming in C
So, as I get it, "objects" are basically just wrappers for values and methods. Can't you have the same functionality in C, with structs? A struct seems just like a simple class, but, of course, it doesn't have any methods. Here we get to the heart of my problem: I don't understand why methods are needed at all. Wouldn't it be simpler and more memory-efficient if we had an outside function, which just accepted a pointer to an instance of a struct? Or even have the structs have pointers to these functions, but this seems purely aesthetic...
Being object-oriented means being object-oriented out of the box. Sure you can simulate object orientation with C (and with many other non-OO languages), but if the language (and/or its standard library) does not help you with that in any way (special syntax, standard objects, etc) and does not encourage to write in OO style by default, it will not be called object-oriented.
Using c-style function pointers as struct members, you can indeed make C object oriented.
I like the concept of having a class with its attributes and methods all defined together. Its easier to see what the related entities are, as opposed to having separate functions that take pointers to the struct like you mention.
Here are 2 related SO questions:
Is there a simple way to code the strategy (or other) design pattern in ANSI C that would fit on the screen of an 11" MacBook Air?
How do function pointers in C work?
What you suggest is already used to implement OO features in C. But many people find easier to use dedicated languages where OO features are included.
This is the same trend as why people switched to C from assembly.
Object is real-world entity and it has state, behavior. In class(c++/java) we can define state as member variables and functions as behavior. In case of C: in struct, you may have state but not behavior. Therefore C is not object oriented. This is just a small e.g. Also C does not support OOPS principals like inheritance, function overloading, polymorphism etc.

Data structures in C? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Good STL-like library for C
Are there any open source C libraries with common data structures?
Is there a "standard" library that C programmers use for frequently-used data structures (hash/tree-based sets/maps, heaps, etc.)? Or is there no single, well-known implementation?
(Something kind of like Boost for C++?)
See GLib or APR (Apache Portable Runtime) library, they are the most well-known C libraries for data structures.
http://developer.gnome.org/glib
http://apr.apache.org

Object Oriented pattern in C? [duplicate]

This question already has answers here:
How would one write object-oriented code in C? [closed]
(32 answers)
Closed 1 year ago.
Possible Duplicate:
Can you write object oriented code in C?
I am writing a large application in C and have heard that prior to the advent of C++ programmers used to implement the "Object Oriented" pattern in C. My question is what is the usual form this pattern takes? and how would I go about implementing such an OOP pattern in a modern C application?
Here are a few helpful links to guides on Object Oriented C:
Object Oriented Programming with C - A very thorough treatment of the subject.
Phil's guide to object-oriented C - This is a rather simplistic approach to the subject, imo.
GObject Reference Manual - GObject is used heavily throughout Gnome and GTK+ applications (mostly on Linux) and therefore provides a thorough example of Object Oriented C in the real world.
Where a C++ object has methods, object-style 'C' takes a struct full of function pointers. The functions corresponding to a member function have an explicit data argument that takes the place of the implied 'this' pointer.
Subclasses use function-pointer structs of the same type, with different function pointers to indicate overridded methods.
I used to simply adopt naming conventions for a structure and associated "methods".
Each method would begin with e.g. CANDIDATE_ for a candidate object, and be associated with a typedef CANDIDATE { ... }, and be in a file Candidate.c
An additional link from someone who wrote several OO frameworks for C.

Resources