C to C++
Hello !
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.
|
Not hard, IMO.
Ask people what OO is in C++, it'll probably just start a flame. Instead of all the esoteric vocabulary (polymorphism, etc....) I like to think of OO as syntax shortcuts. For example: Code:
struct point p1, p2, p3; Code:
point p1(x1, y1), p2(x2, y2), p3; 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 :D . 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 disagree with the other guys ;}
If you're a well-versed C programmer you'll have to unlearn procedural thinking to make the most of C++s features ... :} Migrants from other OO languages will probably have an easier time with C++ than C coders. Cheers, Tink |
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 |
maybe should go striaght to COM style and similar instead of OOP .......
|
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; Code:
printf( "this is an integer %i followed by a float %f\n" ); Regards |
Quote:
actual variables in your call to printf :) Cheers, Tink |
thank you for replying !
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) |
Polymorphism is probably easier in C++ although I'm sure there
are probably people who like it in C :D In C you'd probably use function pointers, but in C++ the class syntax takes care of it. |
|
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 |
Quote:
Code:
struct foo { my %0.2f dollars; --Jonas |
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.
|
All times are GMT -5. The time now is 12:10 AM. |