🔒 Locked
C vs C++
Advertisement
Original Post
I know this is probably a very debatable subject, but when is it better to use C instead of C++ or vice versa? I ask this because I find C easier to use than C++ because I find procedural programming easier to understand than OOP. When is it better to use OOP? Thanks to whoever helps clear this up...
Quote:
Original post by Kaze
why C vs C++
if you really want to learn OOP try java, C#, python or ruby
This doesn't really answer the question though, I want to know when its better to use C++ instead of C or vice versa.
Quote:
Original post by ApochPiQ
It's only ever "better" if you are more comfortable in one language over the other. Whatever gets the job done the fastest and with the best results wins.
Any other debate over what language to pick is pretty much pointless.
Alright thanks, I guess I have to start thinking in terms of 'getting the job done'.
Quote:
Original post by Kaze
if you really want to learn OOP try java, C#, python or ruby
C++ is not "C with classes".
A good reason for using C instead of C++ is: C compilers are easier to implement than C++ compilers, thus for some platforms, there simply are no C++ compilers and you have to use plain old C.
Quote:
Whatever gets the job done the fastest and with the best results wins.
For small and simple, absolutely.
If the code is large and has reach beyond the immediate and is to be supported beyond your scope of involvement, quality design to allow low cost of ownership and support is more important then getting it done the fastest. The definition of 'best restults' must be keyed to the life-cycle of the code.
Overall, large C code is much more difficult to have staff maintain as it is typically more difficult to find the part needing the correction of updating - but not always if the code flow is documented well. Maintaining Cplusplus should be easier as you can find the object(s) involved. The problem is not always knowing how the objects interact with other objects and this is typically not document (spaghetti objects vs. spaghetti code).
Bottom line, if it is for you, small, use C as it seems to be your comfort level.
Best luck!
These days, C is undisputed champion of system programming taking the role of portable assembly. Also, legacy code and lowest common denominator.
OOP practices don't require OO language.
That is not language trait. I also avoid books written in Kanji since I don't understand them. It doesn't say anything about books themselves.
Not a language trait either. Lots of C++ code is horrible and much more unreliable than equivalent C code could be.
Quote:
When is it better to use OOP?
OOP practices don't require OO language.
Quote:
I ask this because I find C easier to use than C++ because I find procedural programming easier to understand than OOP.
That is not language trait. I also avoid books written in Kanji since I don't understand them. It doesn't say anything about books themselves.
Quote:
Overall, large C code is much more difficult to have staff maintain as it is typically more difficult to find the part needing the correction of updating
Not a language trait either. Lots of C++ code is horrible and much more unreliable than equivalent C code could be.
Quote:
Original post by Antheus
Lots of C++ code is horrible and much more unreliable than equivalent C code could be.
Hard to say, really. While I agree there's lots of horrible C++ code out there, I doubt the “equivalent” C code would be much better if it was written by the same programmer.
Personally, I don't see any point in using C these days, except for platforms that simply don't offer anything but a C compiler -- or if you want to work with large and old codebase that's already written in C.
C++ does not force you to program in an object-oriented manner, it's a multi-paradigm language -- its point is not to support OOP but to support multiple paradigms, including OOP and procedural programming.
And anyway, C++ is quite a low-level programming language, and arguably the most complicated language in mainstream use today. I realise this is not what OP asked, but I would probably take a peak around other languages than C and C++ -- it's not like learning them took ages like in C++'s case. [grin]
Sometimes things are just done easier in C as well. I would pick C++ over C any day if I could. But when writing a z80 assembler with a group of three others, we decided C was the best. It's easily maintainable, and object-oriented. It works perfect, and did I mention that a C++ compiler can't compile the exact code without some modifications?
At any rate, there is no reason why you can't write perfectly fine object-oriented code in any language. There's even a high-level object-oriented game engine written for the TI-83+ calculator in z80 assembly.
At any rate, there is no reason why you can't write perfectly fine object-oriented code in any language. There's even a high-level object-oriented game engine written for the TI-83+ calculator in z80 assembly.
C++ is not 'C with more tacked on'; the two are separate languages and code that will compile fine in C may not compile for C++ and vice versa.
It's a matter of picking the right tool for the job. C++ has increasingly been replacing C as the right tool for many tasks and, like wise, these days other languages have been replacing C++ as the right tool too.
As for OOP it doesn't matter which you pick. You can write object-oriented C code and you can write procedural C++ code.
If you wanted to learn OOP then try to keep your understanding at a language agnostic level, like polymorphism, SRP, and so on. These things can be applied, within reason, to most any language once you bow to each one's own idiosyncrasies.
There are languages like Java that claim to be purely-OO, whilst they are certainly 'oriented around objects' they often don't follow good OO practices. Most likely you'll have to break some rules to write OO code in any language though.
OOP is a tool, an optional one at that. If you have a problem that you must solve and you're more comfortable to do it in C, then by all means to it in C. Bear in mind though, that learning C++ and/or OOP would always be a beneficial experience rather than a waste of time.
It's a matter of picking the right tool for the job. C++ has increasingly been replacing C as the right tool for many tasks and, like wise, these days other languages have been replacing C++ as the right tool too.
As for OOP it doesn't matter which you pick. You can write object-oriented C code and you can write procedural C++ code.
If you wanted to learn OOP then try to keep your understanding at a language agnostic level, like polymorphism, SRP, and so on. These things can be applied, within reason, to most any language once you bow to each one's own idiosyncrasies.
There are languages like Java that claim to be purely-OO, whilst they are certainly 'oriented around objects' they often don't follow good OO practices. Most likely you'll have to break some rules to write OO code in any language though.
OOP is a tool, an optional one at that. If you have a problem that you must solve and you're more comfortable to do it in C, then by all means to it in C. Bear in mind though, that learning C++ and/or OOP would always be a beneficial experience rather than a waste of time.
From what I have read procedural programming reaches the limits of maintainability around the millions of lines of code. while OOP hits those limits in the 10s to 100s of millions of lines of code. Properly used Standard C++ is safer than Standard C (or at least easier to use in a safe manner).
It's been so long since I was limited to pure C that I'm not totally sure I remember what it is anymore. I guess I'm not very qualified to answer the question.
There are no classes. Does that mean no member functions? If that's the case, I would put C++ above it for that single feature.
There are no classes. Does that mean no member functions? If that's the case, I would put C++ above it for that single feature.
Use C if you are writing the public API of a library that you want to be callable by other languages.
Use C++ if you intend to use templates and the awesomely powerful Standard Template Library or any of the awesomely powerful boost libraries.
Use Java if you are wanting to interact with Java code.
Use .NET (C# preferably, but Managed C++ is ok too) if you want to interact with .NET code (like XNA).
Use whatever language you want for anything else. (And if wanting to "learn" not use, OO concepts I currently recommend C# as the cleanest learning language of choice ... then once learned, you can use all the ideas in any enviroment you want ... ruby, python, C, your own custom language, etc).
Use C++ if you intend to use templates and the awesomely powerful Standard Template Library or any of the awesomely powerful boost libraries.
Use Java if you are wanting to interact with Java code.
Use .NET (C# preferably, but Managed C++ is ok too) if you want to interact with .NET code (like XNA).
Use whatever language you want for anything else. (And if wanting to "learn" not use, OO concepts I currently recommend C# as the cleanest learning language of choice ... then once learned, you can use all the ideas in any enviroment you want ... ruby, python, C, your own custom language, etc).
Personally, the thing I miss most when I have to work with C is the whole RAII system of destructors, exceptions and so on.
Other than that, and templates I suppose, you can pretty much simulate everything else C++ gives you in C with a bit of work.
For example, member functions on objects can be implemented by manually passing the equivalent of a this pointer to free functions.
What becomes an annoyance to me is the lack of any way to automate the call to str_destroy in the above example at scope exit, so I end up having to nest functions really deeply.
Other than that, and templates I suppose, you can pretty much simulate everything else C++ gives you in C with a bit of work.
For example, member functions on objects can be implemented by manually passing the equivalent of a this pointer to free functions.
struct string_s{ char *data; size_t length;};typedef struct string_s string;string *str_create(const char *s);void str_destroy(string *s);void str_append(string *s,const char *text);What becomes an annoyance to me is the lack of any way to automate the call to str_destroy in the above example at scope exit, so I end up having to nest functions really deeply.
Quote:
Original post by Deprogrammer
When is it better to use OOP?
When you expect your project to change significantly (add new features, modify old features) over the course of its development, or when your project is large (small changes can significantly affect the rest of the system). Which is true for about 90% of software projects, hence why OOP was created.
Well this is the creator of C++ opinion on this subject which I agree with:
C is better than C++ for small projects, right?
Not in my opinion. I never saw a project for which C was better than C++ for any reason but the lack of a good C++ compiler.
But yeah I guess C++ does make it hard to choose since it offers you more freedom since unlike other languages like Java it doesn't force you to start even the simplest program with a class!
[Edited by - daviangel on August 30, 2008 7:00:24 PM]
C is better than C++ for small projects, right?
Not in my opinion. I never saw a project for which C was better than C++ for any reason but the lack of a good C++ compiler.
But yeah I guess C++ does make it hard to choose since it offers you more freedom since unlike other languages like Java it doesn't force you to start even the simplest program with a class!
[Edited by - daviangel on August 30, 2008 7:00:24 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement