LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2005, 04:04 PM   #1
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
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 ?
 
Old 05-25-2005, 04:16 PM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
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.
 
Old 05-25-2005, 04:21 PM   #3
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
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 .

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.

Last edited by towlie; 05-25-2005 at 04:52 PM.
 
Old 05-25-2005, 05:30 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 05-26-2005, 01:06 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 05-26-2005, 02:41 AM   #6
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
maybe should go striaght to COM style and similar instead of OOP .......
 
Old 05-26-2005, 03:38 AM   #7
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
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
 
Old 05-26-2005, 01:52 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 05-26-2005, 02:03 PM   #9
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Original Poster
Rep: Reputation: 31
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)
 
Old 05-26-2005, 02:30 PM   #10
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
Polymorphism is probably easier in C++ although I'm sure there
are probably people who like it in C


In C you'd probably use function pointers, but in C++ the class
syntax takes care of it.
 
Old 05-27-2005, 09:45 AM   #11
Chris Weimer
Member
 
Registered: Jan 2004
Location: NYC
Distribution: Fedora XFCE
Posts: 91

Rep: Reputation: Disabled
From my website you'll find this great tutorial.

Hope that helps.
 
Old 05-30-2005, 03:23 PM   #12
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 05-30-2005, 04:33 PM   #13
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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
 
Old 05-30-2005, 06:00 PM   #14
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 05-30-2005, 06:41 PM   #15
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration