PDA

View Full Version : Learning C/C++



0wn 4 skill
11-11-2008, 03:06 PM
Firstly i would like to appologise if this is in the wrong section.

Anyways, I was wondering how I should go about learning C/C++ and what are the best things to do to get me started? Im tottaly new to C/C++ so any comments would be greatly appreciated! :)

Thanks in advance! :D

boberman
11-11-2008, 03:29 PM
in many ways, c is a lot like pascal, so if you know pascal then learning C will be more of a syntax problem.

The most important feature of C++ is OOP (object oriented programing), so I would suggest studying OOP design, the shift from "Do this, then this, then this" to "We have this object and this object, how do they interact with each other?" is a big one.

Heres a programming list in order of difficulty. once you can program (and understand) these programs, you should be pretty proficient at C-C++ programming.

C.
1. Hello word program (goes without saying :P)
2. Fibonacci number calculator
3. Calendar program (IE enter a year and a month and display a calandar for that month.)

C++

4. An address book. Use objects to represent book entries.
5. A banking program. Use objects to represent the bank, and different accounts. Create your own different types of accounts (for example, a checking account may not accrue interest, but a savings account will. A loan may not support a withdraw but it can be deposted into, ect). Use polymorphism to make these different accounts.
6. A binary search tree. each node represents a value and has a left and a right child. The left child is always greater then the parent and the right child is always less then the parent.

If you can do these out of order, great! If not, just do them in order and complete one at a time to get the concepts down. Once you can make all of these programs, you should understand the basics of c and C++.

If you do them, post your results here (or in new threads) and Ill be happy to provide insight, along with other, of what should be changed or what is done good. It might help others to do better in their programs as well.

0wn 4 skill
11-11-2008, 03:44 PM
Thankyou very much! :D Would it be worth me buying any books etc? Or is there a tutorial I can read?

Also:Whats a Fibonacci Calculator?

boberman
11-11-2008, 04:03 PM
Thankyou very much! :D Would it be worth me buying any books etc? Or is there a tutorial I can read?

Also:Whats a Fibonacci Calculator?

Buying a book is worth it. I started off with "C++ for dummies", I don't have a lot of programming books that I have read through so I don't have many good recommendations :(.

Fibonacci number n is equal to fibinocci number (n - 1) + fibinocci number (n - 2); with fibonacci number 0 being equal to 0 and fibonacci number 1 equal to 1. So the pattern looks as follows.

0, 1, 1, 2, 3, 5, 8, 13, 21, ect...

0wn 4 skill
11-11-2008, 04:08 PM
Ok, well thankyou Boberman! :D I will procede with my ambition :p lol

Hello World :D :


#include <iostream>
using namespace std;

int main(void)
{

cout<<"Hello World! Well, this is my first C++ Program lol. Ownage";

int f;
cin >> f;
}

ShowerThoughts
11-11-2008, 06:09 PM
Ok, well thankyou Boberman! :D I will procede with my ambition :p lol

Hello World :D :


#include <iostream>
using namespace std;

int main(void)
{

cout<<"Hello World! Well, this is my first C++ Program lol. Ownage";

int f;
cin >> f;
}

:confused: I can't get it to compile :confused:

lol

0wn 4 skill
11-11-2008, 06:20 PM
What compiler you using?

boberman
11-11-2008, 06:37 PM
Ok, well thankyou Boberman! :D I will procede with my ambition :p lol

Hello World :D :


#include <iostream>
using namespace std;

int main(void)
{

cout<<"Hello World! Well, this is my first C++ Program lol. Ownage";

int f;
cin >> f;
}

:). You probably should end your cout with a newline, that can be done like this,

cout << "Hello World ...\n";
or this
cout << "Hello World .." << endl;

Also, while not so important in this program, you introduce the possibility for a buffer overflow by doing cin >> f. where f is an int. You should use a string instead to avoid that problem. (and not a char*, but an actual string object)

One more thing, Don't forget to include indentations. Believe me, proper indentation will save your life some day!

You should also return something from your main. 0 is generally what everyone returns, it means your program finished successfully. By not returning anything, the OS doesn't know if your program failed or not. This can be important in some cases.

@Hermpie
You need to run it through a C++ compiler like g++. your command line should be something like "g++ file.cpp"