C++的概述
C++语言在c语言的基础上添加了面向对象编程和泛型编程的支持。c++继承了c语言高效,简洁,快速和可移植的传统。c++语言融合了3种不同的编程方式:
c语言代表的过程性语言.
c++在c语言基础上添加的类代表的面向对象语言.
c++模板支持的泛型编程。
C语言和C++语言的联系
C++语言是在C语言的基础上,添加了面向对象、模板等现代程序设计语言的特性而发展起来的。两者无论是从语法规则上,还是从运算符的数量和使用上,都非常相似。
初识C++
代码示例
#include <iostream> // 预编译指令,引入头文件iostream
/**
* c++第一个源程序
*/
// 命名空间
using namespace std;
int main() {
// cout:标准输出流对象。
// endl:刷新缓存区,并且换行
cout << "Hello, World!" << endl;
return 0;
}
C++对C的扩展
::作用域运算符
代码示例
#include <iostream>
/**
* 双冒号作用域运算符。
*/
using namespace std;
// 定义全局变量
int a = 100;
// 声明函数
void test01()
{
// 定义局部变量
int a = 200;
cout << "局部变量:" << a << endl;
// 如果::前面没有任何内容,表示代表全局作用域。
cout << "全局变量:" << ::a << endl;
}
int main() {
// 调用函数
test01();
return 0;
}
注意:
如果::前面没有任何内容,表示代表全局作用域。
C++命名空间
在c++中,名称(name)可以是符号常量、变量、函数、结构、枚举、类和对象等等。工程越大,名称互相冲突性的可能性越大。标准C++引入关键字namespace(命名空间/名字空间/名称空间),可以更好地控制标识符的作用域。
代码示例
#include <iostream>
/**
* 创建命名空间
* @return
*/
namespace A{
// 定义变量
int a = 10;
}
namespace B{
// 定义变量
int a = 20;
}
int main()
{
std::cout << "A::a : " << A::a << std::endl;
std::cout << "B::a : " << B::a << std::endl;
return 0;
}
using声明和使用
代码示例
#include <iostream>
/**
* 使用命名空间
*/
using namespace std;
// 创建命名空间stu
namespace stu
{
void sort()
{
cout << "stu" << std::endl;
}
}
// 使用命名空间stu
using namespace stu;
// 创建命名空间stu1
namespace stu1
{
void sort()
{
std::cout << "stu1" << std::endl;
}
}
// 使用命名空间stu1
using namespace stu1;
int main()
{
stu::sort();
stu1::sort();
cout << "kobe is mvp" <<endl;
return 0;
}
全局变量检测增强
int a;
// int a = 10; a重定义
函数检查增强
// 获取长方形的面积
int getArea(int w, int l){
return w * l;
}
/**
* 函数检测增强 返回值检测增强 形参类型检测增强 调用时候参数数量检测增强
*/
// 声明test01()函数
void test1(){
// getArea(10, 12, 13); 参数传入过多
}
类型转换检测加强
/**
* 类型转换检测加强
* @return
*/
// 声明test02()函数
void test2(){
// 指针左右类型必须一样,才能赋值
int * str = (int *)malloc(sizeof(int)* 8);
}
结构体的简化
/**
* struct增强
* @return
*/
// 在C++语言下,可以在结构体中放函数
struct Student{
int age;
void function(){
age++;
}
};
// 声明test03()函数
void test03(){
// 在C++下面 创建结构体变量的时候,可以简化关键字struct
Student st;
st.age = 17;
st.function();
cout << "学生的年龄是:" << st.age << endl;
}
bool数据类型的扩展
/**
* bool数据类型的扩展,C++语言下有bool数据类型,代表真和假
* @return
*/
bool flag;
void test04(){
// bool类型占用一个字节
cout << "bool类型占用"<< sizeof(bool) << "字节" << endl;
// 真 true(1) 假 false(0)
flag = true;
cout << flag << endl;
}
三目运算符增强
/*
* 三目运算符增强
* C++语言下 三目运算符返回的是变量,可以继续赋值
* @return
*/
void test05(){
// 定义变量
int a = 10;
int b = 20;
// printf("a > b ? a : b = %d\n", a > b ? a : b);
(a > b ? a : b) =100; // b = 100 加上括号 保证运算完整性
cout << "a = " << a << endl; // 10
cout << "b = " << b << endl; // 100
}
const关键字
C语言中的const的特征
- const修饰的变量是只读的, 本质还是变量,不是常量。const修饰的局部变量在栈上分配空间。
- const修饰的全局变量在只读存储区分配空间,const只在编译期有用,在运行期无用。
- const修饰的变量不是真的常量,它只是告诉编译器该变量不能出现在赋值符号的左边。
代码示例
#include <stdio.h>
int main()
{
// 定义普通变量
const int c = 0;
// 定义指针变量
int* p = (int*)&c;
printf("hello world...\n");
*p = 5;
printf("c = %d\n", c);
return 0;
}
程序运行结果
结论
C语言中的const使得变量具有只读属性。
const将具有全局生命周期的变量存储于只读存储区。
const不能定义真正意义上的常量!
C++语言中的const的特征
C++在C的基础上的const进行了进化处理。
当碰见const声明时在符号表中放入常量,编译过程中若发现使用常量则直接以符号表中的值替换。
编译过程中若发现下述情况则给对应的常量分配存储空间
对const常量使用了extern,对const常量使用&操作符。
注意:
C+ +编译器虽然可能为const常量分配空间,但不会使用其存储空间中的值(兼容C语言)。
C++中的const常量
可能分配存储空间
- 当const常量为全局,并且需要在其它文件中使用。
- 当使用&操作符对const常量取地址。
C++中的const常量类似于宏定义。
constintc= 5; ≈ #define C 5
C++中的const常量在与宏定义的区别
const常量是由编译器处理。
编译器对const常量进行类型检查和作用域检查.
宏定义由预处理器处理,单纯的文本替换。
代码示例
#include <stdio.h>
void fax()
{ // 定义宏
#define a 3
}
// 声明函数
void gax()
{
printf("a = %d\n", a);
}
int main()
{
// 定义变量
const int A = 1;
const int B = 2;
// 定义数组
int array[A + B] = {0};
int i = 0;
// 遍历循环
for(i=0; i<(A + B); i++)
{
printf("array[%d] = %d\n", i, array[i]);
}
// 调用函数
fax();
gax();
return 0;
}
程序运行结果
修改const的值
代码示例
#include <cstdlib>
#include <iostream>
using namespace std;
// 定义全局变量
const int const_A = 10;
// 声明函数
void test01(){
/**
* 全局变量受到常量区的保护,无法修改
*/
// const_A = 1000;
/*
int* p = (int *)&const_A;
*p = 100;
cout << "const_A的值:"<< const_A<< endl;
*/
// 定义局部变量
const int const_B = 20;
/*
当对const_B取地址的时候,创建临时内存空间,临时空间是看不到的。
int temp = const_B;
int *p = (int*)&temp
*/
int *p = (int*)&const_B;
*p = 200;
// 输出结果
cout << "const_B的值是"<< const_B << endl;
}
int main() {
printf("hello world!!!\n");
// 调用函数
test01();
return 0;
}
程序运行结果
const分配内存
代码示例
#include <iostream>
/**
* const在C++下分配内存的情况
*/
using namespace std;
// 1.对const修饰的局部变量取地址,会分配临时空间
void test01(){
// 01.定义变量
const int const_A = 10;
int *p = (int *)&const_A;
}
// 2.使用变量,初始化const修饰的局部变量
void test02(){
// 定义变量
int a = 10;
// 会分配内存,可以通过间接修改,修改成功.
const int const_B = a;
int * p = (int *)&const_B;
*p = 30;
// 输出结果
cout << "const_B的值:" << const_B << endl;
}
// 3.对于自定义数据类型,也会分配内存
struct Person{
// 定义变量
string name;
int age;
};
// 声明函数
void test03(){
const Person p1{};
Person * p = (Person *)&p1;
p->age = 18;
p->name = "常山赵子龙";
// 输出结果
cout << "姓名:" << p->name <<endl << "年龄:" << p->age << endl;
}
int main() {
// 1.调用函数
// test02();
test03();
return 0;
}