ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

C ++第5章:C ++限定词和C ++国彩网符

前开发者教程 2020年1月26日

在本章中,我们将学习以下主题:

  1. C ++限定词
  2. C ++国彩网符

 

1. C ++限定词

C ++提供了两个限定符之一。

  1. 易挥发的:将变量定义为易失性时,可以更改值。默认情况下,如果创建变量,则将其声明为“易失性”变量。

 

“易挥发的”是关键字。

例:

易挥发的 int num = 10;
                  num = 20; // correct

 

 

  1. 不变:当将变量声明为常量时,无法更改分配给该变量的值。

“ const”是缩略词。

例:

const int num = 10;
                num = 20; // error. as int is a constant.

 

2. C ++国彩网符

 

以下是CPP中的国彩网符类型:

 

  1. 关系国彩网符
  2. 逻辑国彩网符
  3. 算术国彩网符
  4. 按位国彩网符
  5. 移位国彩网符
  6. 一元国彩网符
  7. 赋值国彩网符

 

  1. 关系国彩网符:

 

关系国彩网符用于了解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.逻辑国彩网符

逻辑国彩网符基于这两个语句给出输出。以下是可用的国彩网符:

 

  1. &&逻辑和国彩网符。

 

如果两个语句都为真,则条件为真。如果任何一条语句为假,则它将返回假。

 

例:

int a = 10;
int b = 20;
int c = 30;



if( c>b && c > a ) // This condition is true

 

  1. ||逻辑或国彩网符。 如果满足任何一个条件,它将返回true。
if( a>b || c > a )

这里第一个条件为假,但第二个条件为真。当我们使用“或”国彩网符时,结果为true。

 

  1. !逻辑非国彩网符。

它将使结果相反。

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

 

  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

增量国彩网符:

有两种类型:

  1. 预递增(++ num)。在此,值将递增,然后将分配值。
  2. 后增量(num ++)。此处将分配值,然后递增。

 

  1. 递减前(–num)。在此,该值将减一,然后分配该值。
  2. 后减量(数字–)。此处将分配值,然后再减一。

 

 

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+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的
<col id="Dpi4hZd" class="DvhZTQO"><details id="lpC1LR2"></details></col>


    <ins class="cs9VgtM"><big id="xOkAqLT" class="x4JMMPt"><q id="obXJyGI" class="o6EB8OF"><acronym id="ou1917v" class="oSkF6td"></acronym></q></big></ins>

    1. <a id="k4TBktE" class="kdHhZhQ"><label id="OznhtHt"></label></a>

    2. <dl class="RURBBD8"></dl>

      <source id="y7oqw2S" class="yLIHn6R"></source>
      <ruby class="P2wgj8k"></ruby>