0

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.

  • Well, I guess a good example is the source code of LLVM? –  May 20 '11 at 22:37
  • The source code of what? Source code is precisely what I'm asking for, but in the context of a useful application, not just demonstrating the syntax. Namely I want the to see what useful things others have written so I can learn from specific examples, not a generic tutorial. – turingmaschine May 20 '11 at 22:39
  • 4
    What you need is a good book, not code to stare at. We have a list [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). If you wanted to learn another language, you wouldn't ask for a book in that language (with a specific genre) and then try to read it, you wouldn't learn anything. You need someone to explain the language to you. You haven't mentioned a book, and I don't think you can be taught C++ without one, so that's what you should do. – GManNickG May 20 '11 at 22:41
  • 2
    @turingmashine The source code of LLVM. – alternative May 20 '11 at 22:41
  • LLVM is a complete project which does everything you asked for (except maybe validating user input, see Clang for that). –  May 20 '11 at 22:43

4 Answers4

2

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

mkj
  • 2,761
  • 5
  • 24
  • 28
snoofkin
  • 8,725
  • 14
  • 49
  • 86
  • given the particular features I talked about above, is there anything which comes to mind for a possible implementation, ie, a basic program which I can write which does that stuff? I'm trying to brainstorm ideas so I can start coding. – turingmaschine May 20 '11 at 23:19
1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • I think you should only read Meyers, when you already have some experience with C++ and not as an absolute beginner. But when you got some experience, you really have to read him. – Christian Rau May 20 '11 at 22:57
  • Okay, I'm going to look for a good book based on the suggestions here and the ones from http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list...however, given the particular features I talked about above, is there anything which comes to mind for a possible implementation, ie, a basic program which I can write which does that stuff? – turingmaschine May 20 '11 at 23:04
0

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/

sarnold
  • 102,305
  • 22
  • 181
  • 238
0

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Cryo
  • 817
  • 1
  • 7
  • 16