LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C to C++ (https://www.linuxquestions.org/questions/programming-9/c-to-c-327084/)

introuble 05-25-2005 04:04 PM

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 ?

marghorp 05-25-2005 04:16 PM

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.

towlie 05-25-2005 04:21 PM

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;
init_point(&p1, x1, y1);
init_point(&p2, x2, y2);

point_add(p1, p2, &p3);  // p3= p1 + p2

would go to:
Code:

point p1(x1, y1), p2(x2, y2), p3;
p3= p1 + p2;

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 :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.

Tinkster 05-25-2005 05:30 PM

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

ta0kira 05-26-2005 01:06 AM

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

alred 05-26-2005 02:41 AM

maybe should go striaght to COM style and similar instead of OOP .......

enemorales 05-26-2005 03:38 AM

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...

Regards

Tinkster 05-26-2005 01:52 PM

Quote:

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.
And you since got so used to cout that you forgot the
actual variables in your call to printf :)


Cheers,
Tink

introuble 05-26-2005 02:03 PM

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)

towlie 05-26-2005 02:30 PM

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.

Chris Weimer 05-27-2005 09:45 AM

From my website you'll find this great tutorial.

Hope that helps.

ta0kira 05-30-2005 03:23 PM

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

jonaskoelker 05-30-2005 04:33 PM

Quote:

Since C only uses data structs and does not allow member functions or inheritance, there is no way to use polymorphism in C.
Yes there is, you just have to do it `by hand';

Code:

struct foo {
    int x;
};

stuct bar {
    int y;
    struct foo super;
};

struct bar b;
bar_constructor(&b);
frob_foo(&b.super);

I haven't used GTK in C, but have a look at how they do it.

my %0.2f dollars; --Jonas

ta0kira 05-30-2005 06:00 PM

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

btmiller 05-30-2005 06:41 PM

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.