C++学习记录01--C的拓展1
开始系统性代码学习,提升代码能力
1.特点
C:面向过程
C++:添加类的面向对象(C的拓展)
2.第一个C++程序
#include <iostream> //预编译指令,引入头文件iostream
using namespace std; //使用std命名空间
int main() {
int a;
a = 10;
//std::cout << "Hello, World!" << std::endl;
cout << "Hello world" << endl;
return 0;
}
注意:
1.C++头文件没有扩展名,若使用C语言头文件,加上前缀c,如:math.h->cmath
2.命名空间用关键字namespace定义,将单个标识符下的大量有逻辑联系的程序实体组合到一起,该标识符作为群组的名字。
3.cout是C++中的标准输出流,endl是输出换行兵刷新缓冲区。
3.C++对C的拓展
1.命令空间
1.C中通过static关键字使得名字只在本编译单元内可见,C++中通过命名空间控制对名字的访问。
2.C++中,名称(name)可以是符号常量、变量、结构、枚举、类和对象等。
3.std是C++标准命名空间,标准程序库中所有标识符都被定义其中,使用时需要加上using声明(using namespace std)或std指示(std::cout)
1.命名空间的使用
1.定义
namespace 名称
{
//定义变量、函数......
}
2.访问
空间名::成员(::作用域操作符)
调用函数时同理(如:A::func();)
#include <iostream>
using namespace std
namespace A{
int a = 10;
}
namespace B{
int a = 20;
}
namespace C
{
int x;
void func()
{cout <<"nameB" <<endl;}
}
int main(){
C::x =100;
cout << A::a << endl;
cout << B::a << endl;
cout << C::x << endl;//x = 100
//访问空间中的成员函数
C::func();
return 0;
}
注意:命名空间只能在全局范围内定义
3.命名空间可嵌套定义
#include <iostream>
using namespace std
namespace nameC
{
int a = 100;
namespace nameB
{
int a = 200;
}
}
int main(){
cout << nameC::a << endl;//a=100
cout << nameC::nameB::a << endl;//a=200
return 0;
}
2. 命名空间的声明和实现分开
1.在头文件test.h中声明命名空间
#ifndef PROJECT_9_10_TEST_H
#define PROJECT_9_10_TEST_H
//声明一个命名空间
namespace MySpace
{
//声明函数
void func1();
void func2(int);
}
#endif //PROJECT_9_10_TEST_H
// ifndef/endif,一般出现在头文件中,防止头文件被重复包含
2.在test.cpp中实现命名空间
#include <iostream>
#include "test.h" //包含头文件
using namespace std;
//实现MySpace空间中的函数
void MySpace::func1()
{
cout <<"void MySpace func1()" <<endl;
}
void MySpace::func2(int x)
{
cout <<"void MySpace func2():" << x << endl;
}
3.在main.cpp中调用
#include <iostream>
#include "test.h" //包含头文件
using namespace std;
int main() {
MySpace::func1();
MySpace::func2(100);
return 0;
}
输出结果:void MySpace func1()
void MySpace func2():100
3.命名空间的别名
为长命名函数进行简短命名,提高代码效率
namespace studentInfoHandle //声明命名空间
{
void func()
{
cout <<"studentInfoHandle" <<endl;
}
}
//给长命名取一个别名
int main() {
namespace stuInfoH = studentInfoHandle;
stuInfoH::func();
return 0;
}
3.引用
1.概念
1.引用可以看作一个已定义变量的别名
2.语法:Type&name = var
例如:int a =10; int& p = a;
注意:&在此处起标识作用,不是求地址运算
2.使用
#include <iostream>
using namespace std;
void test01()
{
int a = 10;
//为变量a取别名
int& b = a;
cout << "a" << a <<endl;
cout << "b" << b <<endl;
cout << "------------" <<endl;
//操作b相当于操作a本身
b = 100;
cout << "a" << a <<endl;
cout << "b" << b <<endl;
cout << "------------" <<endl;
//一个人变量可以有n个别名
int& c = a;
c = 200;
cout << "a:" << a <<endl;
cout << "b:" << b <<endl;
cout << "c:" << c <<endl;
cout << "------------" <<endl;
//a,b,c地址相同
cout << "a:" << &a <<endl;
cout << "b:" << &b <<endl;
cout << "c:" << &c <<endl;
}
int main() {
test01();
return 0;
}
输出结果:
a10
b10
a100
b100
a200
b200
c200
a0x67fbdff7ec
b0x67fbdff7ec
c0x67fbdff7ec
3.注意事项
1.引用变量时必须初始化。
2.引用变量是唯一的,不可同时引用多个。
4.本质
1.引用的本质是实现一个常指针。
2.引用占用的空间大小和指针相同。
5.引用作为函数的参数
普通引用在声明时必须用其他的变量初始化,作为函数参数声明时不进行初始化。
#include <iostream>
using namespace std;
typedef struct Teacher //定义结构体
{
char name[32];
int age;
}Teacher;
void printfT2(Teacher &t) //定义函数
{
cout << t.age<< endl;
}
}
int main() {
Teacher teacher;
teacher.age = 30;
printfT2(teacher); //Teacher &t = teacher
return 0;
}
6.意义
1.引用在一些场合可以替代指针
2.具有更好的可读性和实用性