-
October 28th, 2003, 05:50 PM
#1
Banned
C++ Torn Apart: Part 1 - The Beggining
[gloworange]**************THIS TUTORIAL BELONGS TO ANTICODE**************************[/gloworange]
ABOUT THE C++ Programming language
C++ is basically the C# Programming language + some very powerful features + object oriented approach.
According to me, C++ is the best available language. BUT WAIT, dont shout! I said... "According to me".
Yeah, there is a small disadvantage C++ has. If you dont know the language real well, you will never go further than creating programs which find sum and average of numbers! Unlike Perl, where basic knowledge + some practice can get you creating sockets in no time, C++ needs loadz n loadz of attention.
But once you are a C++ guru, you have an extensive toolkit at your fingertips. You'll never
need to download any kind of software for scanning, mailing, posting, socketing, brute-forcing blah-blah..
Its got all the features of any language you could dream of. And if you didnt know, windows and unix kernel are made in c#, which is a primate when compared to c++
WHAT WILL I TEACH YOU IN THIS TUTORIAL........
Ill get on with giving you basic knowledge about the C++ programming language. If you know C#, it'll be helpful but if you dont... dont worry. Im starting from notch..
CAN YOU COUNT ON ME TO TURN YOU INTO A C++ GURU
The answer is very simple. NO! I might stop writing this tutorial at anytime. Ive got my Grade XI Board Examinations coming up. So i might not be taking you very far.
But you can be sure of one thing..
Whatever i teach you.... you wont need to know more about it.
WHAT YOU NEED TO PROGRAM IN C++
1. Good Logic
2. Every program that you write in C++ .... is meaningless. Yes, its meaningless until your system cant understand it. And your box wont understand it unless you have a compiler. A compiler is a program which converts your C++ code (which is known as source code)into a meaningful program which the computer can understand.
So get your hands on a compiler. I would recommend getting Borland C++ 4.5 for begginers.
If you are using a shell account which already has a C++ compiler, you dont need to get one on your system. However, its still useful to get Borland because its much more user-friendly and has got a lot of built in features.
And BESIDES, THIS TUITORIAL IS BASED ON BORLAND AND MICROSOFT C++. Some Parameters Might Differ In The Unix C++...
Once you have a compiler at hand, you are ready to begin programming.
.......................................................................................................
A NOTE ABOUT HEADER FILES
[B]What are header files??
(If you possess even a basic knowledge of header files in C#, JUST SKIP THIS)
C++ like C# is all made up of functions and definitions. Some of these have to be created by you. Others have already been made and stored as header files.
These header files have extension .h
Different header files have different purpose which i shall be explaining one-by-one later on...
The header file must be included into your source code. This is done using the Pre-Processor Directive (i.e #include)
Suppose you want to include the header files iostream.h and conio.h into your source, you type
#include<iostream.h>
#include<conio.h>
at the beggining of your source code.
PHP Code:
TIP: In Unix, the way to include files is different. Instead of using the syntax
#include<iostream.h>
we use:
include<iostream.h>
But as this is basically for Borland/Microsoft, I shall continue to use the first option!!!
Points to remember:
1. Header files must be included at the beginning of your program
2. If you want to include more than one header file, you can do so in any order.
...................................................................................................................................
THE MANDATORY HELLO_WORLD PROGRAM
If you have programmed in any language before, you will be knowing about the Hello World Program.
Its the first program they (books, teachers, other tuitorials) teach you when you start learning.
Lets take a look at the Hello World Program in C++
#include<iostream.h>
void main()
{
cout<<"HELLO WORLD";
}
Before I explain the above program, let me tell you about something important...
C++ is executed line by line. Your line ends at a semicolon ( ; ). So it is possible to write
two statements in the same line provided that they are seperated by a ;
And anything that you type after // are known as comments. The compiler ignores comments and does not compile them.
PHP Code:
NOTE: The ............ before comments have no significance. you must not use them. I have used them only to insert spaces between the code and the comments
Now i shall re-write the above program with comments included.
include<iostream.h> .....................................................................//Here you are telling the compiler to include header file iostream.h
void main() .....................................................................//The main fuction...
{
cout<<"HELLO WORLD"; .....................................................................//You print out Hello World at the screen
}
THE MAIN() FUNCTION
All the code that you write must be written in a function. Every function begins and ends with curly braces as shown in the above program. A C++ program is divided into many funtions. main() is
the basic function which has to present in every program. The use of the identifier void before main() tells the program that the function is not returning any value
And unless you are making a complex program(which you are not going to do for quite a while, you dont need any other functions in the program. main() will do for all..
..................................................................................................................................................
PRINTING DATA ONTO THE SCREEN
cout<< (pronounced see out)is the operator used to output some data onto the screen. The definition for this is stored in the
header file iostream.h
Suppose i want to output "i am SpIdER" on the screen. i will write the following program..
include<iostream.h>
void main()
{
cout<<"i am SpIdER"; .....................................................................// i am SpIdER must be in double quotes for the program to understand that its a string
}
____________ The output will be_________________
i am SpIdER
.......................................................................................................
DECLARING VARIABLES
In C++ there are basically three types of variables. They are
1. int - All integers (eg. 1,2,3,4,167)
2. float - floating point variables (eg. 1.8, 6.87, 2.312)
3. char - character variables (eg. s, t , h)
It is very easy to declare variables in C++. They can be declared anywhere in the code.
when declaring variables, the following syntax must be followed.
datatype name; (where datatype is either int, float or char and name is the name of the variable)
Lets write a program where we declare a variable and use it in the code...
#include<iostream.h>
void main()
{
int a=4; .....................................................................// i declare the variable a and initialise its value to 4;
cout<<"The value of a is: "; .....................................................................// prints "The value of a is: " on the screen
cout<<a; .....................................................................// prints the value of a (i.e 4) on the screen
a=a+3; .....................................................................// a becomes a+3 = 4+3 = 7;
cout<<"\n After changing a is: "; .....................................................................// "\n" is used to tell the program to output on a new line
cout<<a; .....................................................................// prints the new value of a (i.e 7)
}
The Output of the program will be:
___________________________________
The value of a is: 4
After changing a is: 7
___________________________________
......................................................................................................
GETTING INPUT FROM THE USER
Suppose you want to write a program which asks a user for two numbers and then finds the sum of the two, you must have some method to get the
user to input the numbers.
The operator used to get data from a user is cin>> . This operator is also defined in the header file iostream.h
let us try writing a program for the above problem.....
#include<iostream.h>
void main()
{
int a, b; ........................................................................................// we declare two variables for numbers to be inputted by the user
int sum; .......................................................................................// we declare a third variable which we will use to find the sum of a and b
cout<<"Enter two numbers: ";
cin>>a; .......................................................................................// the user enters a number which gets stored as variable a
cin>>b; .......................................................................................// the user inputs another number which gets stored as variable b
sum=a+b; .......................................................................................// the variable sum is given the value a+b
cout<<sum; .......................................................................................// we display the value of sum;
}
......................................................................................................
Thats it for now... Hope This Helps...
Watch Out For Part 2 of this tutorial - SoMe Handy Functions in C++
ANTICODE
__________________________
-
October 28th, 2003, 06:09 PM
#2
That may work in Microsoft Visuall C++, but I don't think that will compile in unix. It should be '#include <iostream>'
And then declare 'using namespace std;.
Also, you forgot to mention that the compiler skips white spaces so:
cout << "Hello World";
is the same as
cout <<"Hello World";
and maybe you should have included the 'endl' function so their programs don't end like:
Hello Worldbash$:
Finally, it is good practice to add 'return 0;' at the end of your program. Good tut, but make sure it is correct.
You shall no longer take things at second or third hand,
nor look through the eyes of the dead...You shall listen to all
sides and filter them for your self.
-Walt Whitman-
-
October 28th, 2003, 06:10 PM
#3
Good start at a C++ tutorial, very easy to follow and under stand. I think the examples you gave are a bit oudated though. Havn't stayed current with C++ but I believe the correct way would be:
#include <iostream>
using namespace std;
Also some additional info on variables would have been helpfull. Such as, long, double bouleen. etc.
Not sure when your going to cover pointers, usually those come later.
Nice job though.
DeafLamb
EDIT: Oh well, got beaten to the punch.
-
October 28th, 2003, 06:13 PM
#4
Banned
Actually I dont have much experience about C++ in Unix.
Im writing it mostly for Microsoft Visual C++ and Borland...
thats what i work in
about the endl, that and much more will come in later...
thanks for the tip anyway
th3 Spid3r
_________
-
October 28th, 2003, 06:15 PM
#5
Edit: Nevermind, my compiler (g++) compiled that without any namespaces and ran. It did give me backward header warning (using an old header). But it said that main has to return a value of int. Learn something new everyday.
You shall no longer take things at second or third hand,
nor look through the eyes of the dead...You shall listen to all
sides and filter them for your self.
-Walt Whitman-
-
October 28th, 2003, 06:19 PM
#6
Banned
Ill be giving info about variables quite soon... and pointers yeah. they are a bit complex and as this is for beginners, that might come in a bit late....
th3 Spid3r
_______________
-
October 28th, 2003, 08:51 PM
#7
C++ is basically the C# Programming language + some very powerful features + object oriented approach.
W T F its all i can say
-
October 28th, 2003, 09:04 PM
#8
You know, I was looking back at my post and I realised that the forum skips empty space as well. I meant it to look like cout <<_______________"Hello World"; and cout <<"Hello World";
If you want more info with a unix contrast, feel free to drop a line, I would be glad to help.
WTF THE FORUM KEEPS EFFING MY POST!!!
You shall no longer take things at second or third hand,
nor look through the eyes of the dead...You shall listen to all
sides and filter them for your self.
-Walt Whitman-
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
|
|