[dark blue C++] [Chapter 2] [summary of knowledge points]

0 foreword

  • Environment is ubuntu18.06
  • IDE is clion

1. Summary of knowledge points

1.1 viewing the value returned by the main function

Terminal execution:

echo $?

As follows:

  • If the operating system drops "main", the return value of "main" will be given to the operating system
bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ ./helloworld 
Hello, World!

bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ echo $?
0
  • Returning 0 means the program runs successfully
  • The main function returns a value even if it is not of void type. If no return statement is written, a 0 is returned by default
  • The return type of the main function must be int

1.2 differences between header file < > and ""

  • Using "", the system will start from the current directory to find the header file
  • Using < >, the system starts from the environment variable
  • Generally speaking, header files are self written. If they belong to the current project, they will use ""
  • Use the header file of C++ standard library, use < >, and its header file does not h/.hpp and other types of suffixes

1.3 use of input cin and output cout, cerr and clog

  • Isostream: the IO interface provided by the standard library for user interaction
  • Input stream: cin; Output stream: cout/cerr/clog
  • Differences between output streams: 1. Output target; 2. Flush buffer now
  • Buffer and buffer refresh: std:flush; std:endl;

1.3.1 cout,cerr,clog

  • Store cout and cerr information
    The codes are as follows:
#include <iostream>

int main() {
    std::cout << "out from cout";
    std::cerr << "out from cerr";
}

Compile and run the code output:

out from coutout from cerr

You can redirect the results to different files:

bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ ./helloworld >txt1 2>txt2
bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ cat ./txt1 
out from coutbupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ cat ./txt2 
out from cerrbupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ 
  • cout is the output of the program system
  • cerr is some wrong information
  • The clock and cerr are basically similar. Here is a program:
#include <iostream>

int main() {
    std::cout << "out from cout";
    std::cerr << "out from cerr";
    std::clog << "out from clog";
}

Output is:

out from coutout from cerrout from clog

Then relocate to a different file as well:

bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ ./helloworld >txt1 2>txt2
bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ cat ./txt1 
out from coutbupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug$ cat ./txt2 
out from cerrout from clog
  • The difference between cerr and clog is whether to flush the buffer immediately. Cerr flushes immediately. The log information of clog is not flushed immediately. Only when the buffer is full will it be automatically output

1.3.2 refresh buffer std::flush, std::endl

  • flush is to refresh the buffer and display the information in the buffer
  • endl is to flush the buffer and wrap the line
  • Refreshing the buffer will affect the execution speed of the program. It is not a good behavior to use a large number of refreshing buffers

1.4 noun space

1.4.1 basic usage of user-defined namespaces

  • Namespaces: used to prevent name conflicts
  • There are three ways to access elements in a namespace: domain resolver::; using statement; namespace alias
#include <iostream>

namespace Namespace1
{
    void fun()
    { }
}

namespace Namespace2
{
    void fun()
    { }
}

int main() {
    //way 1
    Namespace1::fun();

    //way 2
    //It can be written outside the function, but it is not recommended
    //Because the namespace itself is used to prevent conflicts
    using namespace Namespace2;
    fun();
    
    //way 3
    namespace ns1 = Namespace1;
    ns1::fun();

}

  • When using method 2, you should note that the namespace declaration can be written outside the function body, but it is not recommended, because the namespace itself is used to prevent noun conflicts;
  • The cpp file is OK. It is very dangerous to write in the header file, because if you import the source file of a header file written in this way, there will be a risk of noun conflict
  • The safest way to write is to use mode 1 or mode 3

1.4.2 std namespace

  • Namespaces defined by C++ Standard Libraries

1.5 view the generated by C++ files o all external links and reverse operations of documents

  • Namespace and name mangling

1.5.1 name mangling

Commands under linux:

nm ./main.cpp.o

The main All external links included in CPP:

bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug/CMakeFiles/helloworld.dir$ nm ./main.cpp.o 
                 U __cxa_atexit
                 U __dso_handle
                 U _GLOBAL_OFFSET_TABLE_
000000000000009b t _GLOBAL__sub_I__ZN10Namespace13funEv
000000000000000e T main
0000000000000052 t _Z41__static_initialization_and_destruction_0ii
0000000000000000 T _ZN10Namespace13funEv
0000000000000007 T _ZN10Namespace23funEv
                 U _ZNSt8ios_base4InitC1Ev
                 U _ZNSt8ios_base4InitD1Ev
                 U _ZSt4cerr
                 U _ZSt4clog
                 U _ZSt4cout
0000000000000000 r _ZStL19piecewise_construct
0000000000000000 b _ZStL8__ioinit
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc

among

0000000000000000 T _ZN10Namespace13funEv
0000000000000007 T _ZN10Namespace23funEv

Is the fun file of the namespace you wrote

1.5.1 de name mangling

  • Commands under linux
nm ./main.cpp.o | c++filt -t

As follows:

bupo@bupo-vpc:~/shenlan/C++/cap2/cmake-build-debug/CMakeFiles/helloworld.dir$ nm ./main.cpp.o | c++filt -t
                 U __cxa_atexit
                 U __dso_handle
                 U _GLOBAL_OFFSET_TABLE_
000000000000009b unsigned short _GLOBAL__sub_I__ZN10Namespace13funEv
000000000000000e T main
0000000000000052 unsigned short __static_initialization_and_destruction_0(int, int)
0000000000000000 T Namespace1::fun()
0000000000000007 T Namespace2::fun()
                 U std::ios_base::Init::Init()
                 U std::ios_base::Init::~Init()
                 U std::cerr
                 U std::clog
                 U std::cout
0000000000000000 r std::piecewise_construct
0000000000000000 bool std::__ioinit
                 U std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

  • The two will not change main. The reason for adaptation is that the name may conflict with other names in the program (function overloading). Main is unique and cannot have the same name, so it will not be changed

1.6 system I/O

  • C/C++ system I/O comparison
#include <iostream>
#include <cstdio>
int main()
{
    std::cout << "Hello World" << std::endl;
    printf("Hello World\n");
}

  1. printf: intuitive but error prone
  2. cout: not easy to make mistakes, but the writing is lengthy
  3. C++20 format library: a new solution
#include <iostream>
#include <cstdio>
int main()
{
    int x = 10;
    std::cout << "I have " << x << " pens\n";
    printf("I have %d pens\n", x);
}

1.7 = = techniques used, if and while loops

  • You can put the constant to the left of = = to prevent misuse. You can add a const to the constant
    As follows:
#include <iostream>

int main()
{
    const int x = 42;
    std::cout << "Please input a number: \n";

    int y = 0;
    std::cin >> y;

    if(x == y)
    {
        std::cout << "You are right!\n";
    }
    else
    {
        std::cout << "You are wrong!\n";
    }
}

  • Use a while loop for loop execution
#include <iostream>

int main()
{
    const int x = 42;
    int y = 0;

    while(x != y)
    {
        std::cout << "Please input a number: \n";
        std::cin >> y;
    }
        std::cout << "You are right!\n";
}

1.8 structure and user-defined data types

  • Structures: placing related data together
  1. You can use the dot operator (.) Accessing internal elements
  2. Can be an input parameter or return type of a function
  3. Member functions can be introduced to better represent the correlation between functions and data
#include <iostream>

struct Point
{
    int x;
    int y;
    void IncX()
    {
        x = x + 1;
    }
};

Point& fun(Point &p)
{
    p.x = p.x +1;
    return p;
}
int main()
{
    Point p;
    p.x = 3;
    p.y = 4;
    fun(p);
    std::cout << p.x << " " << p.y << '\n';
    p.IncX();
    std::cout << p.x << " " << p.y << '\n';
}

Tags: C++ clion

Posted by WorldBizEduComputerShops on Wed, 01 Jun 2022 14:05:04 +0530