Post

Aprendendo C++

Segue abaixo um resumão dos principais tópicos sobre a linguagem de programação C++

1
2
3
4
5
6
#include <iostream> // (C++)
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include <string> // (C++)
#include <sstream> // (C++)

Usando using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void input_console()
{
    // we can use a keyword 'using' to avoid declare namespace explicity.
    using namespace std; // (C++)
    // input console
    int a, b, sum;
    cout << "Add first number:" << endl;
    cin >> a;
    cout << "Add second number:" << endl;
    cin >> b;

    sum = a + b;
    cout << sum;
}

Iniciando valores em variáveis:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void initialize_vars()
{
    
    int x {10}; // uniform (C++)
    int y (10); // constructor (C++)
    std::string my_string; // string (C++)
    my_string = "My String!";
    bool b = false; // bool (C++)

    // automatically (C++)
    auto i = 22;
    auto f = 23.1f;
    auto t = true;
    auto s = "Hello World";
    auto ull = -1ull; // force unsigned long long
    auto ui = 2u; // force unsigned int
    auto ex = 6.02e23; // potencial

    std::cout << x << std::endl;
    std::cout << y << std::endl;
    std::cout << b << std::endl;
    std::cout << i << std::endl;
    std::cout << f << std::endl;
    std::cout << t << std::endl;
    std::cout << s << std::endl;
    std::cout << my_string << std::endl;
    std::cout << ull << std::endl;
    std::cout << ui << std::endl;
    std::cout << ex << std::endl;
}

Valores nulos:

1
2
3
4
5
6
7
8
9
10
void nullable()
{
    auto n = NULL; // NULL is macro to ZERO (C style)
    // auto n = 0;

    // nullptr is c++ way.
    int *number = nullptr; // (C++)
    
    std::cout << n << std::endl;
}

Constantes:

1
2
3
4
5
6
7
8
9
void constants()
{
    const int x = 10; // compile-time (checker)
    // x = 20; <- ERROR!
#define MY_CONST 20 // preprocessor (nochecker)

    std::cout << x << std::endl;
    std::cout << MY_CONST << std::endl;
}

Tamanhos de dados:

1
2
3
4
5
6
7
8
9
10
11
12
13
void sizes()
{
    // returns size in bytes,
    // can be a type or a variable
    int c = sizeof(char);
    int n = 10;
    int i = sizeof(n);
    int ul = sizeof(unsigned long long);

    std::cout << c << std::endl;
    std::cout << i << std::endl;
    std::cout << ul << std::endl;
}

Entrada e saída de dados.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void get_lines()
{
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    using std::stringstream;

    string mystr;
    float price = 0;
    int quantity = 0;

    cout << "Enter price: ";
    // get the user input and insert into string
    // using stream.
    getline(cin, mystr);

    // put the string value into var.
    // stringstream allows string to be treated as a stream
    stringstream(mystr) >> price;
    cout << "Enter quantity: ";
    getline(cin, mystr);
    stringstream(mystr) >> quantity;
    cout << "Total price: " << price * quantity << endl;
}

Passando argumentos por valor ou referência:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// int x: by value (copy), must be used by primitive types
// int a: by reference (no copy - read/write), effiency, must be used by compound types.
// int b: by reference (no copy - read only), effiency, must be used by compound types.
//   and garantee that no one change the value, even passed by reference!
// int c (C++): idem a, but shorthand of declaring pointers and access the contnet
int pass_by_value_reference(int x, int *a, const int *b, int &c)
{
    x = 10;
    *a = 20;
    // *b = 30; // Error!
    c = 30;

    return x + *b + *a + c;
}

Standard input/output:

1
2
3
4
5
6
7
8
9
10
11
12
13
void input()
{
    // for reading a input with fundamental types.
    std::cout << "Digit a number:\n";
    int a;
    std::cin >> a;
    std::cout << "integer is" << a << std::endl;

    // for reading a entire string line, uses getline.
    std::string s;
    getline(std::cin, s);
    std::cout << "string is" << s << std::endl;
}

Funções inline:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// inline tells to compiler that this function could be a simple generated code
// put at function caller. Sometimes, the compiler can decide to use inline in
// functions that not marked as inline, but looks like inline.
// We used generally on simple functions.
inline std::string concat(const std::string *a, const std::string *b)
{
    return *a + *b;
}

// idem previous functions, but using shorthand pointers introduced in C++
inline std::string concat2(const std::string &a, const std::string &b)
{
    return a + b;
}

Valores padrões em parâmetros:

1
2
3
4
void default_value(int a, int b = 10) // (C++)
{
    std::cout << a + b << std::endl;
}

Definindo cabeçalho de funções:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// In c/c++ we must declare the function before caller. So, in this example
// we can call 'prototype' implementation even it is below the main function
// (the caller in this case);
// BUT, there's a catch! If you forgot to declare the implementation, you'll
// receive a linker error.
// So, this is one of many reasons that developers uses header file for
// prototyping functions.
void prototype(int n);
void prototype2(int, int); // It's allow declare without name, just type.

int main()
{
    // input_console();
    initialize_vars();
    nullable();
    constants();
    sizes();
    // get_lines();

    int x = 1;
    int a = 1;
    int b = 5;
    int c = 1;
    std::cout << pass_by_value_reference(x, &a, &b, c) << std::endl;
    std::cout << x << std::endl;
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;

    // input();
  
    std::string left_s = "A";
    std::string left_r = "B";
    std::cout << concat(&left_s, &left_r) << std::endl;
    std::cout << concat2(left_s, left_r) << std::endl;

    default_value(10, 10);
    default_value(7);

    prototype(2);
    // can not call "prototype2" because we don't implemented yet!
    // prototype2(2, 3); 

    // TODO; Why exists XXXlib.h and <cXXXXlib>??? 
    // #include <stdlib.h>
    // #include <cstdlib>
    return EXIT_SUCCESS;
}

// In c/c++ we must declare the function before caller. So, in this example
// we must to have a definition of the function to call prototype below the
// main function (the caller in this case)

void prototype(int x)
{
    std::cout << "Prototype impl: " << x << std::endl;
}
Esta postagem está licenciada sob CC BY 4.0 pelo autor.