C++-basic variables and type definitions

Variable type

Basic variable type

short     //短整形,占两个字节
long      //长整型,占4个字节
int       //整形,2或4个字节
long long //长长整形,8个字节
char      //字符类型,1个字节
bool      //布尔值,1个字节,true或者false
float     //浮点数,单精度,4个字节
double    //浮点数,双精度,4个字节

Signed variable

unsigned xxx  //无符号,正值
xxx           //有符号,正负

constant

There are two ways to define constants:

  • method one:
const int red_score = 50;
  • Way two:
#define red_score = 50

Automatic variable

The compiler automatically infers the type of the variable based on the initial value assigned to the variable.

auto index = 1;
auto rate = 500 / 0.3;

Array

One-dimensional array

int past[5] = {
    
    1,2,3,4,5}

Two-dimensional array

int frid[2, 3] // 2行3列

Multidimensional Arrays

int cube[5,3,4] 

Operator

Assignment operator

grade = 96

Combination operator

Self-assignment addition operator

score+= 10

Increment and decrement operators

score ++;   等价于  score = score + 1;
score --;   等价于  score = score - 1;

Prefix operator and postfix operator

//效果相同,都是将count加1
++count;
count++;

The difference between the prefix operator and the postfix operator is that the postfix operator is executed after the assignment.

Logical operation

And operation

if ((x == 5) && (y == 5))

Or operation

if ((x == 5) || (y == 5))

Non-operation

if (!(grade < 70))

function

Function declaration

int findArea(int length, int width);
  • Return value: int
  • Function name: findArea
  • The name and type of the two formal parameters: int length, int width

Specific usage method:

#include <iostream>

//声明函数
int findArea(int length, int width);

int main()
{
    
    
    int length;
    int width;
    int area;

    std::cout << "\nWidth: ";
    std::cin >> width;
    std::cout << "\nlength: ";
    std::cin >> length;

    area = width * length;

    std::cout << "\nthe area is " << area;
    return 0;
}

//定义函数
int findArea(int l, int w)
{
    
    
    return l*w;
}

class

Create a basic class

#include <iostream>

class Tricycle
{
    
    
    public:
        Tricycle(int initialSpeed);
        int getSpeed();
        void setSpeed(int speed);
        void pedal();
        void brake();
      
    private:
        int speed;
};

// 初始化类
Tricycle::Tricycle(int initialSpeed)
{
    
    
    setSpeed(initialSpeed);
    std::cout << "\niniting; tricycle speed: " << speed << "m/s\n";
}

// 获取当前速度
int Tricycle::getSpeed()
{
    
    
    return speed;
}

// 设置速度
void Tricycle::setSpeed(int newSpeed)
{
    
    
    if(newSpeed >= 0)
    {
    
    
        speed = newSpeed;
    } 
}

// 踩踏板
void Tricycle::pedal()
{
    
    
    setSpeed(speed + 1);
    std::cout << "\nPedaling; tricycle speed: " << speed << "m/s\n";
}

void Tricycle::brake()
{
    
    
    setSpeed(speed - 1);
    std::cout << "\nBraking; tricycle speed: " << speed << "m/s\n";
}

int main()
{
    
    
    Tricycle myBike(0);
    myBike.setSpeed(0);
    myBike.pedal();
    myBike.pedal();
    myBike.brake();
    myBike.brake();
    myBike.brake();
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45779334/article/details/114664666