C++ Object Oriented langage

Now that we know well the C language, learning C + + is much easier. This language makes some improvements over the language such as C strings easier, greater use of programming and classes.

C or C++

All you can do in C, you also can do in C++.

C++ is based on C, so you can write a code in C and compile it with a C++ compiler.

Hello world!! Reloaded

We now edit the file main.cpp (cpp means C++).

#include <iostream>
using namespace std;

int main(int argc, char *argv[]){
    /*I write Hello World !!*/
    cout << "Hello world !!\n" << endl;
    return 0;
}

Some difference with C code:

  • We don’t include stdio.h but iostream, a C++ library, which is composed of several namespaces. We choose to use the namespace std (to use cout).

  • The main function is the same

  • We use flux for display output, here cout.

  • The end of the main function is the same.

We will now compile and run the code:

make main
./main

The make command know you want to compile a C++ file.

Hello world !!

Pointers

Pointers are naturally enable in C too. You can do it in the same way than in C, or in the C way

#include <iostream>

int main(void){
    int N = 10;
    double *d = NULL;
    d = new double[N];
    for (int i = 0; i < N; i++){
        d[i] = i+1;
    }
    for (int i = 0; i < N; i++){
        std::cout << d[i] << std::endl;
    }
    delete[] d;
    d = NULL;
    return 0;
}

The result of this code is:

1
2
3
4
5
6
7
8
9
10
  • Here, we don’t define the std namespace, so we have to define it when we use a function of the std namespace: std::cout for example.

  • In C++, we can declare a variable directly in a loop.

  • The keyworks new and delete manage the pointers.

Classes

Classes are very useful in C++, but in FreeFem they are not useful (excepted for programmers). We just have a look on that.

#include <iostream>
using namespace std;

class Object{
    private:
    int id;
    std::string txt;

    public:
    Object();
    int get_id();
    std::string get_txt();
};

Object::Object(){
    id = 1;
    txt = "Hello";
}

int Object::get_id(){
    return id;
}

std::string Object::get_txt(){
    return txt;
}

int main(void){
    Object test_object = Object();
    cout << test_object.get_id() << endl;
    cout << test_object.get_txt() << endl;
    return 0;
}

We define a class using the keyword class. In this class, we have defined attributes : id and txt, and equally methods : get_id() and get_txt(). Object() function is the initializer.

Methods are describes with the name of the class Object::.

The class can be used in the main function.

We speak about OOP: Object-Oriented Programming

Result:

1
Hello

Read and write

To read or write a file in C++, you can use the flux.

#include <iostream>
#include <fstream>
using namespace std;

int main(void){
    ofstream file2write("test.txt", ios::out | ios::trunc);
    if (file2write){
        file2write << "Test";
        file2write.close();
    }

    ifstream file2read("test.txt", ios::in);
    if (file2read){
        string line;
        getline(file2read, line);
        cout << line << endl;
        file2read.close();
    }

    return 0;
}
  • You must include the fstream library for using files.

  • For open a file to write, use ofstream.

  • You can write in a file using flux, or with others functions.

  • For open a file to read, use ifstream.

  • You can use getline to read a file, or flux too.

  • Don’t forget to close the file when you have finished.

Results:

Test