在本章中,我们将学习以下主题:
- C ++限定词
- C ++国彩网符
1. C ++限定词
C ++提供了两个限定符之一。
- 易挥发的:将变量定义为易失性时,可以更改值。默认情况下,如果创建变量,则将其声明为“易失性”变量。
“易挥发的”是关键字。
例:
易挥发的 int num = 10; num = 20; // correct
- 不变:当将变量声明为常量时,无法更改分配给该变量的值。
“ const”是缩略词。
例:
const int num = 10; num = 20; // error. as int is a constant.
2. C ++国彩网符
以下是CPP中的国彩网符类型:
- 关系国彩网符
- 逻辑国彩网符
- 算术国彩网符
- 按位国彩网符
- 移位国彩网符
- 一元国彩网符
- 赋值国彩网符
-
关系国彩网符:
关系国彩网符用于了解2个变量之间的关系。以下是可用的国彩网符。
== checks if two values are same. If same it will return true. != checks if both variables are not equals to each other. Then it will return true. > Checks if the value of Left is greater than the value to right. Then it will return true < Checks if the value of Left is lesser than the value to right. Then it will return true. >= Checks if the value of Left is greater than or euqall the value to right. Then it will return true. <= Checks if the value of Left is lesser than or equall to the value to right. Then it will return true.
C ++中的关系国彩网符示例
/* * File : Relational _operators.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; int num_2 = 20; cout<<"The result for 关系国彩网符 will always be 0 and 1. 0 means False and 1 means true"<<endl; cout<<"The value of "<<num_1 << " == " << num_2<< " is = "<<(num_1 == num_2)<<endl; cout<<"The value of "<<num_1 << " <= " << num_2<< " is = "<<(num_1 <= num_2)<<endl; cout<<"The value of "<<num_1 << " >= " << num_2<< " is = "<<(num_1 >= num_2)<<endl; cout<<"The value of "<<num_1 << " != " << num_2<< " is = "<<(num_1 != num_2)<<endl; cout<<"The value of "<<num_1 << " < " << num_2<< " is = "<<(num_1 < num_2)<<endl; cout<<"The value of "<<num_1 << " > " << num_2<< " is = "<<(num_1 > num_2)<<endl; return 0; }
输出:
The value of 10 == 20 is = 0 The value of 10 <= 20 is = 1 The value of 10 >= 20 is = 0 The value of 10 != 20 is = 1 The value of 10 < 20 is = 1 The value of 10 > 20 is = 0
2.逻辑国彩网符
逻辑国彩网符基于这两个语句给出输出。以下是可用的国彩网符:
- &&逻辑和国彩网符。
如果两个语句都为真,则条件为真。如果任何一条语句为假,则它将返回假。
例:
int a = 10; int b = 20; int c = 30; if( c>b && c > a ) // This condition is true
- ||逻辑或国彩网符。 如果满足任何一个条件,它将返回true。
if( a>b || c > a )
这里第一个条件为假,但第二个条件为真。当我们使用“或”国彩网符时,结果为true。
- !逻辑非国彩网符。
它将使结果相反。
if (!true) result will be false.
C ++中逻辑国彩网符的示例
/* * File : Logical_operators.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> using namespace std; int main() { int a = 10; int b = 20; int c = 30; cout<<"The result for 逻辑国彩网符 will always be 0 and 1. 0 means False and 1 means true"<<endl; cout<<"The value of ( c > b && c > a ) is = " << ( c>b && c > a ) <<endl; cout<<"The value of ( a>b || c > a ) is = " << ( a > b || c > a ) <<endl; cout<<"The value of ( !true ) is = " << ( !true ) <<endl; cout<<"The value of ( !false ) is = " << ( !false ) <<endl; return 0; }
输出:
The result for 逻辑国彩网符 will always be 0 and 1. 0 means False and 1 means true The value of ( c > b && c > a ) is = 1 The value of ( a>b || c > a ) is = 1 The value of ( !true ) is = 0 The value of ( !false ) is = 1
-
算术国彩网符
+ Addition operator - Subtraction Operator * Multiplication Operator / Division Operator % Modulus Operator
C ++中无主题国彩网符的示例
/* * File : Arithmetic_operators.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; int num_2 = 20; cout<<"The value of "<<num_1 << " + " << num_2<< " is = "<<(num_1 + num_2)<<endl; cout<<"The value of "<<num_1 << " - " << num_2<< " is = "<<(num_1 - num_2)<<endl; cout<<"The value of "<<num_1 << " / " << num_2<< " is = "<<(num_1 / num_2)<<endl; cout<<"The value of "<<num_1 << " * " << num_2<< " is = "<<(num_1 * num_2)<<endl; cout<<"The value of "<<num_1 << " % " << num_2<< " is = "<<(num_1 % num_2)<<endl; return 0; }
输出:
The value of 10 + 20 is = 30 The value of 10 - 20 is = -10 The value of 10 / 20 is = 0 The value of 10 * 20 is = 200 The value of 10 % 20 is = 10
4.按位国彩网符
& Bitwise And | Bitwise Or ^ Bitwise NOT >> Bitwise Left shift << Bitwise Right Shift ~ Ones complement
C ++中按位国彩网符的示例
/* * File : Bitwise_operators.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; int num_2 = 20; cout<<"The value of "<<num_1 << " & " << num_2<< " is = "<<(num_1 & num_2)<<endl; cout<<"The value of "<<num_1 << " | " << num_2<< " is = "<<(num_1 | num_2)<<endl; cout<<"The value of ~ " << num_1<< " is = "<<~num_1<<endl; cout<<"The value of " << num_1<< " << 2 is = "<< num_1<<2 <<endl; cout<<"The value of " << num_1 << " >> 2 is = "<< (num_1>>2) <<endl; return 0; }
输出:
The value of 10 & 20 is = 0 The value of 10 | 20 is = 30 The value of ~ 10 is = -11 The value of 10 << 2 is = 102 The value of 10 >> 2 is = 2
6.一元国彩网符
++ Increment Operator -- Decrement Operator
增量国彩网符:
有两种类型:
- 预递增(++ num)。在此,值将递增,然后将分配值。
- 后增量(num ++)。此处将分配值,然后递增。
- 递减前(–num)。在此,该值将减一,然后分配该值。
- 后减量(数字–)。此处将分配值,然后再减一。
C ++中一元国彩网符的示例
/* * File : Unary_operators.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> using namespace std; int main() { int num_1 = 1; int num_2 = 1; int num_3 = 1; int num_4 = 1; cout<<"The value of num_1++ is = "<< num_1++<<endl; cout<<"The value of num_2-- is = "<< num_2--<<endl; cout<<"The value of ++num_3 is = "<< ++num_3<<endl; cout<<"The value of --num_4 is = "<< --num_4<<endl; return 0; }
输出:
The value of num_1++ is = 1 The value of num_2-- is = 1 The value of ++num_3 is = 2 The value of --num_4 is = 0
7.赋值国彩网符
= Equal to operator += Add and assign operator -= Subtract and assign operator *= Multiply and assign operator /= Divide and assign operator <<= Left shift and assign operator >>= Right Shift and assign operator &= “Bitwise And” and assign operator |= Bitwise Or and assign operator ^= Bitwise exclusive or and assign operator
C ++中赋值国彩网符的示例
/* * File : Relational _operators.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; cout<<"The value of "<<num_1 << " = " << 20<< " is = "<<(num_1 = 20)<<endl; cout<<"The value of "<<num_1 << " += " << 20<< " is = "<<(num_1 += 20)<<endl; cout<<"The value of "<<num_1 << " -= " << 20<< " is = "<<(num_1 -= 20)<<endl; cout<<"The value of "<<num_1 << " *= " << 20<< " is = "<<(num_1 *= 20)<<endl; cout<<"The value of "<<num_1 << " /= " << 20<< " is = "<<(num_1 /= 20)<<endl; cout<<"The value of "<<num_1 << " <<= " << 20<< " is = "<<(num_1 <<= 20)<<endl; cout<<"The value of "<<num_1 << " >>= " << 20<< " is = "<<(num_1 >>= 20)<<endl; cout<<"The value of "<<num_1 << " &= " << 20<< " is = "<<(num_1 &= 20)<<endl; cout<<"The value of "<<num_1 << " |= " << 20<< " is = "<<(num_1 |= 20)<<endl; cout<<"The value of "<<num_1 << " ^= " << 20<< " is = "<<(num_1 ^= 20)<<endl; return 0; }
输出:
The value of 10 = 20 is = 20 The value of 20 += 20 is = 40 The value of 40 -= 20 is = 20 The value of 20 *= 20 is = 400 The value of 400 /= 20 is = 20 The value of 20 <<= 20 is = 20971520 The value of 20971520 >>= 20 is = 20 The value of 20 &= 20 is = 20 The value of 20 |= 20 is = 20 The value of 20 ^= 20 is = 0
该网站上可用的教程列表:
C编程20+章 | C ++编程80+章 |
100多个编码问题 | 数据结构和算法85+章 |
系统设计20+章 | Shell脚本编写12章 |
4g LTE 60+章节 | 最常见的编码问题 |
5G NR 50+章 | Linux系统编程20+章 |