#inlcude Is an add-on for you programs needs. one of the main ones in C++ Is <iostream>. If you learn C++ Thoroughly you will learn the commands you will need for you program. The syntax for the command is very simple:
CODE :
#include <iostream>
Using namespace std;
Now Once you have written the #include <iostream> you will need to add a command which tells the compiler that you are writing in C++. Something you mayu notice different is the semi-colon, You should use a semi-colon after every statement you make, It simply tell the program to move on form this statement and go to the next on in the program. Again the syntax for this part is very simple:
CODE :
using namespace std;
The Bulk Of The Program
Now is the time to make the program.This is were the main() Command comes into position. The main part of the program goes after this command. This is were all of the program comes together. Yes you can add things before that command but i will come to that if this article is succesful. In this command you will be intoduced (If you havent used them before) to the braces. Which look like this: {}. This is were the code is stored. That may have confused you but it is very basic once you see the syntax, Which is shown below:
CODE :
main()
{
code goes here
}
Return Statement
The return statement is again very simple. All it does is return a value to the operating system. Their isnt much to this statement although it can be quite important when writing certain programs. Here is the syntax for the return statement:
CODE :
return 0;
Once you learn more C++ you wont always have the 0 their. In C++ 0 is just another way of saying null. Also notice i have used the semi-colon. this is because it is a statement.
Writing a program
Now we have learnt all of the basic commands maybe we can write a very simple and traditional hello world program. This include another statement which i have saved until now, It is the cout << statement this is another word for print. Also this is another statement so the semi-colon is used. Here is the sytax for that:
CODE :
cout<<"text goes inbetween the speech marks";
Back to the program.
CODE :
#include <iostream>
using namespace std;
main()
{
cout << "hello world";
cin.get();
return 0;
}
The output is simply:
CODE :
Hello world
If you liked this artricle then please post and leave a good number please thanks
Nice to see here an article on the basics of C++.. mind if I add something? :D
The return statement causes the main function to finish. return may be followed by a return code (in that example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.
You could've also explained that " cin.get(); ".. you writed in the code and didn't mention it while explaining..
I'm not flaming, good job on writing it. ^^
Just contructive criticize.. :rolleyes:
yeah i have noticed them thnaks for explaining the return sattement properly for me i know bits of C but i skipped C adn went staright to C++ and i only know up to the structural part of some programs but since you sadi i have done a good job i would like to thank you
mosshack
By: thetrojan01 - 02:41 pm Tuesday August 28th, 2007
9.... very good but you could tell about the variables.. i think :)
A very good article for the basics. It would be nice to see a more advanced article on c++ though. 9/10
By: mshamsuddeen2 - 04:35 am Wednesday August 29th, 2007
A well done tutorial on the basics, but i cant agree more that you cud've explained more on the "cin.get();" statement as that is a not so obvious statement
By: mosshack - 04:35 am Wednesday August 29th, 2007
yeah since this one has gone down well i am going to start an intermediate one and then move onto the advanced thanks guys
mosshack
By: valefor - 06:19 am Wednesday August 29th, 2007
You need to explain, how to use a compiler, how to save and view your scripts etc etc... someone new to coding this language will get very confused at first with the compiler i can garuntee ;) Overall nice knowledge =]
The comments say it all..
7/10
Good work though ;)
By: alex_king - 12:08 pm Wednesday August 29th, 2007
thought this was a great article, especially for a beginner. I agree it would be great if you could write another article based on variables, input, if statements etc.
9/10
By: mosshack - 06:27 pm Wednesday August 29th, 2007
thnaks guys erm yeah ofcourse ill write another article erm maybe ill write a second article on compiler use but i learnt my compiler by myself and then i discovered the C++ Tutorials i am taking but thanks anyway
should be int main(){} as you are return'ing '0', you can't type cast a function, then return something different!
By: arkaros - 04:44 pm Saturday September 27th, 2008
yes it should me int main(). and i would write system("PAUSE"); instead of cin.get(); but maby it dosent work on Linux i dunno havent written C++ on Linux
This site is the collective work of the
HackThisSite staff. Please don't reproduce in part or whole without permission.
Page Generated: Sat, 11 Oct 2008 23:20:12 -0500 Exec:
9