It's Mods

Full Version: Basics of C++ - Part 1
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey guys, this will be the first part in a series of C++ tutorials. Enjoy Big Grin !

Structure of a program

Statements and expressions:


Statements in C++ are basicly the sentences used to tell the compiler what to run, all statements are terminated by the semi colon ( ; ).

There are many different types of statments in C++, here are just a few common statements:

PHP Code:
int x;
5;
cout << x

int x: This is called an declaration statement, x is declared to be a variable, all variables must be declared before they are used.
x = 5: This is called the assignment statment, x is assignmed to be the number 5
cout << x :This is an output statement.

Expressions:


The compiler is also capable of resolving expressions. An expression is an mathematical entity that evaluates to a value. For example, in math, the expression 2+3 evaluates to the value 5. Expressions can involve values (such as 2), variables (such as x), operators (such as +) and functions (which return an output value based on some input value). They can be singular (such as 2, or x), or compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3)).

For example, the statement x = 2 + 3; is a valid assignment statement. The expression 2+3 evaluates to the value of 5. This value of 5 is then assigned to x.



Hello World!:


Here we have the code :

PHP Code:
// #include "stdafx.h" // Uncomment if Visual Studio user  
#include <iostream>

int main()
{
   
using namespace std;
   
cout << "Hello world!" << endl;
   return 
0;


#include <iostream>: This line indicates the preprocessor directive of the proram.Preprocessor directives tell the compiler to perform a special task. In this case, we are telling the compiler that we would like to use the iostream library.In this case the preprocessor directive tells the program what cout and endl do.

int main(): This line shows the main() function which every C++ program has.

{}:These brackets indicate the start and end of the main() function.

using namespace std;: This is the first statement of the program, you can tell that it is a statement because it ends with a semi colon ( ; ) . wWthin iostream, they live inside a special compartment named std (short for standard). This using statement tells the compiler to look inside a compartment named std if it can’t find cout or endl defined anywhere else. In other words, this statement is also necessary so the compiler can find cout and endl, which we use on line 6.

cout << "Hello world!" << endl;: Cout is an output statement, which represents the console/screen. Cout understands that anything sent through the << operator should be printed onto the screen. Endl is a statement which ends the line and moves the cursor to the next line.

return 0;: This is called the return statement, it return a value to the operating system after an executable program has run, the value determines whether it was successfully run or not. The return value of 0 is given which means that everything went ok.

Cin:

Cin is basicly the opposite of cout,whereas cout prints data to the console, cin reads data from the console.

PHP Code:
//#include "stdafx.h" // Uncomment this line if using Visual Studio
#include <iostream>

int main()
{
    
using namespace std;
    
cout << "Enter a number: "// ask user for a number
    
int x;
    
cin >> x// read number from console and store it in x
    
cout << "You entered " << << endl;
    return 
0;


Try compiling this program and running it for yourself. When you run the program, it will print “Enter a number: ” and then wait for you to enter one. Once you enter a number (and press enter), it will print “You entered ” followed by the number you just entered. Big Grin
nice tutorial osama8
Aweomse. Thx very much Big Grin
I always wanted a C++ Tut.
+1 Rep
How many Parts u gonna make?
(07-06-2011 12:57)Tomsen1410 Wrote: [ -> ]Aweomse. Thx very much Big Grin
I always wanted a C++ Tut.
+1 Rep
How many Parts u gonna make?

Not sure, maybe 3-4 more.
Nice tut
why there is no C# section Fuu Fuu Fuu
Because C# is a useless language. It's as silly in being a scripting language as Visual Basic.
(01-25-2012 02:02)SuperNovaAO Wrote: [ -> ]Because C# is a useless language. It's as silly in being a scripting language as Visual Basic.

But all programmers need to know C# Im studing C# and i think im going well
(01-25-2012 02:07)kokole Wrote: [ -> ]
(01-25-2012 02:02)SuperNovaAO Wrote: [ -> ]Because C# is a useless language. It's as silly in being a scripting language as Visual Basic.

But all programmers need to know C# Im studing C# and i think im going well

No, you don't need to know C# to learn programming. You can't go around C(++(0x)) though because they had much more impact on modern computers than C# will ever have.
(01-25-2012 02:09)SuperNovaAO Wrote: [ -> ]
(01-25-2012 02:07)kokole Wrote: [ -> ]
(01-25-2012 02:02)SuperNovaAO Wrote: [ -> ]Because C# is a useless language. It's as silly in being a scripting language as Visual Basic.

But all programmers need to know C# Im studing C# and i think im going well

No, you don't need to know C# to learn programming. You can't go around C(++(0x)) though because they had much more impact on modern computers than C# will ever have.

do you think i should change from c# for c++? it will be a bit hard. right now im learning loops in c#
Pages: 1 2
Reference URL's