New to C++, need useful examples [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am new to Stack Overflow, and new to programming.
I am learning how to program in C++.
My question is not related to specific code, but is about research and learning the language.
What I have learned so far relates to narrow examples of syntax and simple programs which use variables, functions, arrays, etc.
I am wondering if people have or can link to example programs so I can study them.
I'm looking for console programs which:
use variables (int, double, string, etc)
use functions
use arrays use classes with
attributes and methods use objects of that class
reads and writes to a file
validates user inputs, displaying appropriate error messages
and is basically a useful program.
Through Google, I have mostly only been able to find C++ tutorial pages (cplusplus, cprogramming, etc) which deal with each of the above separately, usually in a bare-bones way to show the syntax. I'm looking for something more complex (but not overly so) so I can learn how to combine these things in a meaningful way with the intent of eventually writing programs of my own at the same level.
I've already coded a calculator (though not one that has all of these features; namely it was missing file i/o and I was able to make a basic one which didn't need objects), so I'm looking for something different. I understand console programs are text based and lend themselves well to these kind of programs, so it can be a calculator of another type, as long as it isn't a basic arithmetic one.

People here won't teach you C++. In fact, even a book or Google by themselves won't exactly teach you any language, they are just tools to make your life easier and the studying curve smoother.
My suggestion is to use Google or a good C++ book and write code.. especially write code, otherwise you won't learn anything, you must get your hands dirty in order to learn C++.

cplusplus.com has a few examples.
As #GMan said, you'd be better off reading a book.
Possibly Effective C++ by Scott Meyers, or maybe one in the Beginner\Introductory section.
The best way to improve is to give yourself a task and code it. Use different techniques/paradigms (OOP, modular, etc). Instead of studying programs, try to create them yourself - you'll learn a lot better this way.
The book can guide you, but you must make the journey.
Here are some exercises. You can try solving puzzles, too. CodeGolf.SE is good if you want to have some fun.

I hope you've got an excellent book. :)
That said, I understand the desire to find source code you can read that's larger than toys, but not giant cathedrals of code; perhaps the AppArmor policy parser can be of service to you. It's a little bit involved, because it's a small-language compiler that builds a DFA of a security policy for 'execution' in the kernel when confined programs perform file operations.
You can clone it with bzr: http://bazaar.launchpad.net/~apparmor-dev/apparmor/master/ or you can browse the source code: http://bazaar.launchpad.net/~apparmor-dev/apparmor/master/files/head:/parser/libapparmor_re/

Supplementing the other posts directing you to various reading, if you are new to programming, I highly recommend starting your venture into programming with Python.
Python is an easy-to-learn programming language that has a LOT built-in and will allow you to start making useful "programs" very quickly.
For example, you can read the entire contents of a text file with a single line of code:
file_contents = open('example.txt','r').read()
I feel it keeps people more encouraged when they can see significant results as they learn. If you are interested, Dive Into Python is a very popular tutorial.

Related

Start with some C++ framework or python [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I am thinking about to learn new language or framework. Now I deal with C# and WPF, WCF, Winforms. I have some free time so I would like get new skills.
But I have dilema, start with some C++ framework (such as Platinum, Reason, Evocosm, ACF)
or try Python / python framework.
I you are on my place which possibility you choose?
I am 17 years student, I have basic skills in C++ (OOP, little with STL), with Python I haven’t any experience.
What would be your choice and why?
Python is incredibly easy to learn, very powerful, and very useful (especially for web development IMO).
C++ is very widespread and is similar to other languages like C# and Java. Python's syntax is quite unique and AFAIK is not very similar to any other languages. Nonetheless, Python syntax is far easier to read and cleaner to write than C++.
Python is also fairly widespread and is used by huge companies like Google.
C++ has some wonderful libraries that can really help development, like Boost and Qt. Python also has excellent libraries like Django (for web development) and Twisted (for networking stuff). I find that C++ libraries tend to be broader in scope than Python libraries do, but the Python standard library is far better than the STL, IMO.
Python recently had a backwards-incompatible release (Python 3), and most libraries haven't caught up yet, so you might have to use Python 2 to take advantage of some of Python's libraries.
C++ is a lot faster than Python in terms of execution speed, but Python more than makes up for it in terms of development speed. Plus, Python allows you to write performance-critical parts of your application in C or C++.
Overall, I'd say go with Python, mostly because it's easy to learn and use, and makes programming simple and fun.
I recommend you to keep learning C++. Before you started looking for framework learn some popular search algorithm and try implement them. After that try implement some structures, like queues, list, stack, binary trees and some operation on them. Meanwhile play with I/O (for example, try write your stack to file and read it back to stack - in plain text and binary).
It was my university tour on programming class. C++ is good choice because it is hard and multiparadigmats language so in future u will find much in common with other languages and you will be familiar with memory management system.
Personally I prefer Python, but profesionally, that is if you want a good job C++ is a better choice.
I'd pick C++ for the sole reason that it's nothing like the languages you already know, even though it shares some syntax.
I strongly recommend you at least learn a bit of Python first, even if you decide stay in C++.
One may say C++ is the most powerful program language.
However, for a starter, there are too many traps and pitfalls.
In your situation, Python is something that can let you know more about programming.
Since you have no experience in Python, It's very easy to learn at the beginning.
And it will show its effect very quickly.
Plus, learning it will help you understand C++ more, like STL and Boost, they all have similarities with Python. Qt even has PyQt as a Python bindings. They have the same API, while the syntax in Python is clean and elegant.
Here is a remarkable tutorial for Python: http://diveintopython3.org/
I suggest you just try it a bit, it wouldn't hurt.
Well. Learning Python basics will take a week, and you will save the time spent in a year since it is such a good language for small hacks and scripts. So I suggest you learn it first.
Learning C++ well will take you five to ten years, so there is not the same immediate benefit :)

C++ | Progresssion Path [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I know the basic OOP-related topics, RTTI, Templates. Reverting back from Java' Collection Framework, I tried to find such collections in C++ and found STL, and am trying to use it in my projects (although I don't know them in and out). I searched and found recommendations for books like Accelerated C++, Effective and More Effective C++.
But I am not sure what should be my progression path. I am looking for something like this -- Python-Progression Path:
def apprentice():
read(diveintopython)
experiment(interpreter)
read(python_tutorial)
experiment(interpreter, modules/files)
watch(pycon)
def master():
refer(python-essential-reference)
refer(PEPs/language reference)
experiment()
read(good_python_code) # Eg. twisted, other libraries
write(basic_library) # reinvent wheel and compare to existing wheels
if have_interesting_ideas:
give_talk(pycon)
def guru():
pass # Not qualified to comment. Fix the GIL perhaps?
Discover list comprehensions
Discover generators
Incorporate map, reduce, filter, iter, range, xrange often into your code
Discover Decorators
Write recursive functions, a lot
Discover itertools and functools
Read Real World Haskell
Rewrite all your old Python code with tons of higher order functions, recursion, and whatnot.
Annoy your cubicle mates every time they present you with a Python class. Claim it could be "better" implemented as a dictionary
plus some functions. Embrace functional programming.
Rediscover the Strategy pattern and then all those things from imperative code you tried so hard to forget after
Haskell.
Find a balance.
It's a tough question, because what you really need is becoming good at what you do, and thus no authoritative list exists.
That being said...
Read Effective C++ by Meyers and C++ Coding Standards by Sutter, you're not likely to understand everything if you're a beginner, so re-read them from time to time (it's also a good vaccine)
Time to introduce the STL (it's an amazing little pearl), learn to use its algorithms instead of hand-crafting everything, if possible jump straight to the C++0x version
Incorporate Boost into the mix, softly at first: boost::optional, boost::variant, boost::lexical_cast, boost::numeric_cast make your code safer and more idiomatic. Also poke the Boost String Algorithms library.
Template Meta Programming and Boost.MPL are next: C++ Template Meta Programming by Abrahams Gurtovoy will help there. You might have to leverage Boost.Preprocessor for some template stuff.
Learn more Boost Libraries, it's a gigormous repository and it's amazing all the libraries there are.
I am still at that last part myself, so cannot comment on going further :)
At each step, you should write a lot of code, reading isn't sufficient, you need to experiment. Programming is not just technic, the architectural part of the program is extremely important in the field.
Oh and try and join (if only to read) an open-source project, nothing beats writing code and it's better when someone else reviews it :)
First get really good with C. I recommend "Deep C Secrets" by Peter van der Linden.
Get a copy of "Effective C++" by Scott Meyers, and commit it's advice to memory ("More Effective C++" is also pretty good).
Read "Advanced C++" by Coplien.
Read the The C++ FAQ.
Read "The C++ Programming Language" by Stroustrup at this point.

Good precautions (practices) to start C++ programming [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I'm starting C++ programming in my first job. I'm a CS student and I have learn programming in Java. What advice would you tell me to watch out so I don't cause trouble in my new job?
Would you have any advice or references would be appreciated.
(example: I know C++ is more likely to have memory problem than Java)
Thank you very much!
Maybe you already know this, but one common mistake for folks used to Java and learning C++:
Don't use new unless you have to (and you probably don't really have to). In most cases where you want to create an object, you should just create it "on the stack", like ClassType obj;.
Two things:
Get yourself a copy of Effective C++ by Scott Meyers.
Lint your code. This will not only catch potential bugs early in the development process, but also teach you good coding habits (e.g. declaring a method's input arguments as const references). I use PC-Lint for this.
For me, the biggest difference between Java and C++ is pointers, so I would try to get pointers down. Just my opinion.
No two ways about it. You're going to be a menace until you've been bit enough by C++ gotchas to hate the language. I'd recommend trying to write functions and not classes. A lot of people think OOP is great, but really most users want your code to "do" something, not "be" something. Anyways, good luck. :-)
Definitely what Chance said. Memory management is explicit in C++. I had an excellent textbook in college called Deitel C++. It's a C++ Bible.
I would also learn up on the Standard Template Library and Boost.
This might give you some ideas.
Some important excerpts :
Researchers (Bloom (1985), Bryan & Harter (1899), Hayes (1989), Simmon & Chase (1973)) have shown it takes about ten years to develop expertise in any of a wide variety of areas, including chess playing, music composition, telegraph operation, painting, piano playing, swimming, tennis, and research in neuropsychology and topology. The key is deliberative practice: not just doing it again and again, but challenging yourself with a task that is just beyond your current ability, trying it, analyzing your performance while and after doing it, and correcting any mistakes. Then repeat. And repeat again.
And here is how to do :
Get interested in programming, and do some because it is fun. Make
sure that it keeps being enough fun so that you will be willing to
put in your ten years/10,000 hours.
Program. The best kind of learning is learning by doing. To put it
more technically, "the maximal level of performance for individuals
in a given domain is not attained automatically as a function of
extended experience, but the level of performance can be increased
even by highly experienced individuals as a result of deliberate
efforts to improve." (p. 366) and "the most effective learning
requires a well-defined task with an appropriate difficulty level for
the particular individual, informative feedback, and opportunities
for repetition and corrections of errors." (p. 20-21) The book
Cognition in Practice: Mind, Mathematics, and Culture in Everyday
Life is an interesting reference for this viewpoint.
Talk with other programmers; read other programs. This is more
important than any book or training course.
If you want, put in four years at a college (or more at a graduate
school). This will give you access to some jobs that require
credentials, and it will give you a deeper understanding of the
field, but if you don't enjoy school, you can (with some dedication)
get similar experience on your own or on the job. In any case, book
learning alone won't be enough. "Computer science education cannot
make anybody an expert programmer any more than studying brushes and
pigment can make somebody an expert painter" says Eric Raymond,
author of The New Hacker's Dictionary. One of the best programmers I
ever hired had only a High School degree; he's produced a lot of
great software, has his own news group, and made enough in stock
options to buy his own nightclub.
Work on projects with other programmers. Be the best programmer on
some projects; be the worst on some others. When you're the best, you
get to test your abilities to lead a project, and to inspire others
with your vision. When you're the worst, you learn what the masters
do, and you learn what they don't like to do (because they make you
do it for them).
Work on projects after other programmers. Understand a program
written by someone else. See what it takes to understand and fix it
when the original programmers are not around. Think about how to
design your programs to make it easier for those who will maintain
them after you.
Learn at least a half dozen programming languages. Include one
language that supports class abstractions (like Java or C++), one
that supports functional abstraction (like Lisp or ML), one that
supports syntactic abstraction (like Lisp), one that supports
declarative specifications (like Prolog or C++ templates), one that
supports coroutines (like Icon or Scheme), and one that supports
parallelism (like Sisal).
Remember that there is a "computer" in "computer science". Know how
long it takes your computer to execute an instruction, fetch a word
from memory (with and without a cache miss), read consecutive words
from disk, and seek to a new location on disk. (Answers here.)
Get involved in a language standardization effort. It could be the
ANSI C++ committee, or it could be deciding if your local coding
style will have 2 or 4 space indentation levels. Either way, you
learn about what other people like in a language, how deeply they
feel so, and perhaps even a little about why they feel so.
Have the good sense to get off the language standardization effort as
quickly as possible.

What does C++ add to C? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
What does C++ add to C?
What features of the language are the Clang/LLVM projects, the parts of GCC that are being written in C++, chromium, and any others all taking advantage of? What features are they avoiding?
Like all sophisticated and powerful things there is a price to be paid to succeed in C++.
You have to be incredibly careful with memory management.
Multi-paradigm capability means you have to be really good at design to avoid making a mess.
Extreme performance requires careful planning and selection of features used.
The ability to circumvent most every language policy requires monumental self discipline.
So if you're sloppy with memory, poor at design, don't need fast programs, or have no self discipline, then please don't learn C++. There is always Java or C#.
meta programming? templates?
like with C you get performance, but the code looks horrible.
with the high level languages you get nice code but there is less flexibility to make the fastest possible code.
with c++ you can do both? you can freely make anything as fast as it could be made in C, but native object orientation, and templates/operator overloading ect makes it so you can write fairly nice looking code too. indeed, you can make it so it is neat and fast.
I have never really found it more of a pain to write stuff in c++ than in a higher level language. the trick is having good libraries.
Because despite academic efforts such as Singularity, there's not a single mainstream OS where drivers can be written in a high-level language.
Note that anything that can be done in C++ can also be done in C, but some things are a lot easier in C++.
Not? I would say it's not worth if you performance is not an issue for you. (Follow the double negatives.)
My two cents:
Although I don't program in Python, I would have to say that Python is probably the best programming language for getting real work done. It's an elegant language, and it has an enormous collection of libraries for doing various things. However, my experience as a user has shown me over and over again that Python is slow (take yum, for example).
I do know Haskell pretty well, and I have to say that it's a friggin' awesome language. Better yet, it is compiled, and its speed is competitive with Java and C++ (though you have to put forth extra effort to get this speed in some cases). However, libraries for things like database access don't always match the elegance of Haskell's base libraries (I'm probably way wrong about this), and they're harder to install on Ubuntu. In my opinion, that's why it's more challenging to get real work done in Haskell than in Python.
Ruby's good for web applications. Other than that, it's slow (though I speculate jRuby or something might be faster).
C++ is far from elegant, and in many cases, elegance is frowned apon. Anyone ever told you to use static_cast instead of C-style casting? Anyone ever told you not to use namespace std;? C++ has a lot of features, but doesn't tend to have many important language features (such as closures, which are formally proven to be the best thing since sliced bread).
Why do people use C++, then? Well, it's performance-focused, making it a good choice when you need speed. It has classes, namespaces, and templates, so it's a good choice when you want better code organization, but still need to use "C" for some reason. Also, it has the Boost library, which I've heard is really good for getting work done.

If you only have a hammer...or [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 13 years ago.
I need to download some csv files over http from the internet, parse it and convert it to a more useful fomat. Eventually a C++ program will consume the data. A few years ago, I would be pulling out my Perl books and start writing Perl scripts to do the downloading and parsing. But now with Boost and Qt I can do the download, parsing, and throw in a GUI front end in C++ with very little effort. Last time I wrote Perl/Python was about 6 months ago. It will probably take me longer to do it in Perl/Python and my Perl/Python code will be crap. If the only tool I have is a hammer, everything looks like a nail? Or time has changed and C++ can be productive in area traditionally dominated by script languages such as Perl or Python?
What does it matter what other people might usually expect the solution to be? If you can get the work done better and faster in C++, do it in C++, end of story.
For your example it seems that you will be more productive and able to solve your problem more easily using C++ as that is what you know.
But generally I would expect that more people would use python/perl for this kind of task.
Why not?
If you're code is going to be hacky because of lack of libraries/features, then C++ might not be best.
If it needs to change often, then C++ might not be best.
If others who know Perl/Python will maintain it, then C++ might not be best.
etc.
Do you really need a GUI? Do you need the speed? Nothing has changed to make C++ a scripting language, but that doesn't mean you shouldn't use it.
If it passes common sense tests like this, than why not?
Well, if you have to build a house, a hammer will work just fine. But building a house using a pneumatic framing nailer is much easier.
C++ is a fine language and you can be productive in areas that are dominated by scripting languages, but all things equal, you will be more productive using perl/python for text parsing.
The issue of productivity of C/C++ vs. python/perl seems irrelevant to me. If you want to write your parser using Qt, Boost, and any other off the shelf tool sets, do it. You didn't actually specify if the speed of parsing is an issue, but even if it was, would it even matter for your case.
Even if you think that it might be easier in language X, write it in language Y if you want to try and learn something new about that language. It sounds like a pretty easy task so just write it how you want to write it.
You also need to think about future use. If this program will need to be enhanced and extended in some way, that might dictate a specific language choice.
If you're more productive in C++ then by all means use C++. It's still a good idea to learn other languages, but sometimes you need to go with what you know to get things done.
BTW, you probably already know this, but the Boost.Tokenizer library has CSV parsing capabilities built-in through the escaped list separator.
From your description, it sounds like you have other tools besides a hammer. The more tools you have, the more efficiently you can get things done. Use whatever tools you have at your disposal to get the job done, and occasionally buy (learn) more tools.
I like C++. I hate the compile, run, test, close cycle - specifically when I need to test data. My solution: I made a small programming environment [powered by Lua] which I embed in my C++ applications and I open it at run-time and do stuff interactively. Why to stick at one programming environment or another? Use the best of both worlds.
C++ was not specially designed to handle long strings of text. (Just look at the old C-Strings... they were a nightmare for anything serious.) By contrast, Perl was made for this sort of use.
You can probably cruft together something in C++, but a Perl solution will probably be more robust and maintainable.