目录
2.一般情况都会包含命名空间 using namespace std;
注:boolalpha 用true和false方式打印bool类型(具体见以下代码)
2.C语言中的空指针为NULL,在C++中最好改为 nullptr
一、头文件与命名空间
1.C++创建的源文件为.cpp文件,而C则是.c文件。
2.包含头文件的方式:
(1).两者包含自己创建的头文件没有区别,都为#include"Myhead.h".
(2).包含库目录:
● 运用C语言中的库目录可以采用C语言中包含方式,如#include<stdio.h>,也可以使用C++中包含方式(去掉.h,加个c),如#include<cstdio>.
● C++的标准输入输出流的头文件为iostream,可写为#include<iostream>.
3.命名空间的引入
(1).命名语法:namespace + 空间名
//1.基本创建空间方法
namespace 空间名
{
int a;
void print(){}
}
//2 用空间名限定去访问
int main()
{
空间名::a=1001;
空间名::print();
return 0;
}
(2).命名空间可以增加标识符的使用率,因为在C语言中同一个作用域下不允许定义相同的标识符。
(3).作用域分辨符::
● 空间名限定,类名限定。
● 用来标识全局变量。
(4).using语法,可以省略前缀的写法。
using namespace 空间名; //之后可以省略空间名,但只能在当前域下有效
(5).空间名的嵌套和内容访问
namespace student{//全局变量
char name[20] = "MM";
namespace score{
double ywscore;//定义全局变量
double sxscore;
double yyscore;
void print(){
printf("学生姓名:%s\n", name);
printf("学生语文成绩:%lf\n", ywscore);
printf("学生数学成绩:%lf\n", sxscore);
printf("学生英语成绩:%lf\n", yyscore);
}
}
}
int main(){
student::score::ywscore = 85.5;//空间名嵌套赋值
student::score::sxscore = 65;
student::score::yyscore = 96.5;
student::score::print();//空间名的嵌套使用
while (1);
return 0;
}
二、基本输入输出
1.C++输入输出头文件:#include<iostream>
2.一般情况都会包含命名空间 using namespace std;
3.输出:cout<<输出的内容;
● C++中也支持C语言中的格式控制符
4.输入:cin>>输入的内容;
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;//一般都是写在这里,避免std的前缀使用
int main(){
//1.基本变量打印不需要格式控制
cout << "ILoveyou\n";
char str[] = "ILoveyou\n";
cout << str;
int a = 1;
float b = 1.1;
cout << a;
cout << b;
cout << "\n";
//2.如何一次性打印多个数据
int n;
double dn;
char cn;
string sn;
cin >> n >> dn >> cn >> sn;
cout << n << "\n" << dn << "\n" << cn << "\n" << sn;
//3.C++换行 endl替换\n的作用
cout << "姓名" << endl;
cout << "年龄" << endl;
while(1); //VS2013中防止cmd窗口一闪而过
return 0;
}
注:一些问题(直接CV^^)
//输入的几个问题
//1.字符串输入空格问题
void testStr()
{
using namespace std;
char str[10] = "";
cin >> str; //不带空格的输入
cout << str;
//fflush(stdin) //已被淘汰
//setbuf(stdin,NULL);
while (getchar() != '\n'); //字符和字符串输入前,做了输入
char c;
cin >> c;
cout << c;
//接受空格的输入--->了解
cin.getline(str, 10); //gets_s(str, 10);
cout.write(str, 10);
}
//2.关于命名空间std
void testNamespace()
{
//当不加using namespace std;
std::cout << "没有写using namespace std" << std::endl;
std::string str = "ILoveyou";
}
三、新数据类型
1.bool类型
(1).占用内存是一个字节
(2).计算机非零表示成立,只有0或者指针空表示不成立
(3).一般充当函数返回值,或者开关
(4).一般充当函数返回值,或者开关
(5).正常输出是0和1
(6).C++专有的赋值方式,false和true
注:boolalpha 用true和false方式打印bool类型(具体见以下代码)
void testBool()
{
bool bNum = 1234;
cout << bNum << endl;
bNum = false;
bNum = true;
cout << bNum << endl;
//boolalpha 用true和false方式打印bool类型
cout << boolalpha << bNum << endl;
}
2.C语言中的空指针为NULL,在C++中最好改为 nullptr
注:C++中也可以用NULL;
void testnullptr()
{
int* p = nullptr; //NULL
char* pc = NULL;
}
3.引用类型
(1).理解为起别名。
(2).基本引用
//类型名& 别名=要起别名的东西;
int a=1;
int& b=a; //a有另一个名字叫b ,a就是b,b就是a
(3).常引用(右值引用)
注:右值只能给右值起别名
//类型名&& 别名=右值
int&& a=1; //1就是a ,a就是1
//int& x = 1; //直接报错,C++对const要求更严格
int aa = 1;
const int& x = 1; //第一种写法:const修饰
const int& x2 = aa;
//右值引用,只能给右值起别名
int&& xx = 1; //常量是右值(运算符的右边)
//int&& xx2 = aa; //右值引用只能给右值起别名
4.引用一般用在那些地方
(1).函数参数(防止拷贝产生)
//右值引用
void printRightValue(int&& a)
{
a += 11; //可以增加一个可以修改的功能
cout << a << endl;
}
void printRight(const int& a)
{
//a += 11; //常引用不能修改
cout << a << endl;
}
void modifyA(int a)
{
a = 1001;
}
void modifyB(int& a) //int& a=实参;
{
a = 1001;
}
void testQue{
int aa = 1;
modifyA(aa); //拷贝本 aa=1
cout << aa << endl;
modifyB(aa); //任何传参都是赋值的方式传参
cout << aa << endl; // aa=1001
printRightValue(11);
printRight(11);
}
(2).函数返回值(增加左值使用)
● 不能返回局部变量引用
int test_num=100;//全局变量
int getData()
{
return test_num;
}
int& getValue()
{
return test_num;
}
int main(){
//返回值就是一个值,这个值只能是右值
//getData() = 1001; 错误
getValue() = 0; //返回引用表示返回变量本身
cout << test_num << endl;//输出0
}
5.自动推断auto类型:
(1). 必须根据赋值的数据推断类型,不能直接推断。
(2). 用auto类型必须初始化
struct Node
{
int data;
};
int Max(int a, int b)
{
return a > b ? a : b;
}
void printMax(int(*pMax)(int, int), int a, int b) //有点花里胡哨
{
cout << pMax(1, 2) << endl;
}
void testAuto{
struct Node pStruct = { 1 };
auto pS = &pStruct; //struct Node*
auto pA = pStruct; //struct Node
auto a = 1;
//auto b; //错误,auto不能定义变量
auto pMax = Max;
cout << pMax(1, 2) << endl;
auto ppMax = printMax;
//等效下面代码
void (*p)(int(*)(int, int), int, int) = printMax;
ppMax(pMax, 2, 3);
}
四、函数思想
1.内敛思想 inline关键字
(1).什么样的函数可以成为inline,短小精悍
(2).在结构体中或者类种实现的函数默认内敛(知道即可)
inline int Max(int a, int b)
{
return a > b ? a : b;
}
//函数以二进制形式存储,运行速度快,牺牲空间提高效率
2.函数重载: C++允许同名不同参数函数存在
(1).参数数目不同
(2).参数类型不同
(3).参数顺序不同(一定建立在不同类型的基础上)
void print(int a)
{
cout << a << endl;
}
void print(int a, int b)
{
cout << a + b << endl;
}
//和上面不是顺序不同
//void print(int b, int a)
//{
// cout << a + b << endl;
//}
void print(int a, char b)
{
cout << "int,char" << endl;
cout << a + b << endl;
}
void print(char a, int b)
{
cout << a + b << endl;
}
3.函数缺省: C++中允许给函数形参初始化
(1).缺省顺序 必须从右往左缺省,缺省的参数中间不能存在没有缺省的
(2).没有参入参数,使用的是默认值
//函数缺省
void printData(int a=1, int b=2, int c=3, int d=4)
{
cout << a + b + c + d << endl;
}
int main()
{
//可以理解为 缺省其实是重载的一种综合写法
printData(); //a=1 b=2 c=3 d=4
printData(10); //a=10 b=2 c=3 d=4
printData(10, 20); //a=10 b=20 c=3 d=4
printData(10, 20, 30); //a=10 b=20 c=30 d=4
printData(10, 20, 30, 40);//a=10 b=20 c=3 d=40
return 0;
}
C++与C的区别(二),见以下链接 https://blog.csdn.net/qq_54492880/article/details/121354155