Common features of 6 cpp

Keywords: C++ Programming C

Common features of 1.6 cpp

Return to directory 1 Object-Oriented Technology
Last Section 1.5 Structures and classes
Next section 1.7 Common features of classes in cpp

bool type

c + + extends a new data type: bool type.

There are only two possible values in the bool type: true or false, representing true and false, respectively.

source code

/* bool.cpp bool Type instance */
#include <iostream>

int main()
{

    bool go_out = false, go_home = true;

    if (go_out)
    {
        std::cout << "Go out today." << std::endl;
    }
    else
    {
        std::cout << "Don't go out today." << std::endl;
    }
    
    if (go_home)
    {
        std::cout << "Go home today." << std::endl;
    }
    else
    {
        std::cout << "Don't go home today." << std::endl;
    }

    return 0;

}

Compile and run

Don't go out today.
Go home today.

std:string

std::string is a string of c++, which needs:

#include <string>

String is actually a class in the c + + standard library, and the string defined is actually an object.

source code

/* string.cpp string Example */
#include <iostream>
#include <string>

int main()
{

    /* Define the beginning of a string */
    std::string str_1;
    str_1 = "111: This is the first part of the string_";

    std::string str_2 = "12: This is the second part of the string._";

    std::string str_3("113: This is the third part of the string._");

    std::string str_4(3, '6');

    std::string str_5 = str_4;
    /* Define the end of the string */

    std::cout << str_1 + str_2 + str_3 + str_4 << std::endl; // String connection

    std::cout << "str_5 The second character is:" << str_5[1] << std::endl; // [] Operator access string

    /* String comparison size */
    std::cout << "str_1 than str_2 Big:" << (str_1 > str_2) << std::endl;
    std::cout << "str_2 than str_3 Small:" << (str_2 < str_3) << std::endl;
    std::cout << "str_3 and str_4 Different:" << (str_3 != str_4) << std::endl;
    std::cout << "str_4 and str_5 Same:" << (str_4 == str_5) << std::endl;
    /* End of string comparison size */

    std::cout << "str_1 size:" << str_1.size() << std::endl;
    std::cout << "str_4 length:" << str_4.length() << std::endl;

    return 0;

}

Compile and run

111: This is the first part of the string_12: This is the second part of the string._113: This is the third part of the string._666
str_5 The second character is:
str_1 than str_2 Big: 0
str_2 than str_3 Small: 0
str_3 and str_4 Different: 1
str_4 and str_5 Same: 1
str_1 size:24
str_4 length:3

string is a class that encapsulates many practical functions to perform many convenient operations and can refer to related functions by itself.

Note: When the data type is bool, the true value outputs 1 and the false value outputs 0.

Using the'>'or'<' symbol for string objects, the actual comparison is the size of the string from left to right in character order.

Such as:

Cb > Ca

Fi > Faa

12 > 111

Pointer and reference

Reference is a new feature of c++. In some cases, we can avoid using pointers instead of references. But in essence, references are still pointers.

source code

/* reference.cpp Pointer and reference instances */
#include <iostream>

void swap_pointer(int* num_1, int* num_2) // Functions of pointer commutation integers
{
    int median_num;

    median_num = *num_1;
    *num_1 = *num_2;
    *num_2 = median_num;
}

void swap_reference(int& num_1, int& num_2) // Functions that refer to commutative integers
{
    int median_num;

    median_num = num_1;
    num_1 = num_2;
    num_2 = median_num;

}

void display_infos(std::string str_display, int num_1, int num_2)
{
    std::cout <<str_display << std::endl;
    std::cout << "num_1:" << num_1 << std::endl;
    std::cout << "num_2:" << num_2 << std::endl;
    std::cout << std::endl;
}

int main()
{

    int num_1 = 2, num_2 = 3;
    display_infos("Initial value:", num_1, num_2);

    swap_pointer(&num_1, &num_2);
    display_infos("Pointer Exchange Initial Value:", num_1, num_2);

    swap_reference(num_1, num_2);
    display_infos("Reference and swap values that have been swapped once:", num_1, num_2);

    return 0;

}

Compile and run

Initial value:
num_1:2
num_2:3

Pointer Exchange Initial Value:
num_1:3
num_2:2

Reference and swap values that have been swapped once:
num_1:2
num_2:3

References actually give variables an alias.

Reference is essentially an operation on the address, but it seems that we don't have to get bogged down in "*" but directly manipulate "ordinary variables". This reduces the visual complexity of the pointer.

const

The function of const is to make the variables modified by const unmodifiable, which can be regarded as constants.

Using references in c++, we can easily alias a variable:

int a = 3;
int &b = a;

In this case, the operation of a or b will affect the memory space they point to.

Because b corresponds to another name for a.

But references can only be initialized with variables, not constants:

int &c = 10;

This is not allowed. To implement constant initialization references, const must be used to modify:

const int &c = 10;

Const objects and variables can only be assigned to const objects and variables.

Non-const objects and variables can also be assigned to const objects and variables.

constt objects and variables cannot be modified directly.

Dynamic allocation of memory

In C language, we use malloc and free to request and release memory space.

#include <malloc.h>

In c++, we directly use new to request memory space, and delete to release memory space.

source code

/* new_delete.cpp c++Dynamic allocation of memory instances */
#include <iostream>

int main()
{

    int* num_1 = new int; // Apply for an int space and do not initialize
    *num_1 = 1; // assignment

    int* num_2 = new int(2); // Apply for an int space and initialize it

    int* array_1 = new int[10]; // Apply for ten consecutive int spaces and do not initialize to default values
    for (int i = 0; i < 10; i++)
    {
        array_1[i] = 1;
    }
    
    int* array_2 = new int[10](); // Ten consecutive int spaces are requested and initialized to default values, where only default values can be initialized and no other values are allowed.

    /* output */
    std::cout << "num_1: " << *num_1 << std::endl;
    std::cout << "num_2: " << *num_2 << std::endl;
    std::cout << std::endl;
    std::cout << "array_1: " << std::endl;
    for (int i = 0; i < 10; i++)
    {
        std::cout << array_1[i] << std::ends;
    }
    std::cout << std::endl << std::endl;
    std::cout << "array_1: " << std::endl;
    for (int i = 0; i < 10; i++)
    {
        std::cout << array_2[i] << std::ends;
    }
    /* Output end */

    delete num_1; // Release a single memory space
    delete num_2;

    delete[] array_1; // Release contiguous memory space
    delete[] array_2;

    return 0;

}

Compile and run

num_1: 1
num_2: 2

array_1: 
1 1 1 1 1 1 1 1 1 1

array_1: 
0 0 0 0 0 0 0 0 0 0

The most important thing to use new is to delete it in time after using up space, otherwise memory leaks will occur, and in serious cases, it will occupy the system memory crazily, resulting in crash.

Before I made a small game with SDL, and then forgot to release the memory. Eight G memory bars could be full in a few minutes. My friend's laptop ran for a short time and crashed directly.

Of course, terminating the program releases memory, but many times we need the program to run continuously. It is dangerous not to release memory.

overloaded function

The overloaded constructor was mentioned in the class before, and here again the overloaded function:

source code

/* overload1.cpp Example 1 of overloaded function */
#include <iostream>

int add_num(int num_1, int num_2)
{
    return num_1 + num_2;
}

double add_num(double num_1, double num_2)
{
    return num_1 + num_2;
}

int main()
{

    std::cout << add_num(1, 1) << std::endl;
    std::cout << add_num(1.1, 1.1) << std::endl;

    return 0;

}

Compile and run

2
2.2

As you can see, we use the same function name, but modify the parameter data type and return value type in the function, so we get the integer additive function and floating-point additive function with the same name.

When we fill in two integers in a function, we get the sum of integers; when we fill in two floating-point numbers in a function, we get the sum of floating-point numbers.

This is called overloading.

Function overloading mainly depends on the data type and number of parameters that are passed in:

source code

/* overload2.cpp Example 2 of overloaded function */
#include <iostream>
#include <string>

std::string str_mycat(std::string str1, std::string str2)
{
    return str1 + str2;
}

std::string str_mycat(std::string str1, std::string str2, std::string str3)
{
    return str1 + str2 + str3;
}

int main()
{

    std::cout << str_mycat("111", "222") <<std::endl;
    std::cout << str_mycat("111", "222", "333") <<std::endl;

    return 0;

}

Compile and run

111222
111222333

Return to directory 1 Object-Oriented Technology
Last Section 1.5 Structures and classes
Next section 1.7 Common features of classes in cpp

Reference material:

  • C++ Programming Podcast
  • Blog Garden
  • CSDN
  • Baidu Encyclopedia

Posted by username123 on Thu, 19 Sep 2019 21:15:03 -0700