c++是一门广泛使用的计算机编程语言,刚开始学习 C++时, 我们需要认真学习 C++的基本语法规则,包括变量声明、数据类型、控制结构(如条件语句、循环语句)等。确保对每一个语法点都理解透彻,避免在后续编程中出现低级错误。注意 C++与其他编程语言在语法上的差异,比如 C++中的指针操作、引用等概念是一些其他语言中没有的,需要特别留意。
1.c++简单程序
C++兼容C语言绝大多数的语法,C++中需要把定义文件代码后缀改为.cpp,vs编译器看到是.cpp就会调⽤C++编译器编译,linux下要⽤g++编译,不再是gcc。
#include<stdio.h>
int main()
{
printf("hello henu\n");
return 0;
}
当然C++有⼀套自己的输⼊输出,严格说C++版本的应该是这样写的。
#include <iostream>
using namespace std;int main()
{cout << "hellow henu" << endl;return 0;}
2.命名空间
2.1namespace命名空间的价值
命名空间(namespace)可以避免命名冲突。在大型项目中,可能会有多个开发人员同时工作,或者使用来自不同库的代码。如果没有命名空间,不同模块中相同名称的标识符很容易发生冲突。例如:两个不同的函数库可能都定义了一个名为“func”的函数。使用命名空间后,可以分别通过不同的命名空间来访问这些函数,避免了冲突。
#include <stdio.h>
#include <stdlib.h>
int rand = 10;
int main()
{// 编译报错:error C2365: “rand”: 重定义;以前的定义是“函数”printf("%d\n", rand);return 0;
}
2.2namespace命名空间的定义
(1)定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接⼀对{}即可,{}中即为命名空间的成员。命名空间中可以定义变量/函数/类型等。
(2)namespace本质是定义出⼀个域,这个域跟全局域各自独立,不同的域可以定义同名变量
(3)C++中域有函数局部域,全局域,命名空间域,类域;域影响的是编译时语法查找⼀个变量/函数/ 类型出处(声明或定义)的逻辑,所有有了域隔离,名字冲突就解决了。局部域和全局域除了会影响编译查找逻辑,还会影响变量的生命周期,命名空间域和类域不影响变量生命周期。
(4)namespace只能定义在全局,当然它还可以嵌套定义。
(5)项目工程中多文件中定义的同名namespace会认为是⼀个namespace,不会冲突。
(6)C++标准库都放在⼀个叫std(standard)的命名空间中。
普通的命名空间定义:
#include <iostream>
using namespace std;
namespace henu
{//命名空间中可以定义变量、函数、类型int rand = 10;int add(int x, int y){return x + y;}struct Node{struct Node* next;int val;};
}
int main()
{//这里访问的是全局变量中的rand函数指针printf("%p\n", rand);//这里指定henu命名空间中的rand//域作用限定符“::“printf("%d\n", henu::rand);return 0;
}
嵌套定义:
#include <iostream>
using namespace std;
namespace henu
{namespace w1{int rand = 10;int add(int x, int y){return x + y;}}namespace w2{int rand = 20;int minus(int x, int y){return x - y;}}
}
int main()
{printf("%d\n", henu::w1::rand);//10printf("%d\n", henu::w2::rand);//20printf("%d\n", henu::w1::add(5, 3));//8printf("%d\n", henu::w2::minus(5, 3));//2return 0;
}
多文件中可以定义同名namespace,它们会默认合并到一起,
2.3namespace命名空间的使用
错误使用:
#include <iostream>
using namespace std;
namespace henu
{int x = 10;int y = 20;
}
int main()
{//报错,未定义标识符xprintf("%d", x);return 0;
}
指定命名空间访问:
#include <iostream>
using namespace std;
int main()
{printf("%d", henu::x);return 0;
}//using 将整个命名空间展开
using namespace henu;
int main()
{printf("%d ", x);printf("%d ", y);return 0;
}
using 将命名空间中的某个成员展开:
#include <iostream>
using namespace std;
namespace henu
{int x = 10;int y = 20;
}
using henu::x;
int main()
{printf("%d ", x);return 0;
}
3.c++输入和输出
(1)<iostream> 是 Input Output Stream 的缩写,是标准的输入、输出流库,定义了标准的输入、输
#include <iostream>
using namespace std;
int main()
{cout << "hellow henu\n";cout << "hellow henu" << endl;int a = 10;double b = 0.1;char c = 'x';cout << a << " " << b << " " << c << endl;//10 0.1 xstd::cout << a << " " << b << " " << c << std::endl;//10 0.1 xscanf("%d %lf", &a, &b);printf("%d %lf\n", a, b);//可以自动识别变量类型cin >> a;//输入10cin >> b >> c;//输入0.1,'x'cout << a << endl;cout << b << " " << c << endl;//10//0.1 xreturn 0;
}
4.缺省参数
void fun(int a = 0)
{cout << a << endl;
}int main()
{//没有传参,使用参数默认值fun();//传参,使用指定参数fun(10);return 0;
}
代码2:
//全缺省
void fun1(int a = 10, int b = 20, int c = 30)
{cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;
}
//C++规定半缺省参数必须从右往左依次连续缺省,不能间隔跳跃给缺省值
//半缺省
void fun2(int a, int b = 10, int c = 20)
{cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;
}
//错误的
//void fun2(int a = 10, int b = 10, int c)
//{
// cout << "a = " << a << endl;
// cout << "b = " << b << endl;
// cout << "c = " << c << endl;
//}int main()
{fun1();//10 20 30 fun1(1);//1 20 30fun1(1, 2);//1 2 30fun1(1, 2, 3);//1 2 3//错误的 fun(1, , 3);//错误的 fun2();fun2(100);fun2(100,200);fun2(100,200,300);return 0;
}
代码3:函数声明和定义分离时,缺省参数不能在函数声明和定义中同时出现,规定必须函数声明给缺省值。
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;
void STInit(ST* ps, int n = 4);// 缺省参数不能声明和定义同时给
void STInit(ST* ps, int n)
{assert(ps && n > 0);ps->a = (STDataType*)malloc(n * sizeof(STDataType));ps->top = 0;ps->capacity = n;
}
int main()
{ST s1;STInit(&s1);// 确定知道要插⼊1000个数据,初始化时⼀把开好,避免扩容ST s2;STInit(&s2, 1000);return 0;
}
5.函数重载
//参数类型不同
int Add(int a, int b)
{cout << a << " " << b << endl;return a + b;
}
double Add(double a, double b)
{cout << a << " " << b << endl;return a + b;
}//参数个数不同
void fun()
{cout << "fun()" << endl;
}
void fun(int a)
{cout << "fun(int a)" << endl;
}
void fun(int a, int b)
{cout << "fun(int a, int b)" << endl;
}//参数类型顺序不同
void fun(int a, char b)
{cout << "fun(int a, char b)" << endl;
}
void fun(char a, int b)
{cout << "fun(char a, int b)" << endl;
}
void Swap(int* a, int* b)
{//...
}
void Swap(double* a, double* b)
{//...
}int main()
{Add(10, 20);Add(1.1, 2.2);fun();fun(10);fun(10, 20);fun(10, 'a');fun('a', 10);int a = 1, b = 2;double c = 1.1, d = 2.2;Swap(&a, &b);Swap(&c, &d);return 0;
}
两个有误示例:
//返回值不同不能作为重载条件,因为调⽤时也⽆法区分
void fxx()
{
}int fxx()
{return 0;
}下⾯两个函数构成重载f()但是调⽤时,会报错,存在歧义,编译器不知道调⽤谁
void f1()
{cout << "f()" << endl;
}
void f1(int a = 10)
{cout << "f(int a)" << endl;
}
int main()
{f1();return 0;
}