Be a Supporter!

C++: The Basics

  • 3,902 Views
  • 24 Replies
New Topic Respond to this Topic
JFriesen
JFriesen
  • Member since: Jan. 21, 2006
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
C++: The Basics 2006-03-11 19:29:49 Reply

Note! This post was made for the C++:Main topic.

==============
C++: The Basics
By: Joel Friesen
==============

Hello there. If you're here, you're probably interested in getting into the world of programming, and you've chosen the "language" C++. To start reading this book, you won't need ANY previous programming experience, all you'll need are a compiler, and an editor. I'll discuss those in a second. All C++ code examples I give you, will be italicized .

Computers have changed a lot in the past couple years. When computers were first made, programmers had to type code in machine language! It was long strings of 0's and 1's (binary code) to be able to make programs. Thank goodness you've decided to start now. At this point in time, those zeros and ones have evolved into languages like C++, in which humans can read and make sense of them. Its still not completely clear, but with some lessons, you'll soon be able to read C++ code.

Compilers:
To start, you'll need a "compiler". A compiler takes the code you've made, translates it into machine language, and creates a program (an Executable, something with the file extension .EXE). With this guide, we will use the free compiler, Bloodshed Dev C++ available here: http://www.bloodshed.net/devcpp.html

Object Oriented Programming:
Object Oriented Programming (OOP) is something that is supported by the language C++, but not by it's brother language, C. Up until OOP, programs were thought of as "procedural". Basically, it was broken down into functions which manipulated data. OOP creates "objects" rather than data. The program then manipulates the objects, which could be anything from a desktop widget, to a button onscreen. These objects can also have characteristics, such as a real life object may have wet, big, small, dry etc., these objects can have similar things. When we get into Classes, you'll understand better.

C:
C is a language very similar to C++, except C does not support OOP. You can learn C if you wish, but everything C has, C++ has as well.

Getting Started:
We are going to start off your C++ experience, by showing you how to create a program that writes out "Hello World!" to the screen. Open up BloodShed Dev C++, hit File, then New in the top corner, then hit Source File. It will open an editor. Copy and paste the following code into the editor, then we will analyze what each part of the code means:


#include <iostream>

int main()
{
std::cout << "Hello World!";
char response;
std::cin >>response;
return 0;
}

Now, hit Execute at the top and hit Compile and Run. Choose where to save your program, then, it will compile your program and open it. You should have a simple "shell" window on your screen that says Hello World!. Sorry it's not all fancy like programs you've seen, but you need to learn the basics before you can make fancy Graphical User Interfaces (GUI)!

Analyzing your First Program:
To successfully learn C++, you must first learn how your programs do what they do!
We'll start with the first line, #include <iostream>.
The # symbol in the code, starts a program inside your compiler called the preproccessor. Before your program is compiled, the preproccessor looks for any lines starting with # and acts upon them. The #include line, tells the preprocessor, that whatever follows it, is a file. Find that file, and put it in my program! The < and > brackets around iostream, tell the compiler to look for the file in all the usual places (the compiler comes preinstalled with files like iostream, so you can include them without having to write them!) We'll talk more about the preprocessor later in another section. The file iostream is needed in this program, because without it, the "cout" function wouldn't work, and cout is needed to print words to the console.

On line 3, you see your first function, int main(). Functions are the heart and soul of programs. They make what the program does, happen. Some programs say add 1 + 2, others perform very advanced tasks, and some functions call other functions! Every program MUST have a function called "main". Without it, programs won't compile. Functions are usually called or invoked, but main is special, so it is automatically called when your program is opened. All functions return a value. They have to, no matter what. When the word "int" is before main, it states that the function "main" will return an integer.
An integer is a type of variable, but I will discuss variables in another section. You're probably saying to yourself, where does main return an integer? Well, in one of the last lines, it states 'return 0;' This means return nothing at all, because in C++, 0 is equal to nothing! The brackets after main, hold things called arguements. The function main has no arguements, but more advanced functions will need them. Those will also be discussed in another section!

After the main line, the actual function starts. Function syntax says "Put the header at the start (our header was int main() ) and then, whatever comes in the brackets right below it, is part of the function that was stated in the header". So, the meat of the function is started with the { bracket, and ended with the } bracket. Whatever is in the brackets, is what the function holds. All functions have a header stating the return type, name, arguements, then right below it in brackets, is what the function does.

The 'std::cout <<' line, says, search in the library that come with the compilers, for the "library" of files, called 'std' and get the object "cout" from it. A '<<' symbol is then put after cout, and whatever is in the brackets after it, is displayed onscreen.

The
char response;
std::cin >>response;
section is not neccessary to explain right now, but you will understand it when we get into variables. Not all compilers need this line, but without it, Dev C++ only keeps the program up for half a second! Its varies from compiler to compiler.

Then, as I explained before, 'return 0;' returns nothing (equivalent to 0) and closes the function, with a closing } bracket.

The End for now!
Sorry, running out of characters, but part two of this basics guide will come soon! It will get more deeply into things like functions and variables, and clear some things you didn't understand here, up.

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to C++: The Basics 2006-03-12 02:39:36 Reply

Not bad! Did you post a link to this thread in C++ main?


Perpetually looking for time to return to the arts.

BBS Signature
DannyIsOnFire
DannyIsOnFire
  • Member since: Apr. 14, 2005
  • Offline.
Forum Stats
Member
Level 21
Movie Buff
Response to C++: The Basics 2006-03-12 05:30:14 Reply

At 3/12/06 02:39 AM, Johnny_Krysys wrote: Not bad! Did you post a link to this thread in C++ main?

Yeah, he did at the very top... although he didnt link to page 9999 but meh... itl be a while before we get there anyway =)


|| Portfolio || Facebook || Twitter ||

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++: The Basics 2006-03-12 05:37:39 Reply

At 3/11/06 07:29 PM, JFriesen wrote:

All functions return a value. They have to, no matter what.

....

void inverse(int * a )
{
*a = 1/*a;
}

that doesnt return anything :p


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
JFriesen
JFriesen
  • Member since: Jan. 21, 2006
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to C++: The Basics 2006-03-12 11:11:36 Reply

At 3/12/06 05:37 AM, -dELta- wrote:
At 3/11/06 07:29 PM, JFriesen wrote:
All functions return a value. They have to, no matter what.

....

void inverse(int * a )
{
*a = 1/*a;
}

that doesnt return anything :p

Heh. I'm sort of teaching the way I was taught, and I don't want to force things like that on them in a beginner lesson XD

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++: The Basics 2006-03-12 11:13:04 Reply

At 3/12/06 11:11 AM, JFriesen wrote:
At 3/12/06 05:37 AM, -dELta- wrote:
At 3/11/06 07:29 PM, JFriesen wrote:
All functions return a value. They have to, no matter what.

....

void inverse(int * a )
{
*a = 1/*a;
}

that doesnt return anything :p
Heh. I'm sort of teaching the way I was taught, and I don't want to force things like that on them in a beginner lesson XD

well ignore the function iteself (its stupid anyways since im using integer data type for 1/n but the point was to show that the function doesnt have to return something you could have a function which would trace a message to the console


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to C++: The Basics 2006-03-12 11:27:13 Reply

At 3/12/06 11:11 AM, JFriesen wrote: Heh. I'm sort of teaching the way I was taught, and I don't want to force things like that on them in a beginner lesson XD

Well, that's one thing but what you typed was completely incorrect, there is no rule stating that a function must return something : )


BBS Signature
JFriesen
JFriesen
  • Member since: Jan. 21, 2006
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to C++: The Basics 2006-03-12 11:30:43 Reply

At 3/12/06 11:27 AM, Afro_Ninja wrote:
At 3/12/06 11:11 AM, JFriesen wrote: Heh. I'm sort of teaching the way I was taught, and I don't want to force things like that on them in a beginner lesson XD
Well, that's one thing but what you typed was completely incorrect, there is no rule stating that a function must return something : )

Yea, shouldn't have done that. If there's any way you can edit it out to say something like, the functions we'll learn at first will return a value, but they don't always have to, and we'll get into that later.

Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to C++: The Basics 2006-03-12 12:04:11 Reply

At 3/12/06 11:30 AM, JFriesen wrote:
Yea, shouldn't have done that. If there's any way you can edit it out to say something like, the functions we'll learn at first will return a value, but they don't always have to, and we'll get into that later.

Nah, can't edit posts. No biggie though.


BBS Signature
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++: The Basics 2006-03-12 13:15:54 Reply

At 3/12/06 12:04 PM, Afro_Ninja wrote: Nah, can't edit posts. No biggie though.

Hopefully someone wanting to learn the basics will actually read the commens ^_^


Sup, bitches :)

BBS Signature
sheffgb
sheffgb
  • Member since: Jun. 14, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to C++: The Basics 2006-03-12 13:21:44 Reply

yay! i understand ( alittle bit) now!

thank you!

JFriesen
JFriesen
  • Member since: Jan. 21, 2006
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to C++: The Basics 2006-03-12 15:05:33 Reply

At 3/12/06 01:21 PM, sheffgb wrote: yay! i understand ( alittle bit) now!

thank you!

No problem..glad to see it's helping some people.

darkwolf7676
darkwolf7676
  • Member since: Mar. 18, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to C++: The Basics 2006-04-16 10:03:36 Reply

this coding stuff is pretty cool
i'm getting all sorts of evil ideas of how to use this program

i was just wondering...
is there a code thatt will alter settings on the computer that executes it?

yours sincerly,
darkwolf

seel
seel
  • Member since: Jun. 27, 2005
  • Offline.
Forum Stats
Member
Level 21
Musician
Response to C++: The Basics 2006-04-30 20:47:26 Reply

aint the int main() suposed to have void in the brackets??? like int main(void) ?

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++: The Basics 2006-04-30 22:02:14 Reply

I rarely use void functions due to the fact the best way to use them is to use them in a situation in which they'll never fail - otherwise you have no method of detecting whether the function worked or not. You can use a global, but sometimes you need to be careful with globals. Things like allocation of memory or sometimes even pointer based functions shouldn't be void.


omg.
Playstation Network tag: muffin-noodle
the empty set

T-F-K
T-F-K
  • Member since: May. 26, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to C++: The Basics 2006-05-01 19:12:47 Reply

At 4/30/06 08:47 PM, seel wrote: aint the int main() suposed to have void in the brackets??? like int main(void) ?

no,no it dose not.

phyconinja
phyconinja
  • Member since: Sep. 18, 2004
  • Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to C++: The Basics 2006-08-18 20:56:37 Reply

At 3/11/06 07:29 PM, JFriesen wrote: With this guide, we will use the free compiler, Bloodshed Dev C++ available here: http://www.bloodshed.net/devcpp.html

sorry to bother you dude.. or anyone whos looking into this.. but I downloaded the program from that file.. and instaled it.. but when I run it I gett an error mesage saying

"There doesn't seem to be GNU Make file in PATH or in Dev-C++ Bin path. Please make shure that you have GNU Make and adjust Bin setting or system PATH enviroment variable and that make setting in Compiler Option contains correct filename, otherwise you will not be able to compile anything"

what should I do?
plz reply soon..

phyconinja
phyconinja
  • Member since: Sep. 18, 2004
  • Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to C++: The Basics 2006-08-18 21:00:21 Reply

At 8/18/06 08:56 PM, phyconinja wrote: lots of stuff

omg.. I feel so emberesed.. can some one plz delete this post and my last?
and never mind about my question

CaptinChu
CaptinChu
  • Member since: Sep. 11, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to C++: The Basics 2006-08-18 21:37:47 Reply

Good tutorial, but it doesn't quite go into C++ alot. That's alot of stuff for a very simple code, but it goes quite in-depth for stuff that looks basic. Nice job.

phyconinja
phyconinja
  • Member since: Sep. 18, 2004
  • Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to C++: The Basics 2006-08-19 20:15:22 Reply

At 8/18/06 09:37 PM, CaptinChu wrote: Good tutorial, but it doesn't quite go into C++ alot. That's alot of stuff for a very simple code, but it goes quite in-depth for stuff that looks basic. Nice job.

but its a powerfull language and you can do alot with it when your good at it!!

UnknownFear
UnknownFear
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 37
Gamer
Response to C++: The Basics 2007-02-11 12:42:50 Reply

Having a little trouble. I copied the above C++ script and pasted it into a new source file. Hit Execute, Compile & Run, saved it as Hello World, hit OK. Nothing happened.

Under the Compiler tab, under Message it says "The system cannot find the file specified" and under the Compile Log, I have one error. This is what the Compile Log says:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Daniel\Desktop\hello world.cpp" -o "C:\Documents and Settings\Daniel\Desktop\hello world.exe"
Execution terminated

Anyone see anything wrong?

cowloverdude
cowloverdude
  • Member since: May. 4, 2006
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to C++: The Basics 2007-02-11 13:09:40 Reply

Ya know, you could just make a flash tutorial of it. I bet it would make good points! Or, at least that's how I learned my javascript. :)


Macs are amazing and everything Apple makes they make it good (except Air) and if you can't afford it stop crying, leave it to the pros, and move on. Fuck off.

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to C++: The Basics 2007-02-11 13:55:23 Reply

At 2/11/07 12:42 PM, UnknownFear wrote: Anyone see anything wrong?

Hehehe. Do you have gpp?


BBS Signature
sirtom93
sirtom93
  • Member since: Dec. 22, 2006
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to C++: The Basics 2007-08-20 12:23:24 Reply

cool i did my first bit of c++.

seel
seel
  • Member since: Jun. 27, 2005
  • Offline.
Forum Stats
Member
Level 21
Musician
Response to C++: The Basics 2007-08-20 13:31:54 Reply

At 3/11/06 07:29 PM, JFriesen wrote: Compilers:
To start, you'll need a "compiler". A compiler takes the code you've made, translates it into machine language, and creates a program (an Executable, something with the file extension .EXE). With this guide, we will use the free compiler, Bloodshed Dev C++ available here: http://www.bloodshed.net/devcpp.html

Excuse me if I'm picky, but Dev C++ is not a compiler. MinGW (which Dev C++ uses by default) and GCC are compilers (MinGW is based on GCC).