Sorry I know this is a dumb question, but I haven't been able to figure out an answer from my internet research.
Since C++ is a Object oriented language and C is not, why are some systems written in both? Many thanks.
Sorry I know this is a dumb question, but I haven't been able to figure out an answer from my internet research.
Since C++ is a Object oriented language and C is not, why are some systems written in both? Many thanks.
C came out a long time before C++, so everything was originally just written in C. Then people started writing new code in C++ since it's much easier and cleaner than C. Some things that do really low level tasks like drivers are written in C (or even assembly) for efficiency...
If you're asking because you want to know when to use C and when to use C++, I'd use this formula:
If you can write it in C++, then write it in C++.
If you're stuck with using C, well, what can you do? Use C.
If you write something in C++ and you can prove that you have a serious performance issue, try to fix it in C++; if that doesn't help, re-write parts of it in C. If that's still not good enough, the next level down is assembly.
Thanks so much for your response cpjust. That's very helpful and I will sure remember that.
C is a much simpler language - not necessarily to learn to a working knowledge level, but definitely to write a compiler for. C is typically the first language ported to an unknown system.
C generates simpler code. With C++, there's constructor calls, operator calls, exceptions, RTTI, and so on. This additional, "hidden" code is undesirable under some circumstances, e.g. when writing low-level kernel code or device drivers.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Many OS's are written in C, mostly because the core OS code was written when C++ was just starting out as a language, and compilers where not very reliable (if at all available). I don't know if this has changed in Vista, but XP certainly have no direct support for C++ in drivers. You can make drivers from C++ code, but it's a fair amount of hard work to "adapt" the C++ code to work in a driver, and there are several restrictions, such as "no global objects" because there is no way to make the constructor calls, and no C++ style try/catch exceptions.
I haven't seen any Linux C++ drivers.
Writing low-level code in C++ is often harder than doing it in C (unless you just use the C++ compiler to compile straight C-code).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Once a C compiler has been built for a new system, isn't it just a matter of taking the source code for a C++ compiler and building it with your new C compiler, or does a C++ compiler have some platform specific code that needs to be ported? i.e. are C++ compilers written purely in C?
C++ is a superset of C, it was made before C. Why use C? Many reasons. One reason is C can typically is closer to assembly, and was much more widely used back in the day. C is procedural. Some programmers don't like object oriented code (I've met a few), so they use C.
Bzuh?
C is a good decade or two older than C++.
The absolute minimum you have to do when porting C++ to a new platform is implementing the stack unwinding required by exception handling. This is not entirely portable, since different architectures work differently.Originally Posted by cpjust
However, you'll also want to consult the architecture reference for things like alignment (OK, C suffers from that, too), and you'll have to port the generated code for things like virtual dispatch and runtime type identification.
It is true, though, that the bulk of the porting work is writing the new code emitter, which can usually be shared between C and C++.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
There's usually a portion of C++ library to port too, before you can write apps in C++. Yes, 95-99% of that is written in portable C or C++, but some small portions need to be written in non-portable assembler or non-portable C/C++ code.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks so much everyone! That would help my interview.