ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have had at least 2 years of experience in C programming and I am wandering what exactly can C++ do and C can't or better yet what is C++ better than C at ? What are the advantages .. and of course .. what are the disadvantages ?
Also , how "hard" is it for a C programmer to learn C++ ? Or how easy ? I know very little things about C (the I/O streams, the fact that this is legal: for (int i=0; i<n; i++) { .. } etc.) but don't know anything about OO in C++ and such .. How hard is it ?
If you know C you will pick up C++ very easily. Why? Because it lets you do C and much more. The code is the same with some improvements, but not much in the way programs are written has changed. You should get it pretty fast.
And you'd basically write the same functions except you'd put them
in your class. The syntax shortcuts can be a real timesaver (assuming
your compiler can handle them).
You can use templates to make generic sorting functions instead of using
function references (pointers). The syntax sucks for both of those language
constructs, but at least function references are pretty well handled. If you wanna
use templates, make sure your compiler can do them properly.
Yeah the C++ STL was kind of supposed to eliminate c-strings and other stuff by providing
plenty of data structures (like strings, lists, resizable arrays, and maps). Unfortunately,
many STL implementations are (IMO) essentially broken.
Short answer: yes classes can help you save time and code, and make your code
look more natural (class methods, operator overloading, etc...), but don't learn
C++ expecting to take full advantage of stuff like the STL or exception handling.
EDIT:
btw here's a link to a page that shows exactly
what operator overloading with classes can do for you.
It's kinda big, but the code is repetitive, it's really not that hard to
understand if you know the math. This kind of thing is great when you
want to write a program to do your, er, I mean, check your homework
for you .
Don't think that all C++ has to be this complex, I went all out with that
file, but it can be as simple or complex as you'd like it to be.
I agree. C++ sucks to debug when it is written procedurally with bits of OO. I didn't come from C, but I was a procedural programmer for 10+ years before C++ and had to learn to split processes up into functional sections and consolidating certain data behaviors into classes. Like the first few responders said, you will pick it up fast, but I'm sure it will take a while to get in the habit of making simple classes that build upon each other (I still have problems with that). I initially learned from www.cplusplus.com, which gets straight to the point. Learning the semantics of it is the easy part. Learning how to take advantage of the improvements over C is the hard part.
ta0kira
I think it shouldn't be very difficult. You only have to learn it bit by bit, learning one feature every time, your will be coding more and more in the C++ way.
I can remember my self. Writing
Code:
int i = 5;
float f = 3.141592;
cout << "this is an integer " << i << " followed by a float " << f << endl;
instead of
Code:
printf( "this is an integer %i followed by a float %f\n" );
was so unnatural (and stupid, I thought, specially when I wanted to format the output) that I continued to include "stdio.h" and use printf for a while. But on the other hand, I loved function overloading and parameter values by default. I started with them and continued to program procedurally. Then I discovered classes (which are probably the main thing to learn, as you have to change a bit the way you think). This allowed me to use operatos overloading, which I also like a lot, yet I know it can be confusing sometimes. Rencently I started to learn templates and the STL. All this has taken a couple of months, but I think it worth the try...
printf( "this is an integer %i followed by a float %f\n" );
was so unnatural (and stupid, I thought, specially when I wanted to format the output) that I continued to include "stdio.h" and use printf for a while.
And you since got so used to cout that you forgot the
actual variables in your call to printf :)
hm .. BUT! in the Linux/UNIX world , most programs are implemented in C (including most [if not all!?] of the linux kernel etc.) .. there are much more people that know C than people that know C++ thus there would be more people who could understand your code and help with it (report bugs/contribute source etc.) if your project is built in C than in C++ .. what are your comments on this ?
question about polymorphism.. if anyone has ever used such techniques (maybe some vxers reading this thread ? :>) .. wich language makes your life easier when wanting to use this programing technique ? C or C++ (actually I have no idea if polymorphic code can be created using C.. I have only a vague idea on how polymorphism can be achieved)
The purpose of polymorphism is so that one object can appear to be other types of objects for the purposes of function arguments or pointers. The way this is made useful is through member functions that are inherited from other classes. Since C only uses data structs and does not allow member functions or inheritance, there is no way to use polymorphism in C.
I disagree that there are more C users than C++. C is definitely older, but most GUI-oriented programs are C++. C is used more for data processing that is done with one command line (or less); not generally interactive. Regardless of if there is more of one or the other, the language you use depends on what purpose your program serves.
ta0kira
That isn't polymorphism; you are passing a data member whereas with polymorphism the same object can look like both (not a "piece" of it can look like something else).
ta0kira
Remember, the first C++ compiler just translated all the code to C, so you can do just about every C++ feature with C, including polymorphism. Polymorphism is really ugly looking, though. Basically you wind up using a dispatch table of function pointers to call the correct function based on a type identifier embedded in a struct.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.