Topic : C++ tutorial for C users
Author : Eric Brasseur
Page : 1 Next >>
Go to page :


C++ tutorial for C users



This text enunciates and illustrates features and basic principles of C++. It is aimed at experienced C users who wish to learn C++. It can also be interesting for C++ users who leaved out some possibilities of the language.


// for one-line remarks

Console input and output streams

Variable declarations can be put inside the code without using hooks

Variables can be initialised by a calculation involving other variables

Variables can be declared inside a for loop declaration

Global variables can be accessed even if a local variables has the same name

It is possible to declare a REFERENCE towards another variable

A function can be declared inline

The exception structure has been added

A functions can have default parameters

PARAMETERS OVERLOAD: several functions can be declared with the same name provided there is a difference in their parameters list

The symbolic operators (+ - * / ...) can be defined for new data types

Different functions for different data types will automatically be generated provided you define a template function

The keywords new and delete are much better to allocate and deallocate memory

A class is a struct upon which you can define METHODS

The CONSTRUCTOR and the DESTRUCTOR can be used to initialise and destroy an instance of a class

The COPY CONSTRUCTOR allows to define an appropriate = operator

The method bodies can be defined below the class definition

The keyword this is a pointer towards the instance a method is acting upon

Arrays of instances can be declared

An example of complete class declaration

static variables inside a class definition

const variables inside a class definition

A class can be DERIVED from another class

If a method is declared virtual the program will always first check the type of an instance that is pointed to and will use the appropriate method.

A class can be derived from more than one base class

Class derivation allows to write generic methods

ENCAPSULATION: public, protected and private

Brief examples of file I/O

Character arrays can be used like files

An example of formated output


1
You can use / / to type a remark:


#include <iostream.h>        // 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.
}



2
Input from keyboard and output to screen can be performed through cout << and cin >>:


#include <iostream.h>

void main()
{
   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;
}



3
Variables can be declared everywhere inside the code without using hooks:


#include <iostream.h>

void main ()
{
   double a;

   cout << "Hello, this is a test program." << endl;

   cout << "Type parameter a: ";
   cin >> a;

   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;
}


4
A variable can be initialised by a calculation involving other variables:


#include <iostream.h>

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;

   cout << "c contains: " << c << endl;
}



Like in C, variables can be encapsulated between hooks:


#include <iostream.h>

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;
}



5
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 <iostream.h>

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;
   }
}



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


#include <iostream.h>

double a = 128;

void main ()
{
   double a = 256;

   cout << "Local a: " << a << endl;
   cout << "Global a: " <<::a << endl;
}



7
It is possible to make one variable be another:


#include <iostream.h>

void main ()
{
   double a = 3.1415927;

   double &b = a;                            // b IS a

   b = 89;

   cout << "a contains: " << a << endl;     // Displays 89.
}



(If you are used at pointers and absolutely want to know what happens, simply think double &b = a is translated to double *b = &a and all subsequent b are replaced by *b.)

The value of REFERENCE b cannot be changed after its declaration. For example you cannot write, a few lines further, &b = c expecting now b IS c. It won't work.
Everything is said on the declaration line of b. Reference b and variable a are married on that line and nothing will separate them.

References can be used to allow a function to modify a calling variable:


#include <iostream.h>

void change (double &r, double s)
{
   r = 100;
   s = 200;
}

void main ()
{
   double k, m;

   k = 3;
   m = 4;

   change (k, m);

   cout << k << ", " << m << endl;        // Displays 100, 4.
}



If you are used at pointers in C and wonder how exactly the program above works, here is how the C++ compiler translates it (those who are not used at pointers, please skip this ugly piece of code):

#include <iostream.h>

void change (double *r, double s)
{
   *r = 100;
   s = 200;
}

void main ()
{
   double k, m;

   k = 3;
   m = 4;

   change (&k, m);

   cout << k << ", " << m << endl;        // Displays 100, 4.
}



A reference can be used to let a function return a variable:

#include <iostream.h>

double &biggest (double &r, double &s)
{
   if (r > s)


Page : 1 Next >>