-
October 19th, 2003, 06:32 AM
#1
Member
Starting C++. A tutorial.
This is a basic tutorial on C++. If this has already been done, sorry. Part of the reason for this tutorial is to ring in my upgraded membership to AO member instead of newbie. The other reason is to educate those who need it. Lets start the tutorial.
A beginners guide to C++ (Written by beginners for beginners)
First off, finding a good compiler. I use MSVC but I have heard good things about DevC/C++. there are other forums which address this problem. The most important part of coding is making readable code. I will include some examples that are in good form in another post. I will also talk about form throughout this tutorial. To include text that won't be compiled (Meaning C++ compiler will skip over it) use this /*insert text here that you wish to be ignored. This is good for as many lines as are kept between the asterix and slash*/ or //this is only good for the current line.
1) The libraries; you must always select a library to use. The only library you will be dealing with right now is "iostream.h". The code for a library looks like this:
#include <iostream.h>
#include must always be used and the library must always be between <>. This is always the first line of code. Next line of code should be:
int main()
Just include this and don't ask why yet. We will hit that later. Remember int main() must always be in your program outside of the brackets.
Lets skip ahead to the fun stuff:
/* Ian A. AKA Dark Star Prometheus 10/19/03 Hello world*/
#include <iostream.h>
int main()
{ //always use this bracket here
cout<<"Hello World!"; //cout *, "" *!, ;*|
return(0); //return (0) just tells the computer that the program executed normally
} //need bracket here too.
* cout stand for console out, it just means that the computer is going to display text.
*! "" are just used to denote text that will be displayed.
*| ; this symbol just means the end of a line. You will almost always use this(Except for IF statements and some others. that will be later though.)
You should indent the lines after the bracket so your code is easier to read.
2) More with cout. cout can be used for more than just printing text, it can also play with numbers. Mathematical operations in C++ are addition "+", subtraction"-", multiplication"*", and division"/". That's it, Yes that is really it. Oh so I lied, shoot me. There is also Modulus "%". This mods the numbers, example: 22%5 = 2. This is basically the left over amount from 5 going into 20 - current number. I hope you get it, it is hard for me to explain. It is the remainder, that is the word I wanted! 22 mod 5= 2 the remainder (cause 20/5 4 times and 2 is left over from 22) Anyway mod is used often, so learn it.
now on to practical use of this info.
cout<< Oh yeah I forgot to mention before, you always need "<<" it signify's the start of a new command.
cout<< 20 * 5;
the above line will not print "20 * 5" when the program is compiled, it will display 100, which is the answer. This is how one creates math problems in C++.
Some code to demonstrate:
#include <iostream.h>
int main()
{
cout<<"This program will display the area of a circle"<<endl; //endl just skips to the next line
//when the program is compiled and run.
cout<<"Radius = "<<10<<endl;
cout<<"Area = "<<3.14*10*10;
return(0);
}
When run this program will display the following lines:
This program will display the area of a circle
Radius = 10
Area = 314
See what it did? It did exactly what I told it to do. Multiplied 3.14 *10 *10 the equation for calculating the area of a circle. This is all for now. I will do more tommorow.
Any questions/comments/flames should be posted or emailed.
If you have an urgent question feel free to E-mail me at dsp2600@bluefrog.com
More on this tommorrow. Bye!
\"The wise programmer is told about Tao and follows it. The average programmer is told about Tao and searches for it. The foolish programmer is told about Tao and laughs at it.
If it were not for laughter, there would be no Tao.\"
-
October 19th, 2003, 07:40 AM
#2
Junior Member
i dont know about C, but this tutorial make me curious and want to know much more
. Could you suggest the step by step to learning C language..a kind of guidance like chapter by chapter in book or courses.
thanks redhawk
-
October 19th, 2003, 08:00 AM
#3
Junior Member
Interesting Stuff. Thanks for the tutorial!
-
October 19th, 2003, 11:03 AM
#4
Before you are writing more tutorials check the search option from this site, and you will find out that there are more c++ tutorials on this site with maybe even more information. Negative has made a thread with an index of all tutorials on this site.
-
October 19th, 2003, 02:21 PM
#5
Member
This is just the start, am going to post A much better C++ tutorial in a few days (Maybe even today) I am still writing it. What the new C++ tutorial will have:
Better format, easier to read
Lots more info, variables, if statements, loops, casting variables, etc.
*Might include a "how to make libraries" minipost*
Downloadable examples of my own code, (I wrote it in notepad, notepad is great for writing code)
I will try to have this out ASAP. Expect for it in two days but look today.
P.S. also cin statements, and boolean operators will definetly be discussed.
\"The wise programmer is told about Tao and follows it. The average programmer is told about Tao and searches for it. The foolish programmer is told about Tao and laughs at it.
If it were not for laughter, there would be no Tao.\"
-
October 19th, 2003, 02:23 PM
#6
Member
maybe you could include some links to download the compiler like http://www.borland.com/products/down..._cbuilder.html
-
October 19th, 2003, 04:41 PM
#7
I can't think of the guys name but, he asked about C. I know a bit of it. Not anything to brag about but I know some. The same program that he wrote could be done in C.
#include<stdio.h>
main()
{
printf("Hello World:\n");
}
return 0;
Now the second one that he did.
Is a little different in C.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number1, number2, total;
printf("What is the first Number:");
scanf("%d", &number1);
printf("What is the second Number:");
scanf("%d", &number2);
total = number1 + number2;
printf("Total is %d\n", total);
system("PAUSE");
return 0;
}
That is that code in C, now to understand it. Instead of adding on to redhawk's tutorial PM me and I will explain it to you.
Oh and the C version is for DevC++/Bloodshed
-
October 19th, 2003, 04:54 PM
#8
Might include a "how to make libraries" minipost
Already done by me...go and search for a tutorial named the C++ Preprocessor or just search for preprocessor and you will find it
Also, your tutorial looks very old because you used an old style of programming in C++. Instead of:
Code:
#include <iostream.h>
it should look like this:
Code:
#include <iostream>
using namespace std;
Anyways, using namespace std i a LOT more common in todays C++ code...thought i would point that out but overall a good tutorial
Support your right to arm bears.
^^This was the first video game which i played on an old win3.1 box
-
October 20th, 2003, 05:26 PM
#9
White_eskimo is correct, that code won't compile properly on g++ and other strict compilers. You should instead write it like so:
Code:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
You can just put 'using namespace std' at the top of the code under the header includes, but I personally consider that to be bad style (and it defeats the whole point of namespaces if you're just going to ignore them anyway).
Also, I found the code in this tutorial incredibly difficult to read, partially because it was in a variable-width font (which can be over-ridden using the BB code tags) and I don't see why you have comments like:
Code:
//cout *, "" *!, ;*|
BTW, cout doesn't necessarily mean that some text will be displayed. Yes, in 90% of cases the standard output will be to the console, but it can be redirected to files or other devices. Obviously this doesn't happen in the Hello world type of programs, but it is a point that needs to be emphasised.
Other than those points above, it's a good introductory tutorial to C++.
-
October 20th, 2003, 10:30 PM
#10
Member
One of the major benifits of c++ as compared to some other languages such as c is the concept of object oriented programming. I am sure there is plenty of reference on AO, but if you would like to make your tutorial more effective, you might want to mention this.
Other important things to mention:
(in no particular order, just coming to mind randomly)
makefiles
debugging
using namespace std
cin
variable assignments
pointers
pass by reference
pass by value
if, ifelse, while, dowhile, for
classes
switches
overloading
arrays
functions
and of course object oriented
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
|
|