Download C++ tutorial for C users / PDF

Download C++ tutorial for C users / PDF




Download C++ tutorial for C users / PDF









This text is aimed at C users who wish to learn C++. It is also interesting for experienced C++ users
who leaved out some features of the language.

The possibilities of C++ are briefly enunciated and illustrated. When you will try to use them for your own programs you will encounter a lot of problems and compilation errors. Come back to this text and look carefully at the examples.

Make use of the help files of your development environment. Do not hesitate to copy the examples from this text and paste them inside your development environment in order to test and modify them.

You can use / / to type a remark :
#include // This library is often used
void main () // The program's main routine.
{
double a; // Declaration of variable a.
a = 456;
a = a + a * 21.5 / 100; // A calculation.
cout << a; // Display the content of a.
}
Input from keyboard and output to screen can be performed through cout << and cin >> :
#include
void main()
{
1 of 56 12.03.99 01:19

int a; // a is an integer variable
char s [100]; // s points to a sring of 99 characters
cout << "This is a sample program." << endl;
cout << endl; // Line feed (end of line)
cout << "Type your age : ";
cin >> a;
cout << "Type your name : ";
cin >> s;
cout << endl;
cout << "Hello " << s << " you're " << a << " old." << endl;
cout << endl << endl << "Bye !" << endl;
}
Variables can be declared everywhere :
#include
void main ()
{
double a;
cout << "Hello, this is a test program." << endl;
cout << "Type parameter a : ";
cin >> a;
2 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
a = (a + 1) / 2;
double c;
c = a * 5 + 1;
cout << "c contains : " << c << endl;
int i, j;
i = 0;
j = i + 1;
cout << "j contains : " << j << endl;
}
A variable can be initialised by a calculation :
#include
void main ()
{
double a = 12 * 3.25;
double b = a + 1.112;
cout << "a contains : " << a << endl;
cout << "b contains : " << b << endl;
a = a * 2 + b;
double c = a + b * a;
3 of 56 12.03.99 01:19

cout << "c contains : " << c << endl;
}
Like in C, variables can be encapsulated between hooks :
#include
void main()
{
double a;
cout << "Type a number : ";
cin >> a;
{
int a = 1;
a = a * 10 + 4;
cout << "Local number : " << a << endl;
}
cout << "You typed : " << a << endl;
}

C++ allows to declare a variable inside the for loop declaration. It's like if the variable had
been declared just before the loop :

#include
4 of 56 12.03.99 01:19

void main ()
{
for (int i = 0; i < 4; i++)
{
cout << i << endl;
}
cout << "i contains : " << i << endl;
for (i = 0; i < 4; i++)
{
for (int i = 0; i < 4; i++) // we're between
{ // previous for's hooks
cout << i;
}
cout << endl;
}
}

A global variable can be accessed even if another variable with the same name has been declared inside the function ....

















Download C++ tutorial for C users / PDF


0 commentaires: