c++ entry notes

c++ entry notes

For those who have a Java foundation, learning C++ is not difficult, after all, C++ is the predecessor of Java.

Besides, I am also proficient in java, haha.

Install the gcc environment, follow the rookie tutorial.

The development tool ide uses vs. Although eclipse is used to it, this feels good. Very easy to use.

Some conceptual knowledge

If you use a variable name as an actual parameter, its transfer direction is one-way, while using a reference as an actual parameter its transfer direction is two-way;

The return value type of a C++ function can be any type except arrays and functions. A function can return a reference. The main purpose of describing a function as returning a reference is to use the function on the left side of the assignment operator. The representation method of the function prototype is: data type & function name (parameter list);. The return value of a function can be a memory address that stores a certain type of data, which is called a pointer function. Their general definition form is: type identifier * function name (parameter list);. Using inline functions can speed up the execution of the program, but if there are many statements in the function body, the size of the program code will increase. When a function is not too complicated, but it is required to be called frequently, an inline function should be used. . . Can be understood as a normal function

Generally speaking, I always feel that C++ is not as easy to use as Java. Maybe it is not used to it at first, so continue to explore.

Effect picture

The following is some basic code, take your time to understand. The notes are very detailed

#include <iostream>
using namespace std;

//Pass by value without changing its value
void f1(int a, int b) {
    int temp;
    temp = a;
    a = b;
    b = temp;
} 

//Pass by reference, change the value of the passed parameter.
void f2(int &a, int &b) {
    int temp;
    temp = a;
    a = b;
    b = temp;
}

void mul99() {
    int i;
    int j;
    for (i = 1; i <= 9; i++) {
        for (j = 1; j <= i; j++) {
            cout << j << "*" << i << "=" << i * j << "\t";
        }
        cout << endl;
    }
}

void star() {
    int i, j, n = 6;
    for (i = 1; i <= n; ++i)
    {
        for (j = 1; j <= n-i; ++j)
            cout << "";
        for (j = 1; j <= 2 * i-1; ++j)
            cout << "*";
        cout << endl;
    }
    for (i = 1; i <= n-1; ++i)
    {
        for (j = 1; j <= i; ++j)
            cout << "";
        for (j = 1; j <= 2 * (n-i)-1; ++j)
            cout << "*";
        cout << endl;
    }
}

void qiongju() {
    int x, y, z;
    for (x = 0; x <= 20; x++)
        for (y = 0; y <= 33; y++)
            for(z = 0; z <= 300; z++)
                if (5 * x + 3 * y + z/3 == 100 && x + y + z == 100 && z% 3 == 0)
                {
                    cout << "Chicken Weng" << x << "Chicken Mother" << y << "Chicken Chick" << z << "only" << endl;
                }
}

void arrOddEven() {
    int arrA[10];
    cout << "Please enter 10 non-zero natural numbers";
    for (int i = 0; i <10; i++) {
        cin >> arrA[i];
    }
    cout << "The odd numbers are:" << endl;
    for (int i = 0; i <10; i++) {
        if (arrA[i]% 2 == 1) {
            cout << arrA[i] << "\t";
        }
    }
    cout << endl;
    cout << "even numbers have:" << endl;
    for (int i = 0; i <10; i++) {
        if (arrA[i]% 2 == 0) {
            cout << arrA[i] << "\t";
        }
    }
    cout << endl;
}

typedef struct {
    int x;
    int y;
}Goods;

int main() {
    int a = 1;
    int b = 2;
   //value transfer
    f1(a,b); 
    cout << a << "---" << b << endl;//1 2

   //Pass by reference
    f2(a,b); 
    cout << a << "---" << b << endl;//2 1

   //99 multiplication table
    mul99();

   //sqrt square root mathematical method
    cout << sqrt(100) << endl;//Find the square root.

   //sporadic output
    star();

   //Exhaustive method
    qiongju();

   //Array to determine parity
   //arrOddEven();

   //typedef struct structure
    Goods g;
    Goods &gs = g;
    gx = 13;
    gy = 14;
    cout << gs.x << "\t" << gs.y << endl;

    system("pause");//Press any key to continue. . .
    return 100;
}

In place! ! !

Reference: https://cloud.tencent.com/developer/article/1335890 C++ Getting Started Notes-Cloud + Community-Tencent Cloud