[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CPP-Beginners-Tutorial
Displaying differences between revision 27 and the latest revision
= C++ Programming Tutorial =
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++.
== Formatted printing in C++ ==
If you are familiar with C language and need to learn C++, you know that C uses formatted strings for output. The famous Hello World program is implemented in C 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 Hello World program in C++ looks like this:
[code]
#include <iostream>
int main()
{
std::cout <<"Hello World\n";
}
[/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 iostream. 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++ uses 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 cin. cin equals the 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;
}
[/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.
Note that there are third-party libraries that provide C's printf/scanf like formatted I/O with C++ streams. An example is the iof library. ¶
¶
== Variables, arrays and operators ==
In the sample program above, i have introduced another feature of C++ without explicitly mentioning it. Variables. Variables are used to store data in it and usually waste memory space.
In C++, all Variables 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]
== Declaring vs Defining variables ==
In C++, there are 2 ways of introducing variables. The first way is called declaration of a variable. The second is called definition 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 int you have to make sure that the value you assign to it is an integer.
[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.
Defining a variable means to declare it and assign a value to it.
Declaring a variable means only to introduce it to the compiler.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CPP-Beginners-Tutorial
Displaying differences between revision 27 and the latest revision
= C++ Programming Tutorial =
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++.
== Formatted printing in C++ ==
If you are familiar with C language and need to learn C++, you know that C uses formatted strings for output. The famous Hello World program is implemented in C 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 Hello World program in C++ looks like this:
[code]
#include <iostream>
int main()
{
std::cout <<"Hello World\n";
}
[/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 iostream. 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++ uses 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 cin. cin equals the 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;
}
[/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.
Note that there are third-party libraries that provide C's printf/scanf like formatted I/O with C++ streams. An example is the iof library. ¶
¶
== Variables, arrays and operators ==
In the sample program above, i have introduced another feature of C++ without explicitly mentioning it. Variables. Variables are used to store data in it and usually waste memory space.
In C++, all Variables 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]
== Declaring vs Defining variables ==
In C++, there are 2 ways of introducing variables. The first way is called declaration of a variable. The second is called definition 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 int you have to make sure that the value you assign to it is an integer.
[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.
Defining a variable means to declare it and assign a value to it.
Declaring a variable means only to introduce it to the compiler.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
