当前位置:实例文章 » 其他实例» [文章]【C++ 重要知识点总结】表达式

【C++ 重要知识点总结】表达式

发布人:shili8 发布时间:2025-02-04 04:03 阅读次数:0

**C++重要知识点总结**

**表达式**

在 C++ 中,表达式是指一个值的计算结果。表达式可以是常量、变量、函数调用、运算符等组合而成。

###1. 简单表达式简单表达式包括以下几种:

* 常量:整数、浮点数、字符等。
* 变量:已声明的变量名。
* 字面值:字符串、布尔值等。

cppint a =5; // 简单表达式:a 是一个整数常量


###2. 一元运算符一元运算符是作用于一个操作数的运算符。C++ 中的一元运算符包括:

* `+`:正号。
* `-`:负号。
* `!`:逻辑非。

cppint a =5; // 一元运算符:a 的值为 -acout << "-a = " << -a << endl;


###3. 二元运算符二元运算符是作用于两个操作数的运算符。C++ 中的二元运算符包括:

* `+`:加法。
* `-`:减法。
* `*`:乘法。
* `/`:除法。
* `%`:取余。

cppint a =5, b =3; // 二元运算符:a + b、a - b、a * b、a / b、a % bcout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;
cout << "a % b = " << a % b << endl;


###4. 三元运算符三元运算符是作用于三个操作数的运算符。C++ 中的三元运算符包括:

* `?:`:条件表达式。

cppint a =5, b =3; // 三元运算符:a > b ? "大" : "小"
cout << (a > b ? "大" : "小") << endl;


###5. 逻辑运算符逻辑运算符是作用于两个或多个操作数的运算符。C++ 中的逻辑运算符包括:

* `&&`:逻辑与。
* `||`:逻辑或。
* `!`:逻辑非。

cppint a =5, b =3; // 逻辑运算符:a > b && a < 10、a > b || a < 10、!(a > b)
cout << (a > b && a < 10 ? "true" : "false") << endl;
cout << (a > b || a < 10 ? "true" : "false") << endl;
cout << (!(a > b) ? "true" : "false") << endl;


###6. 位运算符位运算符是作用于两个操作数的运算符。C++ 中的位运算符包括:

* `&`:按位与。
* `|`:按位或。
* `^`:按位异或。
* `~`:按位非。

cppint a =5, b =3; // 位运算符:a & b、a | b、a ^ b、~acout << (a & b) << endl;
cout << (a | b) << endl;
cout << (a ^ b) << endl;
cout << (~a) << endl;


###7. 移位运算符移位运算符是作用于一个操作数的运算符。C++ 中的移位运算符包括:

* `<<`:左移。
* `>>`:右移。

cppint a =5; // 移位运算符:a << 1、a >>1cout << (a << 1) << endl;
cout << (a >>1) << endl;


###8. 成员访问运算符成员访问运算符是作用于结构或类的成员的运算符。C++ 中的成员访问运算符包括:

* `.`:点运算符。
* `->`:箭头运算符。

cppstruct Person {
 int age;
 string name;
};

Person p; // 成员访问运算符:p.age、p.namecout << (p.age) << endl;
cout << (p.name) << endl;


###9. 函数调用运算符函数调用运算符是作用于函数的运算符。C++ 中的函数调用运算符包括:

* `()`:括号。

cppvoid print(int a, int b); // 函数调用运算符:print(a, b)
print(5,3);


###10. sizeof 运算符sizeof 运算符是作用于类型的运算符。C++ 中的sizeof 运算符包括:

* `sizeof`:sizeof。

cppint a; // sizeof 运算符:sizeof(a)
cout << (sizeof(a)) << endl;


###11. typeid 运算符typeid 运算符是作用于类型的运算符。C++ 中的typeid 运算符包括:

* `typeid`:typeid。

cppint a; // typeid 运算符:typeid(a)
cout << (typeid(a)) << endl;


###12. const_cast 运算符const_cast 运算符是作用于类型的运算符。C++ 中的const_cast 运算符包括:

* `const_cast`:const_cast。

cppint a; // const_cast 运算符:const_cast(a)
cout << (const_cast(int*)(a)) << endl;


###13. dynamic_cast 运算符dynamic_cast 运算符是作用于类型的运算符。C++ 中的dynamic_cast 运算符包括:

* `dynamic_cast`:dynamic_cast。

cppclass A { virtual void foo() =0; };
class B : public A { void foo() override {} };

A* a = new B(); // dynamic_cast 运算符:dynamic_cast(a, B*)
cout << (dynamic_cast(a)) << endl;


###14. static_cast 运算符static_cast 运算符是作用于类型的运算符。C++ 中的static_cast 运算符包括:

* `static_cast`:static_cast。

cppint a; // static_cast 运算符:static_cast(a, int*)
cout << (static_cast<int*>(a)) << endl;


###15. reinterpret_cast 运算符reinterpret_cast 运算符是作用于类型的运算符。C++ 中的reinterpret_cast 运算符包括:

* `reinterpret_cast`:reinterpret_cast。

cppint a; // reinterpret_cast 运算符:reinterpret_cast(a, int*)
cout << (reinterpret_cast<int*>(a)) << endl;


###16. new 运算符new 运算符是作用于类型的运算符。C++ 中的new 运算符包括:

* `new`:new。

cppint a; // new 运算符:new(a)
cout << (new int()) << endl;


###17. delete 运算符delete 运算符是作用于类型的运算符。C++ 中的delete 运算符包括:

* `delete`:delete。

cppint a; // delete 运算符:delete(a)
cout << (delete int()) << endl;


###18. this 指针this 指针是作用于类的运算符。C++ 中的this 指针包括:

* `this`:this。

cppclass A { void foo() { cout << (this) << endl; } };
A a; // this 指针:a.foo()


###19. sizeofof 运算符sizeofof 运算符是作用于类型的运算符。C++ 中的sizeofof 运算符包括:

* `sizeofof`:sizeofof。

cppint a; // sizeofof 运算符:sizeofof(a)
cout << (sizeofof(a)) << endl;


###20. typeidof 运算符typeidof 运算符是作用于类型的运算符。C++ 中的typeidof 运算符包括:

* `typeidof`:

相关标签:c++开发语言
其他信息

其他资源

Top