Go Down

Topic: What is the difference between C and C++ (Read 912 times) previous topic - next topic


Coding Badly

Quote
What is the difference between C and C++


class, virtual, const, public, protected, private, template, friend, overloading, and name mangling.

Quote
Which is better to use?


In the context of programming your Arduino your question is irrelevant.  Your sketch is always fed to the C++ compiler.

So really just the structuring and keywords are the only distinction; therefore, there is no difference between the two.
Does one work better than the other with certain types of hardware?

Arrch


So really just the structuring and keywords are the only distinction;


That's not what was said.

michinyon

Quote
Does one work better than the other with certain types of hardware?


The hardware doesn't care,  because it operates on some kind of machine language and it doesn't
matter what programming language that is compiled and linked from.

Quote
class, virtual, const, public, protected, private, template, friend, overloading, and name mangling.

What does all this mean?

michinyon

Well,  I could sit here and type out the lecture notes for three or four semesters of teaching programming,
or, on the other hand,  perhaps not.

Arrch


Quote
class, virtual, const, public, protected, private, template, friend, overloading, and name mangling.

What does all this mean?


It means Google has a lot to say about this.

JimboZA

If you Google "C vs C++" you get almost 18M hits.... there's no shortage of discussion on the subject!
My hovercraft is full of eels.

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.


Coding Badly

#10
Mar 04, 2013, 06:54 am Last Edit: Mar 04, 2013, 06:56 am by Coding Badly Reason: 1
Quote
Does one work better than the other with certain types of hardware?


Sort of.  Some processors have excellent support for virtual method tables while other processors do not.  Typically, virtual method tables are not used in C but they can be.


Quote
class, virtual, const, public, protected, private, template, friend, overloading, and name mangling.

What does all this mean?


Most of the items are keywords present in C++ but not in C.  The last two are concepts present in C++ but not in C.

Essentially, using the words in the list as keywords in Google searches will help you answer your question.

Nick Gammon


So really just the structuring and keywords are the only distinction; therefore, there is no difference between the two.


That is hardly "no difference". C++ has classes, for one thing. And templates.


Does one work better than the other with certain types of hardware?


The g++ compiler generates good code in either case.

I recommend you use Google a bit, this is a pretty general question.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

westfw

Quote
really just the structuring and keywords are the only distinction

well, you could say THAT about the difference between Cobol and C...

C++ is a different programming language than C.  Unlike most "new" languages, it was designed to be a sort of add-on to the C language.  So (almost) any legal C language program is also a legal C++ program.
C++ add a bunch of features, many of which are not used much in the arduino environment.  People have mentioned a couple of these, and they're complex enough that they're not easy to explain.

A C++ feature that shows up a lot, even in arduino code, is "polymorphism."  This encompasses the idea of being able to define function whose "definition" can be different depending on the type of the arguments.  So "print((int) 1);" and "print((char) 1);" call different code.

Some of the C++ features require resources and run-time libraries that don't exist for small microcontrollers like the AVR.  Many of the advantages of C++ are going to be less apparent in small programs.  My observation is that a lot of the purpose of "object oriented programming languages" like C++ stems from their ability to concisely define in libraries/classes many modern application concepts ("objects") like Windows and Mouse Buttons and so on.  If you've ever looked into writing or using a GUI library in an older language, you run into the problems solved by C++ pretty quickly.

Nick Gammon


Which is better to use?


What is better, a dog or a cat?

Which is better, a pick-up truck, or a sports car?

A violin, or a piano?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up