LinuxQuestions.org
Review your favorite Linux distribution.
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 12-23-2012, 02:17 PM   #1
VisionIncision
Member
 
Registered: Dec 2011
Location: Wiltshire, UK
Distribution: Slackware, Gentoo
Posts: 130

Rep: Reputation: 3
Torn between C and C++


Hi all,
On my course (CS) we are taught mostly Java (had a little MIPS last year, oh and Haskell). Anyway, I am wanting to break away from Java, and learn another popular language in depth. I am torn between focusing on C or C++.

I am wanting to do my final year project on Machine Learning next year, and would possibly use whatever new language I learn, depending on the nature of the project of course.

So, what are your preferences and why?

Also, what are your views on OOP? Some swear by it, others loathe it.

Regards
 
Old 12-23-2012, 02:55 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I strongly prefer C++ over C, but a beginner needs to work with a well chosen subset of C++.

The strength of C is that a smart person can learn pretty much all of it and then the rest is learning to program well, rather than learning the language well.

In C++, most of the advanced constructs won't make any sense until you are very experienced in the basics of C++. Unless you have an incredibly good memory, you'll never learn C++ the way most good C programmers used to learn C (know all the rules and never need to look up an obscure detail). But for advanced C++ programmers, obscure details are easy to look up in the rare cases you need them. You don't need to memorize all the rules.

C strings are a nightmare for most beginning C programmers. Once you have solid experience with pointers in C and C++, learning C strings could be easy. But a C beginner needs to learn them the other way around. You need to learn C strings early, before you are ready to understand them. In C++, there is a sane and friendly string class that is much easier to learn early.

Much of C I/O (especially scanf) is ugly and is the subset of C that a C++ expert will probably never need, and like C strings, something a C beginner must learn before learning the related concepts that might make it easier. C++ I/O is much more beginner friendly.

The C++ vector class is also a lot easier to work with than C arrays. A really expert C++ programmer must know when it is better to use a C array instead of a C++ vector. But a C++ beginner can learn vector first and worry about C arrays much later.

All the above are reasons that C++, despite being a harder language, may be more beginner friendly than C.

As you build bigger projects, the real advantages of C++ over C become more evident:

Overloading functions (same function name for many different functions distinguished by argument type) is a very good feature. I used to work on big C projects and function naming was a major pain. You can never use the obvious name for a function because that name was already taken by some other function. Overloading is horribly abused in a lot of badly written C++ code. Sometimes you will be tempted to give many functions the same name, but experience would tell you it is better not to. It takes experience and judgement to know when it is clearer to reuse a name for many functions with the same purpose but different inputs, vs. choosing different names for clarity (or for other code maintenance reasons).

Last edited by johnsfine; 12-23-2012 at 03:01 PM.
 
Old 12-23-2012, 03:13 PM   #3
VisionIncision
Member
 
Registered: Dec 2011
Location: Wiltshire, UK
Distribution: Slackware, Gentoo
Posts: 130

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by johnsfine View Post
I strongly prefer C++ over C, but a beginner needs to work with a well chosen subset of C++.

The strength of C is that a smart person can learn pretty much all of it and then the rest is learning to program well, rather than learning the language well.

In C++, most of the advanced constructs won't make any sense until you are very experienced in the basics of C++. Unless you have an incredibly good memory, you'll never learn C++ the way most good C programmers used to learn C (know all the rules and never need to look up an obscure detail). But for advanced C++ programmers, obscure details are easy to look up in the rare cases you need them. You don't need to memorize all the rules.

C strings are a nightmare for most beginning C programmers. Once you have solid experience with pointers in C and C++, learning C strings could be easy. But a C beginner needs to learn them the other way around. You need to learn C strings early, before you are ready to understand them. In C++, there is a sane and friendly string class that is much easier to learn early.

Much of C I/O (especially scanf) is ugly and is the subset of C that a C++ expert will probably never need, and like C strings, something a C beginner must learn before learning the related concepts that might make it easier. C++ I/O is much more beginner friendly.

The C++ vector class is also a lot easier to work with than C arrays. A really expert C++ programmer must know when it is better to use a C array instead of a C++ vector. But a C++ beginner can learn vector first and worry about C arrays much later.

All the above are reasons that C++, despite being a harder language, may be more beginner friendly than C.

As you build bigger projects, the real advantages of C++ over C become more evident:

Overloading functions (same function name for many different functions distinguished by argument type) is a very good feature. I used to work on big C projects and function naming was a major pain. You can never use the obvious name for a function because that name was already taken by some other function. Overloading is horribly abused in a lot of badly written C++ code. Sometimes you will be tempted to give many functions the same name, but experience would tell you it is better not to. It takes experience and judgement to know when it is clearer to reuse a name for many functions with the same purpose but different inputs, vs. choosing different names for clarity (or for other code maintenance reasons).
Thanks for the reply, some good points in there!

What would you say about C++ being used as a procedural alternative to C, i.e. not used for OOP?
 
Old 12-23-2012, 03:13 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by VisionIncision View Post
Also, what are your views on OOP? Some swear by it, others loathe it.
I first did OOP in a programming group dominated by OOP fanatics. So I loathed it.

I find the optional OOP in C++ a very powerful tool for a good programmer. But that is a tricky spectrum. I find the extreme of optional OOP in Python to be too big an invitation to write really terrible code. Maybe if I had ever coded a Python project from scratch, I would have found a way to write good code. But just about every project I've ever worked on in any language had a pile of terrible code in it before I arrived. In C or Java or C++, building on a project's rotten foundation is a big but manageable problem. In Python it is hopeless, specifically because of the excess flexibility of optional OOP.

I hate strict encapsulation in OOP. The frequent hassle in working in a C++ project that you never get in C is the data you want to access is in an object that is not easily available from where you want to access it. The constant mantra of OOP fanatics is that a well designed program always fits the OOP paradigm, so when encapsulation or inheritance don't work out, it is because the design is wrong, not the paradigm. An experienced software engineer knows better. Many problems just don't fit strict OOP.

I really like inheritance and polymorphism in OOP. I was writing polymorphic objects in ASM and C long before C++ existed, but having the language do the book keeping leads to more maintainable code.

Quote:
Originally Posted by VisionIncision View Post
What would you say about C++ being used as a procedural alternative to C, i.e. not used for OOP?
You could write entirely non OOP code in C++. Use some pre defined classes like std::string and std::vector and the file stuff, but never write any non trivial classes yourself.

I don't see the point.

Once you get used to the odd syntax and understand its meaning, having functions defined in your structs is a powerful feature. It adds a lot of clarity to some of those massive overloads of common function names I mentioned earlier.

Having constructors and destructors called automatically at the right time is another powerful feature, as is the ability to redefine the assignment operator or other basic operators.

OOP is a very useful tool. OOP is only a handicap in the hands of OOP fanatics who want everything to be strictly OOP.

Many of my C++ classes have public data members. That was absolutely forbidden in the first group where I did C++ programming. There are a lot of situations in which the extra layer of an accessor function is very helpful between the data and the places that data is used. Usually you don't know you'll want that extra layer until later in project evolution, by which time it is very hard to add. So fanatics forbid ever accessing anything without the extra layer. An experienced programmer can make a better judgement call whether a given data item might ever need that and leave the extra layer out when it won't ever be needed.

Last edited by johnsfine; 12-23-2012 at 03:29 PM.
 
Old 12-23-2012, 07:02 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
I strongly prefer C++ over C, but a beginner needs to work with a well chosen subset of C++.
...
And that subset might well be "C" .
 
Old 12-23-2012, 07:10 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
I first did OOP in a programming group dominated by OOP fanatics. So I loathed it.

I find the optional OOP in C++ a very powerful tool for a good programmer. But that is a tricky spectrum. I find the extreme of optional OOP in Python to be too big an invitation to write really terrible code. Maybe if I had ever coded a Python project from scratch, I would have found a way to write good code. But just about every project I've ever worked on in any language had a pile of terrible code in it before I arrived. In C or Java or C++, building on a project's rotten foundation is a big but manageable problem. In Python it is hopeless, specifically because of the excess flexibility of optional OOP.

I hate strict encapsulation in OOP. The frequent hassle in working in a C++ project that you never get in C is the data you want to access is in an object that is not easily available from where you want to access it. The constant mantra of OOP fanatics is that a well designed program always fits the OOP paradigm, so when encapsulation or inheritance don't work out, it is because the design is wrong, not the paradigm. An experienced software engineer knows better. Many problems just don't fit strict OOP.

I really like inheritance and polymorphism in OOP. I was writing polymorphic objects in ASM and C long before C++ existed, but having the language do the book keeping leads to more maintainable code.



You could write entirely non OOP code in C++. Use some pre defined classes like std::string and std::vector and the file stuff, but never write any non trivial classes yourself.

I don't see the point.

Once you get used to the odd syntax and understand its meaning, having functions defined in your structs is a powerful feature. It adds a lot of clarity to some of those massive overloads of common function names I mentioned earlier.

Having constructors and destructors called automatically at the right time is another powerful feature, as is the ability to redefine the assignment operator or other basic operators.

OOP is a very useful tool. OOP is only a handicap in the hands of OOP fanatics who want everything to be strictly OOP.

Many of my C++ classes have public data members. That was absolutely forbidden in the first group where I did C++ programming. There are a lot of situations in which the extra layer of an accessor function is very helpful between the data and the places that data is used. Usually you don't know you'll want that extra layer until later in project evolution, by which time it is very hard to add. So fanatics forbid ever accessing anything without the extra layer. An experienced programmer can make a better judgement call whether a given data item might ever need that and leave the extra layer out when it won't ever be needed.
Fanatics are not good.

OOP paradigm, especially the whole 14 figuratively speaking levels of inheritance makes understanding of the whole problem being solved quite difficult - as I many times wrote here it's very similar to having patch in patch on patch ( http://linux.die.net/man/1/patch ). The utility is quite useful, but expressing code as base and a chain of deltas doesn't help understanding.
 
Old 12-23-2012, 09:36 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Sergei Steshenko View Post
And that subset might well be "C" .
My discussion of C strings and scanf and arrays was intended to preemptively disagree with the fairly common fallacy that C is the best subset of C++
 
Old 12-23-2012, 09:47 PM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Sergei Steshenko View Post
OOP paradigm, especially the whole 14 figuratively speaking levels of inheritance makes understanding of the whole problem being solved quite difficult
Is that 14 a reference to something specific, or just a big number?

Template steering is very messy in C++ (I understand it is better in C++1x, but I can't use that at work, so haven't paid it much attention). Sometimes several extra layers of inheritance are used to get around template steering complications. So I have seen code with inheritance more than 14 layers deep. I think I've even seen some of that without involving templates.

I'm sure having many levels of patch makes a code base frustrating. But having many levels of inheritance usually isn't inconvenient.

Much of what I hate about OOP fanatics and the paradigm derives from the whole "is a" vs. "has a" discussion. OOP fanatics will tell you that X should be a base class of Y in exactly the cases where you might say of the real world concepts represented by those classes: "Y is an X", while X should be a data member of X exactly when you would say "Y has an X". Try applying that rule to real software engineering and you quickly find it is nonsense.
 
Old 12-24-2012, 06:07 AM   #9
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
The only thing I will say is if you like OOP, obviously use C++. If you don't like OOP (like me), stick with C.
 
Old 12-24-2012, 07:13 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
My discussion of C strings and scanf and arrays was intended to preemptively disagree with the fairly common fallacy that C is the best subset of C++
What you claim to be fallacy to me is a fallacy by itself. Somehow I don't remember it be that difficult to understand decades ago (I learned "C" in mid eighties) how strings in "C" work and how they are implemented.

And learning this helps to understand how things work under the hood - which might be valuable.

OTOH, since lately most of the code I write is in Perl and GNU Octave, gory details of string implementation are hidden from me, and I am not missing the details.

As I many times wrote in this forum, since by education I am a radiophysicist (physics, electronics, RF, microwave, lasers), and since I designed digital chips (VLSI) I pretty much understand things from transistor level and up. And I prefer hot to have holes in that understanding.
 
Old 12-24-2012, 07:58 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
Is that 14 a reference to something specific, or just a big number?

Template steering is very messy in C++ (I understand it is better in C++1x, but I can't use that at work, so haven't paid it much attention). Sometimes several extra layers of inheritance are used to get around template steering complications. So I have seen code with inheritance more than 14 layers deep. I think I've even seen some of that without involving templates.

I'm sure having many levels of patch makes a code base frustrating. But having many levels of inheritance usually isn't inconvenient.

Much of what I hate about OOP fanatics and the paradigm derives from the whole "is a" vs. "has a" discussion. OOP fanatics will tell you that X should be a base class of Y in exactly the cases where you might say of the real world concepts represented by those classes: "Y is an X", while X should be a data member of X exactly when you would say "Y has an X". Try applying that rule to real software engineering and you quickly find it is nonsense.
"14" was just a big number.

In real life inheritance is multiple, so "is a" relationship is quite often irrelevant. And then a stupid question: "Whom do you love more - mammy or daddy ?" arises. I mean all those private/protected members in case of multiple inheritance.

In real life kittens typically look like their parents, and traits are inherited from the parents while in OOP figuratively speaking kittens (derived classes) are genetic anomalies of parents (parent class). I mean, derived class has something its parent class doesn't have.

...

I am not a computer scientist by education - as I wrote above. Still, it seems to me that OOP is an attempt to present relationships in a tree-like manner while in real life it's a more complex graph.
 
Old 12-24-2012, 08:29 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Sergei Steshenko View Post
I don't remember it be that difficult to understand decades ago (I learned "C" in mid eighties) how strings in "C" work and how they are implemented.
I learned C in the mid seventies and I also didn't have any confusion over C strings. I taught C and C++ to my four sons and none of them had any confusion over C strings. Several other people that I taught C to also had no confusion over C strings.

But most beginning C programmers are badly confused over C strings. That was true of my classmates at MIT in the 70's. It was true of my sons' classmates, just a few years ago. It was true of surprisingly many junior, but professional, C programmers my then employer hired in the late 80s. It is solidly demonstrated by the beginning C questions and problems posted here at LQ. Even a few people that I taught C to myself were confused by that.
 
Old 12-24-2012, 08:38 AM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by H_TeXMeX_H View Post
The only thing I will say is if you like OOP, obviously use C++. If you don't like OOP (like me), stick with C.
That was also a point of view for which I preemptively (above) explained my disagreement.

I think the subset of C++ in which you do zero OOP yourself is a better language than C. When you use std:vector or std:string or C++ IO, there is OOP under the hood, but you don't need to be aware of it, just as in using the C library there is likely some embedded asm under the hood but you didn't need to learn embedded asm to use the library.

I prefer to use some of the OOP features of C++ fairly early, even for a beginner. But if you really don't want to code OOP, there are still a lot of reasons to prefer C++ over C.
 
Old 12-24-2012, 12:00 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
I learned C in the mid seventies and I also didn't have any confusion over C strings. I taught C and C++ to my four sons and none of them had any confusion over C strings. Several other people that I taught C to also had no confusion over C strings.

But most beginning C programmers are badly confused over C strings. That was true of my classmates at MIT in the 70's. It was true of my sons' classmates, just a few years ago. It was true of surprisingly many junior, but professional, C programmers my then employer hired in the late 80s. It is solidly demonstrated by the beginning C questions and problems posted here at LQ. Even a few people that I taught C to myself were confused by that.
Well, if you want to have a look at bigger picture, you should probably read what you'll find using https://duckduckgo.com/?q=DDDoA.sml.pdf . I am still reading it.

But it's a much much much much bigger picture ...
 
Old 12-24-2012, 12:08 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
That was also a point of view for which I preemptively (above) explained my disagreement.

I think the subset of C++ in which you do zero OOP yourself is a better language than C. When you use std:vector or std:string or C++ IO, there is OOP under the hood, but you don't need to be aware of it, just as in using the C library there is likely some embedded asm under the hood but you didn't need to learn embedded asm to use the library.

I prefer to use some of the OOP features of C++ fairly early, even for a beginner. But if you really don't want to code OOP, there are still a lot of reasons to prefer C++ over C.
Regarding item in bold - you are both right and wrong.

C++ is objectively better than "C" because it's more strictly typed.

OTOH, using standard C++ features like std:vector or std:string gives the illusion, to put it metaphorically, that something is free while in reality it isn't.

Probably a solution is to study simultaneously an assembly and a high level language. For the former one can take something simple like 8051 and use its computer model (at command level) - so there will be no need top buy real HW. In such a case assembly + C++ is a good pair.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Torn between distributions ReaperX7 Slackware 39 08-01-2012 02:50 AM
"Visitor messages": conversation becomes torn apart, which is not good ErV LQ Suggestions & Feedback 0 11-18-2008 11:42 PM
Torn between two distros Justin2021 Linux - Software 4 05-17-2006 07:02 PM
LXer: Bringing software freedom to war-torn Burundi LXer Syndicated Linux News 0 01-13-2006 11:46 PM
GConf, a torn in my... floppy drive. vexer Slackware 1 06-19-2003 11:13 AM

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

All times are GMT -5. The time now is 10:28 PM.

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