Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Basics of C++ - Part 1
#1
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 cant 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
Reply

#2
nice tutorial osama8
[Image: lQDUjba.jpg]
Reply

#3
Aweomse. Thx very much Big Grin
I always wanted a C++ Tut.
+1 Rep
How many Parts u gonna make?
Reply

#4
(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.
Reply

#5
Nice tut
Reply

#6
why there is no C# section Fuu Fuu Fuu
Reply

#7
Because C# is a useless language. It's as silly in being a scripting language as Visual Basic.
Reply

#8
(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
Reply

#9
(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.
Reply

#10
(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#
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Part of HTML missing Arteq 8 4,552 08-03-2013, 03:55
Last Post: Bandarigoda123
  [News] Dedicated Server: Basics Pozzuh 15 8,566 11-07-2011, 17:57
Last Post: Rendflex
  quantum levitation part 2 Pozzuh 1 1,840 10-26-2011, 22:27
Last Post: 4FunPlayin
  [Tutorial] Basic MW2 Memory Hacking: Part 2 Yamato 2 5,170 10-10-2011, 12:37
Last Post: marsjee
  Aosma8's Fans Attention (aosma8 irl: part 4) 4FunPlayin 1 1,971 09-13-2011, 12:55
Last Post: aosma8
  Aosma8 irl part 3: singing friday 4FunPlayin 8 4,830 08-31-2011, 00:06
Last Post: aosma8
  [Tutorial] Basic MW2 Memory Hacking: Part 1 Yamato 2 5,290 08-19-2011, 19:18
Last Post: d0h!
  COD: Black Ops The Hidden Perks Part 2 JariZ 9 10,878 08-09-2011, 10:17
Last Post: JariZ
  [Tutorial] Creating a HUD Element/Text (Basics) iAegle 8 19,828 08-05-2011, 19:31
Last Post: JariZ
  [News] The secret of the worlds console Part 2 iAegle 4 2,792 07-06-2011, 17:28
Last Post: Nekochan

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.