Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C programming data structures vs objected oriented programming?
hello. Is the concept of a structure in C the basic idea behind object oriented programming? Grouping multiple variables together? Just a short explanation would be nice.
6 Answers
- JonathanLv 78 years agoFavorite Answer
Object oriented programming carries many different meanings. To some, C++ defines the meaning because that is their only exposure to it, even though the ideas predate it. For some, it's defined by SmallTalk; or Python; etc. So it depends on the experiences or research interests one may have at the time. C++ provides a rather severely limited perspective, in fact. So I really can't give you a single answer and I don't have time for much of a comprehensive one. Just be aware that the meaning of OOP depends upon the speaker and the listener.
OOP (as the concepts are used in C++) can be done in C. In fact, C++ used to be compiled using CFRONT, which compiled C++ into C and then called the C compiler to actually generate object code. So to answer your question about C's "struct" being the "basic idea behind" OOP, I'd have to say that there is some truth to that idea when talking specifically about C++'s style of OOP. It was, in fact, the first part of C to be modified when developing C++ as an eventual language. But a lot of people may disagree because they aren't drawing their ideas of OOP only from C++ but from, say, Smalltalk.
(It was only with CFRONT 4, when struggling to implement fuller exception handling requirements that they gave up the approach and had to switch over to "true" C++ compiler technology, which then also pushed linker technology, as well.)
You can even do OOP in assembly code. Obviously. Because at the end of the day, all code winds up as some kind of executable machine code. Even .NET's CIL code gets compiled "just in time" at some point in the process and executes as machine code. And in assembly code, you have at your fingertips access to semantics and methods that are simply unavailable any other way.
But most people associate OOP with languages where you aren't manually coding up the entire OOP approach but instead where the language supports at least some basic set of automated OOP facilities.
And it can also mean an approach to program design, as opposed to a set of features in any particular language and their use, too. "Design Patterns" is an old (now) book that provides such an abstract viewpoint.
...
There is a very good book on how C++ implements OOP concepts "underneath the hood." It's by Stanley B. Lippman, May 1996, and called "Inside the C++ Object Model." The ISBN is 978-0-201-83454-3. In that book, you can see how C's structures are used to implement C++ OOP. It's a VERY GOOD book to get and read. And it will ANSWER your questions in detail, I think.
- jplatt39Lv 78 years ago
Understand it differently. A structure is essentially a public object which is accessible from everywhere and can be changed by anyone. An object in OOP is a private object which usually contains the methods (functions and procedures -- void functions) which manipulate it. It is usually not directly accessible from other parts of the program.
In other words objects exist to answer the real security disadvantage that structures have. Data structures can be structures or objects.
- AnalProgrammerLv 78 years ago
There is a vague sort of connection between the grouping of data in a class and a C structure.
But that is as far as it goes. The individual items in a structure are grouped together in contiguous storage. There is no such connection in a class.
In a class you have methods or functions that can act on the data. You don't have that in a structure.
A class is more like a new data type that you have defined. It has state (the variables) and behaviour (the methods that act on the variables).
Have fun.
- Kasey CLv 78 years ago
Object oriented programming goes BEYOND data structure by adding the FUNCTIONS that act upon the data structures to the structures themselves, creating objects.
Remember, data structure is just group of data.
OOP adds the functions themselves to the data, like search, sort, update, and so on.
Add inheritance (i.e. you can "extend" an object by writing your own version of the functions and/or additional data) and you have a lot of "code reuse"
- How do you think about the answers? You can sign in to vote the answer.
- JaredLv 78 years ago
Disclaimer: the terms I use will be specific to Java, not C++ or any other OO language. Having said that, the terms I use should be canonical terms for OO languages.
structs in C are every much similar to objects. And you absolutely CAN write OO code in C.
structs in C, are data structures, as are objects. They BOTH hold data. The thing that is added to objects is methods.
When defining a struct you can include a header file which as ALL possible functions that can be called on the struct. By passing the struct (pointer to it possibly) as an argument to your functions you can define methods on the object (struct in this case).
But it goes deeper than that in C. You can ALSO define abstract classes and interfaces!
Using the function pointer, you can create an abstract class/interface. By adding function pointers to the struct, you can define "custom" methods for a particular struct. You could have nothing but function pointers, in which case it would be an interface, or you could have some data, with function pointers as well as perhaps SOME predefined functions (i.e. functions might be defined in terms of the function pointers passed as a parameter).
...it gets complicated, but YES, structs in C can mimic objects. Function pointers are (most likely--I believe) how C++ implements objects and virtual/non-virtual functions (because C++ uses the C language).
Edit:
You can somewhat emulate private and public (certainly not other levels of access though). You can emulate private members of a struct by requiring the user always use pointers to the struct. In the header file, you can declare that you have a struct, but NOT declare the structure of said struct. In this way, the programmer will not be able to access members of the struct directly--they can only do things through the functions you provide. But this means you have either ALL public members (by declaring the structure in the header file) or you have ALL private (by just declaring the existence of a struct).
Private functions/methods would be functions that are declared only within the source code for the struct (i.e. not in any header file). Again, those functions would not be available to someone who imported the header file. Static vs. non-static is really non-existent. In C all functions are static (in the sense of Java) because there are NO objects anyway. Still, the only difference between static and non-static methods/constants are semantical. Basically, all non-static functions would require a pointer to the struct as an argument (so that it could modify the struct) and all static functions would require no pointer to the struct.
You can even declare constructors and destructors. A constructor would return a pointer to a newly allocated struct (or you could give it a pointer and allocate new memory only if the passed pointer is NULL). You can destruct the object by passing the struct pointer to a method which frees all dynamic memory you might have created.
Bottom Line: no, you cannot do ALL of the things associated with OO in C (mainly access modifiers), but yes, you CAN write C code in the style of object oriented programming--I do this all the time. OO is really just writing code in a modularized way and you can definitely code in that style in C.
- ?Lv 58 years ago
Data Structure means grouping related data together.
Name, Age, Gender, etc
Where OOP
means using a data or group of data and perform actions, functions or even making new structures.