[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
thunks » ASPNET » Memory » CSharpDefault » The GPRMC Sentence » QbasicFAQ_OtherCompilers » QbasicFAQ_Input » talk:Jims Page » WhatLinksHere » WLA DX » CPP-Beginners-Tutorial
Displaying differences between revision 20 and revision 21
== [center] C++ Programming Tutorial [/center] ==
-------------------------------------------------
This is a beginners tutorial on C++ programming. It covers object oriented programming and some C++ basics. You may also check out the C Programming tutorial to learn more on data types, loops, functions, etc. because this is almost similar to C++.
==== [center] Formatted printing in C++ [/center] ====
If you are familiar with C language and need to learn C++, you know that C uses formatted strings for output. The famous </b></font>Hello World program is implemented in C<font color=green><b> as follows:
[code]
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
[/code]
Unlike C, C++ makes an intensive use of strings and streams to display data.
The </b></font>Hello World program in C++<font color=green><b> looks like this:
[code]
#include <iostream>
int main()
{
cout <<"Hello World\n";
return 0;
}
[/code]
If you take a close look at the example above, you may notice at least 3 major differences to the C version of this program.
1. The header file is not <stdio.h> but [blue] <iostream> [/blue]. This is not a mistake, because the new C++ header files dont use the [blue] .h [/blue] suffix.
2. The equivalent of printf() function in C++ is cout. However, cout is not a function but an object of a class of the standard library.
3. C++ use the stream out operator [blue]<<[/blue] operator for output. The [blue] << [/blue] operator is also defined in a class of the standard library.
You can do the same things with [blue] cout<<""; [/blue] as you would do with printf().
So you can use cout for displaying strings, streams, numbers, objects etc.
[code]
cout<<"Hello Reader"<<"How are you"; //equals 2 calls of printf()
int i = 5;
cout << i; //equals printf("%d",i);
cout<<endl; //equals printf("\n");
[/code]
The opposite of cout is [blue] cin>>; [/blue]. [blue] cin>>; [/blue] equals scanf() function in C.
You should not be suprised if i tell you that cin too, is an object of a class in the standard library.
The stream in operator[blue] >> [/blue] is also implemented in a class of the standard library.
And, of course, you can do anything with cin that you would do with scanf().
[code]
#include <iostream>
int main()
{
string name;
cout<<"Whats your name?";
cin >> name;
cout << "Hello"<< name;
return 0;
}
[/code]
This sample program is straightforward. It reads the name of the user and displays it again. However, this program is a bit tricky. Again, if you know C, you might wonder where i have stored the result of cin.
The trick is, cout and cin use the same stream to read and write data. Hence, if i read data with cin, i can use this data for cout to display it.
==== [center] Variables, arrays and operators [/center] ====
In the sample program above, i have introduced another feature of C++ without explicitly mentioning it. [blue] Variables [/blue]. [blue] Variables [/blue] are used to store data in it and usually waste memory space.
In C++, all [blue] Variables [/blue] must be assigned a valid data type. The data types of C++ are the same as in C. Consider the following samples:
[code]
[green] //elementary data types [/green]
bool x; //declaring a variable of type bool
char c; //declaring a variable of type char
int i;
short s;
double d;
float f;
long l;
[green]//non-elementary data types[/green]
struct s;
class c;
enum e;
//STL special data types
[/code]
[b] Declaring vs Defining variables[/b].
In C++, there are 2 ways of introducing variables. The first way is called [b] declaration [/b] of a variable. The second is called [b] definition [/b] of a variable. The following code snippet shows the differences:
[code]
int i; //declaration of i as type int
int i = 5; //definition of variable i and assigning value 5 to it
string s; //declaration of s as type string
s = "Hello World; //assigning "Hello World" to s
string s2 = "Hello Reader"; //definition of s as string with value "Hello Reader
[/code]
Note that, when assigning values to variables, you need to make sure that the value is a [b] valid value [/b]. This means that, when, for example, you define a variable of type [b] int [/b] you have to make sure that the value you assign to it is an [b] integer [/b].
[code]
int i;
i = 5; //correct! 5 is integer
[red]
int j;
j = "Hello World" //wrong! j is not a string
[/red]
double d;
d = 3.145; //correct! 3.145 is a double
[red]
int k;
k = 1.23456; //wrong! 1.23456 is not an int
[/red]
[/code]
If you assing wrong values to variables of another data type, the compiler will complain an error.
[b] Defining [/b] a variable means to [b] declare it and assign a value to it [/b].
[b] Declaring [/b] a variable means only to introduce it to the compiler.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
thunks » ASPNET » Memory » CSharpDefault » The GPRMC Sentence » QbasicFAQ_OtherCompilers » QbasicFAQ_Input » talk:Jims Page » WhatLinksHere » WLA DX » CPP-Beginners-Tutorial
Displaying differences between revision 20 and revision 21
== [center] C++ Programming Tutorial [/center] ==
-------------------------------------------------
This is a beginners tutorial on C++ programming. It covers object oriented programming and some C++ basics. You may also check out the C Programming tutorial to learn more on data types, loops, functions, etc. because this is almost similar to C++.
==== [center] Formatted printing in C++ [/center] ====
If you are familiar with C language and need to learn C++, you know that C uses formatted strings for output. The famous </b></font>Hello World program is implemented in C<font color=green><b> as follows:
[code]
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
[/code]
Unlike C, C++ makes an intensive use of strings and streams to display data.
The </b></font>Hello World program in C++<font color=green><b> looks like this:
[code]
#include <iostream>
int main()
{
cout <<"Hello World\n";
return 0;
}
[/code]
If you take a close look at the example above, you may notice at least 3 major differences to the C version of this program.
1. The header file is not <stdio.h> but [blue] <iostream> [/blue]. This is not a mistake, because the new C++ header files dont use the [blue] .h [/blue] suffix.
2. The equivalent of printf() function in C++ is cout. However, cout is not a function but an object of a class of the standard library.
3. C++ use the stream out operator [blue]<<[/blue] operator for output. The [blue] << [/blue] operator is also defined in a class of the standard library.
You can do the same things with [blue] cout<<""; [/blue] as you would do with printf().
So you can use cout for displaying strings, streams, numbers, objects etc.
[code]
cout<<"Hello Reader"<<"How are you"; //equals 2 calls of printf()
int i = 5;
cout << i; //equals printf("%d",i);
cout<<endl; //equals printf("\n");
[/code]
The opposite of cout is [blue] cin>>; [/blue]. [blue] cin>>; [/blue] equals scanf() function in C.
You should not be suprised if i tell you that cin too, is an object of a class in the standard library.
The stream in operator[blue] >> [/blue] is also implemented in a class of the standard library.
And, of course, you can do anything with cin that you would do with scanf().
[code]
#include <iostream>
int main()
{
string name;
cout<<"Whats your name?";
cin >> name;
cout << "Hello"<< name;
return 0;
}
[/code]
This sample program is straightforward. It reads the name of the user and displays it again. However, this program is a bit tricky. Again, if you know C, you might wonder where i have stored the result of cin.
The trick is, cout and cin use the same stream to read and write data. Hence, if i read data with cin, i can use this data for cout to display it.
==== [center] Variables, arrays and operators [/center] ====
In the sample program above, i have introduced another feature of C++ without explicitly mentioning it. [blue] Variables [/blue]. [blue] Variables [/blue] are used to store data in it and usually waste memory space.
In C++, all [blue] Variables [/blue] must be assigned a valid data type. The data types of C++ are the same as in C. Consider the following samples:
[code]
[green] //elementary data types [/green]
bool x; //declaring a variable of type bool
char c; //declaring a variable of type char
int i;
short s;
double d;
float f;
long l;
[green]//non-elementary data types[/green]
struct s;
class c;
enum e;
//STL special data types
[/code]
[b] Declaring vs Defining variables[/b].
In C++, there are 2 ways of introducing variables. The first way is called [b] declaration [/b] of a variable. The second is called [b] definition [/b] of a variable. The following code snippet shows the differences:
[code]
int i; //declaration of i as type int
int i = 5; //definition of variable i and assigning value 5 to it
string s; //declaration of s as type string
s = "Hello World; //assigning "Hello World" to s
string s2 = "Hello Reader"; //definition of s as string with value "Hello Reader
[/code]
Note that, when assigning values to variables, you need to make sure that the value is a [b] valid value [/b]. This means that, when, for example, you define a variable of type [b] int [/b] you have to make sure that the value you assign to it is an [b] integer [/b].
[code]
int i;
i = 5; //correct! 5 is integer
[red]
int j;
j = "Hello World" //wrong! j is not a string
[/red]
double d;
d = 3.145; //correct! 3.145 is a double
[red]
int k;
k = 1.23456; //wrong! 1.23456 is not an int
[/red]
[/code]
If you assing wrong values to variables of another data type, the compiler will complain an error.
[b] Defining [/b] a variable means to [b] declare it and assign a value to it [/b].
[b] Declaring [/b] a variable means only to introduce it to the compiler.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
