-
June 2nd, 2002, 02:46 PM
#1
C++ Tutorial Chapters 1 & 2
Okay here are two chapters I wrote for my site a while ago. I thought I'd post them here. There might be some technical errors, so sorry.
C++ Programming (Chapter One) by Jethro
Topic: Programming
http://jethrojones.hyperlinx.cz
-------------------------------
This tutorial is about basic programming with C++. Very basic.
If you are a newbie however, you many find this tutorial quite useful...
We cover basics like variables, functions, compilers,
headers, operators...etc
C++ (pronounced "see-plus-plus") is an extended version of the
C language which was developed in Bell Labs in 1978. In the
early 80's, C++ was created as an object-orientated language.
Without going into too much detail on the history of C++, I'll
tell you this: C++ is the language which most software developers favour,
because of its power, ease-of-use and its acceptance
on most systems, such as Windows and *NIX.
To make a C++ program you will need a compiler. I use Dev-C++ for
Windows (http://www.bloodshed.net) but there are good ones such as
Borland C++ Builder 5.5 (http://www.borland.com) and for UNIX, the
famous GCC compiler is the best. This comes with a lot of UNIX systems
like Red Hat Linux, I believe.
Before the code can be executed, it must be linked to other
pieces of code (e.g. included libraries) used by the program. The
compiled & linked program is called an executable file. Finally,
the program is executed by the system. The compiler does this.
Compilers takes your typed source
code and translates it into machine code (1s and 0s).
Here is an example program:
************ helloworld.cpp ************
Code:
/* This is a comment
Hello World program
Written by Jethro */
#include <iostream.h>
int main()
{
cout << "Hello World" << endl; // First statement
return 0; // Second statement
}
*****************************************
Okay, time to go through this line-by-line...
*****************************
/* This is a comment
Hello World program
Written by Jethro */ = This is a comment. There are two types
of comments. Comments which run over a few lines (multiline) or
single-line comments. This is an example of a multi-line comment.
Everything between "/*" and "*/" gets ignored by the program itself.
Comments are good for people who read your code, so they know what
everything does so they can fix/debug it easily.
*****************************
#include <iostream.h> = This tells the compiler that the "iostream.h"
library file is needed and should be included when compiling the code. A
library is a collection of program code that can be included (and used) in
a program to perform a variety of tasks. "iostream.h" is a library
(or "header") that performs certain input and output functions. You can get
these library files (if for some strange reason they didn't come with your
compiler) at loads of different websites like http://code.box.sk.
*****************************
int main() = Okay, this is fairly complicated but bare with
me. "int main()" is a function header. "main()" is the function name. A
function is a collection of statements. A function header includes the return
type of the function (what it returns to the place it was called when it exits)
and the function name, in this case "main". This function returns an INTeger
through the line "return 0", but more on integers and the RETURN statement later.
So all the functions that have an integer as the return type return integers.
All the statements in a function are enclosed in curly brackets "{" and "}".
This means that everything between the open bracket "{" and the closed bracket
"}" are part of that function. I hope I didn't make that sound too complicated.
You'll get it eventually, even if you don't understand it now.
Note: For a function to be executed it needs to be called. You can call a
function from anywhere, just by entering its name, like "askName()", except
for the main() function. This is called automatically when the program is
executed. This is why we don't call it. main() funcions usually return an integer.
*****************************
cout << "Hello World" << endl; = This prints "Hello World" to the screen.
"Hello World" is a string of text (in programming, there's no other kind of
string, other than a text string).
This line is an example of a stream. A stream is an input/output
device. "cout" (pronounced "see-out") is the Console OUTput stream. "iostream.h"
needs to be INCLUDEd when we use streams. First we send "Hello World". We send it
using an operator. The "<<" operator. This basically means, send the string to the
cout stream. Then we also send "endl" (this is optional). endl means END Line, so
if we send some
sort text to cout, it will appear on a new line. All statements are ended with a
semi-colon ";".
****************************
// The first statement
// The second statement = These are both examples of one-line comments.
****************************
return 0; = This returns the integer 0 from where it was called. In the
case of the main() function, this just means it exits fine. The main() function
should always return a 0. More of the return command later.
*****************************
And that is helloworld.cpp explained. Just compile that and view it.
Variables.
Variables are pieces of information stored on the memory. There are three parts
to a variable. Its name, type and value. Its name is just what we call it. Its
value is the information it holds. This can be an integer, a decimal, a string...etc.
It's type is defined in accordance to the information it is going to hold:
int = -32,768 to 32,767
long int = -2,147,483,648 to 2,147,483,647
float = 1.2 x 10^-38 to 3.4 x 10^38
double = 2.2 x 10^-308 to 1.8 x 10^308
char = 256 character values
Here is an example program which shows this:
************** numbers.cpp ***************
Code:
/* To show variables in action
Numbers program
Written by Jethro */
#include <iostream.h>
#include <stdlib.h>
int main ()
{
int num1;
int num2;
cout << "To show variables" << endl;
cout << "Please enter a number: ";
cin >> num1; // User input #1
cout << "\nPlease enter another number: ";
cin >> num2; // User input #2
cout << num1 << " + " << num2 << " = " << num1+num2 << endl; // Add
cout << num1 << " - " << num2 << " = " << num1-num2 << endl; // Subtract
system("PAUSE");
return 0;
}
******************************************
New stuff:
*********************************
#include <stdlib.h> = This is another example of using one of the
countless header files available in C++. We need to include "stdlib.h"
because we use the SYSTEM() function to interface with MS-DOS.
*********************************
int num1; and int num2; = Here we declare two integer-type variables.
We call them (originally enough "num1" and "num2". But you can call them
"drag" and "weed" if you want, it doesn't matter. Just remember that C++ is
CaSe SeNSiTiVe, so num1 is not the same as Num1, nUm1 or even nuM1! We don't
give them values, we can assign them values later... Anyway, as long as you
know that that means they are integers, it's okay.
********************************
cin >> num1; = "cin" (pronounced "see-in") is another example of a stream,
courtesy of "iostream.h" again. This means that everyhing the user types, up
to the carriage return (aka. "Enter"), will be included into the variable "num1".
Notice the way we use ">>" to symbolise that the information is going from
"cin" to "num1", not vica versa, as is in the "cout" stream. We did not use
"endl" on the previous line, because we wanted the user to type the number
right after "Please enter a number: ".
*******************************
cout << "\nPlease enter another number: "; = Your probably wondering why
I put '\n' there. Well, it's simple. \n means go to a new line. Similar to
endl. The following line:
'cout << "\n\n\nThree lines down";' would print out the words "Three lines
down", three new lines down.
*******************************
"num1+num2" and "num1-num2" = This is the use of operators. num1+num2
returns the value of num1 added to num2 and num1-num2 gives the value of num2
subtracted from num1. This does nothing to the actual value of the variable
however.
+ = Addition
- = Subtraction
* = Multiplication
/ = Division
*******************************
system("PAUSE") = The system() function, courtesy of "stdlib.h",
lets you run an MS-DOS command. If you have read any of tutorials (I have
written a few) on MS-DOS, you will know that this creates the prompt "Press
any key to continue..." and will not continue executing the program until
the user has pressed a key.
*******************************
And that's numbers.cpp...
I think that's enough for Chapter One. In Chapter Two I will talk about
functions, conditional statements, loops and whatever else I think of.
If Chapter Two hasn't been written yet, check back in a few days.
**************** EOF ***************
-
June 2nd, 2002, 02:46 PM
#2
C++ Programming (Chapter Two) by Jethro
Topic: Programming
http://jethrojones.hyperlinx.cz
-------------------------------
This is the second chapter in my current series of Computer Programming
with C++. If you haven't read Chapter One, then read it now. It might
shed some light on some of the techniques used in the section. In this
section we are going to cover:
Index:
o Conditional Statements (IF...ELSE)
o Loops (FOR...WHILE)
Here we go!
************** checkage.cpp ************
Code:
/* This program checks
what age you are! !!!WOW!!!
Written by: Jethro */
#include <iostream.h>
void main() {
int age;
cout << "The amazing age checking program" << endl;
cout << "Enter you age in here please: ";
cin >> age;
if (age==18) {
cout << "\tYou are just old enough to vote" << endl;
}
if (age==16) {
cout << "\tYou are just old enough to drink" << endl;
}
if (age>10) {
cout << "\tYou are older than 10" << endl;
}
if (age<70) {
cout << "\tYou are younger than 70" << endl;
}
if (age!=13) {
cout << "\tYou are not 13" << endl;
}
else {
cout << "\tYou have some kind of crazy age!" << endl;
}
}
*****************************************
Ahh, let's check this stuff out.
The conditional statement is easy to understand. If you have ever
learnt a programming language i your life, then you will probably know
that nearly every language has some sort of a conditional statement. A
lot of languages (eg. PHP, JavaScript...etc) have this exact kind of
conditional statement:
IF (conditional) {
statements
}
The "if" part is pretty easy to understand. If the (conditional) is
true then the "statements" should be executed. The condition is
usually in the following form:
IF (variable==value) {
do something
}
For example. In the checkage.cpp we used many variations of this. The
first was:
if (age==18) {
cout << "\tYou are just old enough to vote" << endl;
}
This means that if the age variable is equal to 18 the execute the
statement:
'cout << "\tYou are just old enough to vote" << endl;'
I think that's fairly easy to understand. But what if you want to check
if it isn't equal to it, or it's bigger than that. Then you use a
different operator. Here is a list of some of the most common:
************************
Operator - Definition
----------------------
== - Equal To
!= - Not equal to
< - Smaller than
> - Bigger than
<= - Smaller or equal to
>= - Bigger or equal to
************************
By putting an exclamation mark (!) infront of any of these, turns them
around. For example age!<6 would be true if "age" wasn't smaller than
6. But why say this when you can say >?
When all else fails, use "else". This is used when none of the
conditional statements are true. Of course, the else statement doesn't
need a condition.
******************* loops.cpp **********************
Code:
/* This program shows
examples of the FOR
and WHILE loops
Written by: Jethro */
#include <iostream.h>
void main() {
int i, num1;
for (i=0; i<=10; i++) {
cout << "Saying this for time number: " << i << endl;
}
while (num1!=5) {
cout << "Please enter the number 5: ";
cin >> num1;
}
cout << "You got it right!" << endl;
}
*****************************************************
More stuff to discuss:
FOR
---
The loop is really easy as well.
The FOR statement has a few different things packed into the one
statement, but it's easy once you get the hang of it.
Let's examine/dissect it piece by piece:
for (i=0; i<=10; i++) {
cout << "Saying this for time number: " << i << endl;
}
FOR - This starts off the statement
i=0 - This defines the "i" variable as being 0
i<=10 - This makes it so that "i" shouldn't go past 10
i++ - This increments the "i" variable. Basically this means "i=i+1".
"i--" is the same as "i=i-1". We add this so that every time the loop
executes, "i" gets a little bit bigger. If this didn't happen, the
loop would execute infinitely!
*******
Note: If you have read other tutorials on programming with loops you
probably have seen them use a variable called "i" as well. This is
because using "i" is an age-old tradition in loops. I don't really
know the actual reason for this, but I use the i variable because it's
the first one that comes to mind. You don't have to, of course.
*******
With the above loop, the loop will start at 0 and keep executing the
"cout << ...etc" statement until it reaches 10.
WHILE
-----
The WHILE statement is considerably easy to understand than FOR is.
Basically, the WHILE loop will keep executing while the conditional
statement is met. Kind of like a power-crazy "IF" statement. Here is
the WHILE syntax:
********
WHILE (condition) {
statement
}
********
For example:
**********
Code:
int jk = 0;
WHILE (jk!=5) {
cout << "\nJK is 5! Change it: ";
cin >> jk;
}
**********
The above loop would keep executing WHILE "jk" is not equal to 5. Of
course if you use this command and don't give the program the
opportunity to change it, it will just keep repeating forever and ever
and ever.
And that's Chapter Two finished. Pretty easy stuff, isn't it? Chapter
Three will be out soon!
****** EOF ******
-
June 2nd, 2002, 05:29 PM
#3
Senior Member
Thanks, this will help out lots, and get them started.
[gloworange]Die, or surrender, either way won\'t work.[/gloworange]
[shadow]HuntX7[/shadow]
-
June 2nd, 2002, 05:36 PM
#4
Could you put these in .DOC's or something...
I wan't to store a copy on my File-Store....
- Noia
With all the subtlety of an artillery barrage / Follow blindly, for the true path is sketchy at best. .: Bring OS X to x86!:.
Og ingen kan minnast dei linne drag i dronningas andlet den fagre dag Då landet her kvilte i heilag fred og alle hadde kjærleik å elske med.
-
June 2nd, 2002, 09:45 PM
#5
I have the two files in .html format. Wait, I'll attach them...
-
June 2nd, 2002, 09:51 PM
#6
awesome.../me goes off to learn c++
\"\"A weak mind is like a microscope, which magnifies trifling things but cannot receive great ones.\" — G.K. Chesterton, 19th-century English essayist and poet\"
-
June 2nd, 2002, 10:01 PM
#7
Thanks... I already know it though :P Thanks tho...
-
June 2nd, 2002, 10:32 PM
#8
By the sacred **** of the sacred psychedelic tibetan yeti ....We\'ll smoke the chinese out
The 20th century pharoes have the slaves demanding work
http://muaythaiscotland.com/
-
June 2nd, 2002, 11:29 PM
#9
Senior Member
If you wanna know more read a book, try 'Sams Teach Yourself C++ in 21 Days'
[gloworange]Die, or surrender, either way won\'t work.[/gloworange]
[shadow]HuntX7[/shadow]
-
June 5th, 2002, 11:41 PM
#10
hey jethro u must kinda happy IRELAND tied the game against GERMANY...or do u not like soccer?....well anyway i think maybe u should submit ur C++ tutorial to the AO newsletter talk ot MsMittens that would be cool to put in there
\"\"A weak mind is like a microscope, which magnifies trifling things but cannot receive great ones.\" — G.K. Chesterton, 19th-century English essayist and poet\"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|