As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I'm currently learning C++ and would like to start reading others sourcecode to pick up tips. I was wondering if anyone has examples of well written C++ that I can take a look at (and not pick up bad habits from)
As you've probably discovered, there are many Internet sources for examples of C++ code. StackOverflow, CodeProject, etc. all have examples of source. But of course, they're all over the map in terms of quality and educational value.
I think, in this area, books still trump the Internet. There's no substitute for going to the bookstore, thumbing through a few tomes, and picking one that you find readable. They've been proof-read (unlike may Internet submissions), so the samples are more likely to work and to be of high-quality.
While the books typically have small snippets of code or a few functions, they usually come with a CD or URL that points you to more full-blown examples.
O'Reilly, Thinking in C++, Petzold, and Wrox (and more) all have good books on C++.
The Boost library? It is generally considered some of the highest quality C++ code written. (A lot of it is also more or less unreadable unless you're a C++ guru yourself, though)
In general, I'd advise against you to be cautious with this approach though. In C++, the source code probably won't be as informative as in many other languages.
If you see some Python code, you can pretty much assume it's correct as long as it runs. If it's written by someone who seems to know what they're doing, you can even assume that it's well written.
In C++, there are just so many nasty pitfalls and subtle exceptions to every rule that you really need to know what you're doing. Going by what compiles, or what seems to work, or what you saw in someone else's source code is dangerous, and pretty much guarantees that you'll sooner or later end up with a program that relies on undefined behavior, and will crash when you least expect it.
If I were you, I'd try to stick to books. There are some very well written ones, which in addition to letting you see some source code, also teach you the langage "properly". And as long as you stick to reputable authors, you're ensured that they won't teach you any bad habits or plunge you into nonportable code.
In my opinion if you read a good C++ book (like "The C++ Programming Language", "(More) Effective C++" or "Exceptional C++").
You will not only learn good practices but should also get sense of how to write code.
Of course the samples in these Books are mostly artifical. If you read 'real-world-applications' you will always encounter pieces of code which are pretty ugly, but sometimes there wasn't just a nice clean solution for it (or a not-so-clean solution was just more efficient in terms of speed).
So I don't know if it's best to start with real applications since they can also be very overwhelming due to the amount of codesize and complexity, whereas sample codes in Books are compact and clearly laid-out.
I think for starters you would be best off reading such references as the books I have listed.
If you have to be flexible at some point in the future and have to produce ugly code, you will at least know that it's ugly code and not mistake it for "that's how it should be" ;)
Reading through open source software can be very educational if you already have a little bit of knowledge of C++. If you're just starting out, I imagine the Boost libraries will go right over your head. You could start with the WebKit project, which is an excellent resource. Also Google releases a lot of their source code, which also happens to be VERY clean: Protocol Buffers is a great example. And while you're at it, you may as well read their C++ Style Guide as well.
Personally, I started learning C++ by picking up a couple of books and writing some little challenge applications. Bear in mind that learning a language as extensive as C++ takes a long time, like 10 years long.
Many years ago, I was told that if I ever wanted a career as a C++ developer, I should definitely read Effective C++ by Scott Meyers. That should prevent you from falling into many of the language's pitfalls. If you find you need something even simpler, start with The C Programming Language by Kernighan & Ritchie. Be patient and good luck!
If you want a complete project to browse through, I would recommend Ogre3d. It is a well structured graphics engine with decent documentation and the source code itself is nice, too.
How about: Programming: Principles and Practice Using C++ by Bjarne Stroustrup?
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In my search for a good, freely available resource that will teach me C++ I stumbled on http://www.learncpp.com/.
My question is for intermediate to experienced C++ programmers...
Does this site seem to be a good resource for a beginner to learn C++ from?
I've gone through the first few section of the site, and I feel like I am starting to grasp the language, but being a beginner in C++ I really could be learning things all wrong and have no idea.
EDIT: After doing some research on this topic, and reading the answers and comments here, it seems like if I do the tutorial, read Accelerated C++, and then read Effective STL... I'll be on the right track. Of course, I'll be writing programs to solve project Euler programs and such. Do any of you have a better idea for a beginner with intentions to become competent in the language?
The site does not look too bad.
However it really is a tutorial, in that it just explains the very basic concepts of C++.
Notably, it completely misses an introduction to the STL and the proper use of it. You barely see std::cout and std::string. There's no mention of <algorithm> that I could see of and no mention of the <vector> or <deque> or <map> which are the most commonly used containers in C++.
It may not hurt you to learn with this site, but you'll only have a very basic level when you're done with it.
If you want to learn C++ the right way I strongly recommend you purchase a copy of Accelerated C++. It is the best C++ book for a beginner without a doubt.
Although the few pages I looked at seem reasonable, any instruction on C++ that recommends the use of Hungarian Notation completely misses the point and is thus suspect.
This site doesn't look too bad, but I don't think it's great either. A great free resource to learn C++ is the book "Thinking in C++". It's simply outstanding and I heartily recommend it to you. The other thing I recommend you is to start writing some code in C++ right now! Project Euler is my favourite source for beginners programming tasks(especially if you like math ;-) )
When I was in the university I was using the http://cplusplus.com/. I was quite satisfied with everything there.
It doesn't hurt to start of learning a language through a tutorial, but keep in mind that a tutorial may not (will not) cover all topics of a language. There are always better things than tutorials, like a book and a compiler.
I would recommend that you base your study on a good book like The C++ Programming Language
(Third Edition and Special Edition) by the creator of C++ Bjarne Stroustrup.
It doesn't necessary have to be this book, it can be any useful book that other programmers have used and learned from. Books are also programmers tools.
I'm currently reading the C++ tutorial at learncpp.com and think it's great!
For a C++ beginner, like me, that is. You won't get all the answers, but do you really want that as a C++ beginner? I would say no. At least, I don't.
What you will get are lot's of short answers to questions about basic C++ concepts, that I believe will be helpful when you later learn about other aspects of C++.
As I said, I'm a C++ beginner, but I have used PHP for several years. I would't say I'm a PHP expert, but I'm not a beginner either, so I think I more or less know what I'm talking about when it comes to languages resembling PHP, like C++.
Yes.
The tutorials have a comment section, and the admin seems to be responsive to questions there. This indicates that there is some accountability for the material in the lessons.
I have been using C# for several years... Sometimes I have to work with legacy C++ code (some dated back to VC6, written 10 years ago), but I didn't understand the basic of C++ quite well, for example, how the header files work, and why c++ compiler requires the forward declarations,as well as the extern keyword. So I decided to find an online tutorial to learn some basics of C++, and this is what i found, learnCPP.com!
The tutorial is very well-written, in plain, comprehensible English, I hardly have to turn to the dictionary:-) (I am not an English speaker). Now I have been learning on this site for a couple of months, and I have to say everything I need to know to START programming in C++ can be found in this tutorial.
Admittedly this tutorial is not "an encyclopedia for the basic of C++", it does not include a dedicated chapter on unicode, or another chapter on the crt... but this tutorial is not meant to cover these topics, otherwise there will be much much more chapters in this tutorial.
Try and enjoy!
Learning by doing was the only thing which helped me to understand it. For example, if you want to learn templates and operator overloading, writing your own vector class which uses templates for the datatype of the elements and the length and operator overloading for all the operations as well as assignment will be an lengthy exercise which requires quite some nerves at first ;) But you will realize that you learnt how C++ actually works.
While I advice to write some small stuff as the example illustrated above, following the tutorial to learn the syntax and a the very basics probably isn't bad for a start.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In my early days of programming, before I started working professionally, I wrote a fair share of trinket/exercise apps in C++ and felt fairly confident that I know the language. Then, as opportunity came, I went to do real work and left the C/C++ world. For the past 5 years I've written tons of code in C# and have had scarcely any encounters with the C/C++ languages. Now, after spending some time on SO and teh interwebs, I see that what I thought I know as "C++" is actually a mix between C and C++, with large gaps in my knowledge about the more intricate workings of the language. At the same time I also realize that I am intrigued by the language and would like to learn it more.
Googling for "C++ tutorials" reveals a wealth of tutorials, but they are all aimed at complete beginners, spending a lot of time explaining basics that I'm already familiar with. I don't expect that there will be any tutorials made exactly for me, but are there some that have at least been written with an experienced programmer in mind, not spending a lot of time on trivial things, and discussing the finer points of the language and compilers?
C++ is too complex to be learned through tutorials, you could only scratch the surface that way. Especially the advanced usage of the STL (and templates in general) is usually beyond the scope of online tutorials.
Therefore, I would recommend books: Stroustrup's "The C++ Programming Language", and Scott Meyer's "Effective C++", "More effective C++" and "Effective STL".
You can skip the first few chapters of Stroustrup if you feel confident you know that part already, but it's the best book for going into the details of the language. There should be a law for every aspiring C++ programmer to read at least the first two of Meyer's books I mentioned.
Once you're through with that, check out "Modern C++ Design" by Andrei Alexandrescu for a real eye-opener for what templates can be used for.
Update: In reaction to a downvote eight years later (...), be aware of The Definite C++ Books List. My personal recommendations above hold true, but that list is community-driven.
C++ is a very old language so there are lots of good books available. I would recommend to read those books, rather than searching sites and blogs. My personal experience says in harder to find lot of good articles at one place, by most of the good articles are already in books.
Regarding sites:
Dr. Dobb's site and journal have lots of good articles.
Books:
Effective C++
Exceptional C++
C++ Object Model
Bjarne Stroustrup
Beyond the C++ Standard Library
I suggest, though rather as supplement, the C++ FAQ Lite. It has information ranging from basic things to really obscure C++ features and many tips to write good C++ and helped me more than once when some stupid template thing or whatever wouldn't do what I expect.
1.C++ Object model is nice book for professionals.
2.Design and evolution of C++ by bjarne stroustrup give you in-depth insight.
3.Scott Meyers' Effective C++ series is also nice one.
You've gotten a number of good recommendations, but none seems to have included the one that was really written to fit your situation. Accelerated C++ was written for people who already know the basics of programming, and just want to learn how to program in C++.
Workings of the language? What areas exactly do you think are worth improving? I have a slight trouble extracting it from your question.
One book that i always keep on my table and peek into frequently is C++ Primer by Lippman et al, 4th ed., ISBN 0-201-72148-1. This is not a tutorial, this is a reference. Whenever you have a question about the language itself, this is where you can find a short and precise answer.
If you want specifically a tutorial which aims at experienced C programmers, see Bruce Eckel's Thinking in C++. Available as free download on author's site and also printed through Prentice Hall.
At some point you will have learnt a great many things which you CAN do, from then on, you should learn what NOT to do. This is a long and painful part, and i don't have a precise recipy, but accept my sympathies. The deeper you get involved into complex stuff the more likely is it to bite you at some point due to conflicts between language features. I suggest taking a look at Imperfect C++ by Matthew Wilson, C++ Coding Standards by Sutter and Alexandrescu.
You probably want to skip the stuff you already know and move on to harder more fun stuff. I assume you already know a lot about the language and it's syntax, methods, classes, etc.
Where to start? Well, there are very good books and papers on the language. I can recommend many books on the language but you can probably find them using the stackoverflow search.
I can only tell you what you already know. You wont find a book that covers it all. You will have to browse through many books in order to get what you desire. Take a randomly recommended book, browse through it skip the stuff you know and try to fit it all together. Try to make something out of the fresh knowledge by writing some code to understand it better. Do it until you understand it fully.
Here is a brief tutorial for advanced C programmers to refresh your memory on the language.
Essential C++ by Lippman is a small but great book.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I need to read "A Practical Introduction to Data Structures and Algorithm Analysis" by Shaffer for class but the code examples in the book are all in C++ which I do not know. I know C and Java already and was wondering if you knew any resources that helped learn enough C++ to understand these examples fast if you already know another language. Thanks!
Another free textbook is The C++ Annotations by Frank B. Brokken. You can browse it online, or you can download the pdf version.
A quote from the first page:
This document is intended for
knowledgeable users of C (or any other
language using a C-like grammar, like
Perl or Java) who would like to know
more about, or make the transition to,
C++. This document is the main
textbook for Frank's C++ programming
courses, which are yearly organized at
the University of Groningen
What I like about "The C++ Annotations" is that is being kept uptodate, version 8.0.0 has
added C++0x chapters.
Yes, (the first edition of) Thinking in C++ taught me how to read C++ syntax; it was designed for C programmers and each chapter built incrementally onto my existing knowledge of C, which I found helpful (and it's now available in print or as a download). Many people recommend it.
I'm not familiar with the book, but any good Algorithms and Data Structure book should be understandable to anyone with programming experience, weather they understand the language the examples are in or not.
Especially, in your case where you already know Java and C, I can't see that you will have any problems following the algorithms just because they are written in C++
This is quite a textbook (and quite expensive, so see if your library has it), but I would recommend the man's book itself, the C++ Programming Language linky. I used it to enhance, rather than create, my understanding of C++, but I used it in the form of a dictionary, and it seemed to work out well. It is written for people comfortable with programming, and you've gotten the pointers AND the OO stuff down, so it may mesh well.
For example... Chapter 2, a Tour of C++ (as I have it open in my lap now), talks about a large number of things, many of which compare themselves to the "C" way of doing things. The things are not new to a Java programmer, but different syntax, etc. Basically, if you want to learn about C++ iterators, look up the iterators chapter, etc.
I think you'll do ok without necessarily needing a book, but C++ has ridiculously tricky syntax, (try figuring out how to assign a constant field in an object instance using the constructor, for example) and I found the book to be quite illuminating. Thankfully, there's a pretty nifty index including operators, which is quite helpful.
Lastly, if you want to be a guru (which I certainly am not), there are discussions about everything from "Exception-Safe implementation techniques" (Appendix E.3) to philosophy of developing large software projects.
So I've given you quite the advertisement (I've never met the guy, honest), but I've found the book to be quite useful.
See the following previous questions for online C++ learning resources:
https://stackoverflow.com/questions/45175/resources-online-to-learn-c
https://stackoverflow.com/questions/909323/what-are-good-online-resources-or-tutorials-to-learn-c
I'd recommend taking a look through C++ Primer Plus (5th Edition). What you probably need to get your head around is the syntax for:
the type system
templates
operator overloading
The basic syntax of C++ is usually fairly easy to get a hold of, however, C++ is a complex multi-paradigm language, which requires some serious study to use it effectively.
C++ Primer Plus (5th Edition) http://ecx.images-amazon.com/images/I/41YAKQF6BML._SL160_.jpg
I'm surprised no one has yet mentioned Accelerated C++. It's not based as much on your existing knowledge of C (or Java), but it will teach you the language as it stands alone.
I found 'C++' by Till Jeske, 2002, ISBN 0-201-75879-2 very good. It is not a pet killer, only 1.25 thick, and quite well written - concise and to the point. I knew some C++ before but Jeske's book really helped me.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I have a Phone interview coming up next with with a company which works in financial software industry. The interview is mainly going to be in C++ and problem solving and logic. Please tell me the method of preparation for this interview. I have started skimming through Thinking in C++ and brushing up the concepts. Is there any other way I can prepare?? Please help.
Edit:
Thank you all everyone for the advice. I just want to add that I am currently fresh out of grad school and have no previous experience. So Can you suggest some type of questions that will be asked to new grads??
Make sure you know your basic data structures and algorithms. You're more likely to be asked about that stuff than something higher up the food chain. Those are usually saved for the in-person interview.
Put another way: be solid with the fundamentals and solid with your C++ syntax. Also, knowledge of common libraries like STL and Boost couldn't hurt...but be sure you know what those libraries give you! In the end phone screens are there to cull out people who can't do the basics. Prove you can and you should move on to the next step. Good luck!
Here's some links of interview questions to check out:
C++ Interview Questions # DevBistro
C++ Interview Questions # Blogspot
C++ Interview Questions # FYI Center
Steve Yegge's Five Essential Phone Screen Questions (added this in response to your edit. This isn't C++-only, but a lot of it applies to C++ and I think would be a good read in your situation).
Now, for completion's sake, some books:
Scott Meyers "Effective" series (Effective C++, More Effective C++, Effective STL)
Herb Sutter's "Exceptional" series (Exceptional C++, More Exceptional C++, Exceptional C++ Style)
The C++ Standard Library by Josuttis
C++ Primer by Lippman et al
Stroustrup's text as a reference
I have interviewed several candidates specifically focusing on their C++ knowledge, and if there was one question that worked well to put peoples' knowledge of C++ on a gradient, it was this one:
Fix this memory leak as robustly as you can:
void doSomething()
{
Foo* pFoo = new Foo();
[do some stuff]
}
+1 for putting delete pFoo at the end
+2 for putting pFoo in a std::auto_ptr
+3 for knowing what RAII is - the concept, if not the acronym
+4 for mentioning exception-safety guarantees of the auto_ptr
+5 for putting pFoo in a boost:shared_ptr
+6 for knowing when a shared_ptr might not be freed.
+7 for talking about garbage collection techniques to fix circular references
This always worked to show how long someone had been working with C++. This is one datapoint you can use to tell where you are in the scale of C++ knowledge.
Edit: I would recommend someone for hire at level 3 or above.
Try some practice problems on TopCoder.
Check out Marshall Cline's C++ FAQ. Its a good way to learn some new stuff and bone up on the things you already know in case the decide to ask you some 'knowledge' questions as opposed to 'problem solving' questions.
Grab a knowledgeable friend and have them ask you some C++ programming problems that you can solve on a whiteboard. A lot of interviews will have you solve a problem on a whiteboard, and it can be disconcerting to think on your feet and write things out in front of someone if you are not used to it.
Even if they're interviewing for a C++ position not all questions may be specific to C++. For example, I've been hit with questions related to the following all in the same set of interviews for a single C++ position:
Algorithmic complexity of well known sort and search algorithms
Multithreaded programming
Multiprocess programming
Sockets programming
Software development philosophy / approach
Software test and validation philosophy / approach
Debugging
Benchmarking
Dynamic and static analysis of code (e.g. run-time memory leak detection vs compile-time)
In my case, the phone interview was part of a screening process to determine if I could take an online C/C++ knowledge test (e.g. through BrainBench). The online test results then determined if I would be flown out for on-site interviews, which also included more "hands-on" software development tests.
YMMV. A lot depends on what you claim on your resume, as well.
Interviewers often try to help you by giving you hints so that they can see if you can arrive at the answer they're looking for. Besides gauging your knowledge, they also want to see how you think. Occassionally you might get a crummy interviewer that is neither helpful nor positive. The key is to be confident in your abilities and be truthful.
HTH and good luck!
Something which I am starting to believe is that there is sometimes a clear divide between candidates that enjoy programming as a hobby versus those who consider it "just a day job".
Even if you don't know the answer to a specific question it is worth mentioning that normally you'd look up the answer on < your favourite resource > (eg. StackOverflow).
Based on your experience I don't think the interviewer will expect that you'll get every question right. They're most likely trying to decide if you've got "potential".
So relax and try to enjoy it!
Besides the obvious parts of the language, I've found that employers will want to see if you fully understand pointers, references, how copy-constructors come into everything, probably STL, and of course the basics of classes.
Read (or skim, depending on how much time you have to prepare) "Large-Scale C++ Software Design" by John Lakos. Chances are, you will need it.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I ran into this supposed interview of Bjarne Stroustrup, the inventor of C++.
http://artlung.com/smorgasborg/Invention_of_Cplusplus.shtml
Stroustrup: Well, it's been long enough, now, and I believe most people have figured out for themselves that C++ is a waste of time but, I must say, it's taken them a lot longer than I thought it would...
Interviewer: Yes, but C++ is basically a sound language.
Stroustrup: You really believe that, don't you? Have you ever sat down and worked on a C++ project? Here's what happens: First, I've put in enough pitfalls to make sure that only the most trivial projects will work first time. Take operator overloading. At the end of the project, almost every module has it, usually, because guys feel they really should do it, as it was in their training course. The same operator then means something totally different in every module. Try pulling that lot together, when you have a hundred or so modules. And as for data hiding, God, I sometimes can't help laughing when I hear about the problems companies have making their modules talk to each other.
Is this a hoax? Do any of these points seem true for any of the veteran C++ programmers out there?
You just have to check the Stroustrup's website (the FAQ part) to find that it's wrong - a well known hoax as Judah Himango already pointed :
Did you really give an interview to IEEE?
in which you confessed that C++ was
deliberately created as an awful
language for writing unmaintainable
code to increase programmers'
salaries? Of course not. Read the
real IEEE interview.
It's a well-known hoax.
And no, learning C++ isn't a waste of your time, something that's been discussed on StackOverflow many times.
As mentioned, this is a well-known hoax.
But it does provoke some interesting points. These days C++ is a waste of time, except for when you can't afford to waste time. Less opaquely: C++ is a waste of development time, except for when you can't afford to waste execution time.
From the article titled "The Real Stroustrup Interview" in IEEE Computer Magazine Vol. 31 Issue 6 pp.110-114 (June 1998):
For the past few months, a hoax interview between Stroustrup and Computer has been making the rounds in cyberspace. While we regret the incident, it offers us a welcome opportunity to have the father of C++ share his insights on Standard C++ and software development in general. We can also attest to his continued sense of proportion and humor—he suggests that the fictitious interview would have been a much funnier parody had he written it himself.
As others mentioned, this Interview is hoax.
Well, I am one of the persons who hate C++ and normally doesnt use it, but learning it was definitely not a waste of time. At least now I know why I hate C++ and I understand why other persons use this language and think it is good.
If you want to learn this language to know about its concepts, its benefits and its drawbacks, to be able to read code written in it, and in general to be able to "talk about" it, it is never a waste of time. Same for any other programming language. It will increase your expierience. For example, C++ shows one common way of OOP - a way I dont like, but a way many other people use.
But if you want to learn it because "the people say that it is the best" (as I sometimes read), then it is really a waste of time. Same for any other programming language.
Programmers that feel attracted to higher level languages that take care of memory management and other tasks for them, could feel that C++ is a waste of time.
It certainly is if you can achieve the same goal with another language in less time and with less bug fixing and don't mind the downsides as efficiency.
But I don't regret having learned and spent so many hours coding in C/C++ for it's such a beautiful language and allows you to produce things that not many other languages can.
I mean, don't you want to use the language with which operating systems and compilers are written? that's not a waste of time at all from my perspective.
C++ is far from being a waste of your time. You'll understand valuable concepts that will help you understand many other concepts in different programming languages. I.E.: VTABLE.
There is not a single framework which uses all language features of C++. This introduces a huge inconsistency to the language's ecosystem.
QT is one of the few APIs which I would call a framework (or API for a lot of things):
But it defines own string, own array, ...
What's the point of a "standard" library when no one can use it in a portable and compatible way (from the aspect of interaction with other APIs)?
I know, there is boost, but what is boost compared to an API such as QT? Nothing.
Look at Java: The is the standard Java API, and every "foreign" API uses it, it's all perfectly compatible.
C++ (and Java) probably the best language to learn to understand concepts of OOP.
I remember learning it in college benefited me a lot.
Stroustrup is not that stupid to say that! It is definitely a hoax!