C++ Basics

Here’s a C++ study note that covers some of the basics and important points of C++:

I. C++ Basics

1. Basic data types

  • Integer:
    • ‘int’: typically 32 bits, storing integers, e.g. 'int num = 10; `。
    • ‘long’:一般为 64 位,存储较大范围的整数,如 ‘long bigNum = 123456789012345L;’。
    • ‘short’: typically 16 bits, suitable for storing a smaller range of integers, 'short smallNum = 32767; `。
  • Floating-point:
    • ‘float’: single-precision floating-point number, low precision, stores numbers with decimals, 'float pi = 3.14f; `。
    • double:双精度浮点数,精度更高,是常用的浮点数类型,double moreAccuratePi = 3.141592653589793;
  • Character Type:
    • char:存储单个字符,如 char letter = 'A';
    • ‘string’:存储字符串,需要包含 ‘’ 头文件,‘string name = “John”;’。

2. Variables and constants

-Variable:
- When defining a variable, you need to specify the data type, and then you can assign and modify the variable, for example:
cpp int age; age = 25; age = age + 1;
- It can also be initialized directly at definition time: 'int score = 85; `。
-Constant:
- The ‘const’ keyword can define constants, and the value of the constant cannot be modified after initialization, such as:
cpp const int MAX_VALUE = 100;
- You can also use ‘#define’ to define constants, but it’s not recommended because it’s a preprocessor instruction and not type-safe:
cpp #define PI 3.14159

3. operator

  • Arithmetic Operators:
    • Include ‘+’ (add), ‘-’ (minus), ‘*’ (multiply), ‘/’ (divide), ‘%’ (remainder), etc., e.g.:
    int result = 10 + 5; 
    int remainder = 10 % 3; 
    
  • Relational Operators:
    • For example, ‘>’ (greater than), ‘<’ (less than), ‘==’ (equal to), ‘!=’ (not equal to), ‘>=’ (greater than or equal to), ‘<=’ (less than or equal to), which are often used for conditional judgment:
    if (score > 80) {
         
         
        cout << "优秀" << endl;
    }
    
  • Logical Operators:
    • ‘&&’ (logical and), ‘||’(logical or), ‘!’ (logical not), used to combine multiple conditions, such as:
    if (score >= 60 && score < 80) {
         
         
        cout << "及格" << endl;
    }
    

Second, control flow

1. Conditional statements

  • if-else 语句
    if (condition) {
         
         
        Executed when the condition is true
    } else {
         
         
        Executed when the condition is false
    }
    
    For example:
    if (age >= 18) {
         
         
        cout << "成年" 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

༺ཌༀ傲穹_Vortexༀད༻

你的鼓励奖是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值