栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

接触C++第一课~c和c++区别笔记

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

接触C++第一课~c和c++区别笔记

目录

头文件:

入口函数:

命名空间:

c++基本输入和输出

 c++新类型

函数思想

 答疑

 结构体区别

动态内存申请

内存池

c++ string类型

 答疑

C语言~malloc,calloc,realloc申请

C语言~二维数组动态申请

c++动态申请一维数组和二维数组


头文件:

1.c++当中头文件可以直接采用类名的方式直接包含,不需要.h

2.如果想要包含C语言标准库中的头文件,可以采用.h方式包含,也可以采用去掉.h直接在头文件前面加个c (例如:)

3.包含自定义头文件,不可以采用缺少.h的方式写法(例如:"myhead.h")

入口函数:

1.c++中只支持int返回值类型主函数

2.c++中主函数的参数没什么要求,可以写也可以不写。

命名空间:
#include //标准c++头文件
using namespace std;//标准命名空间

namespace mm1
{
	char name[10];
	int age;
	void print()
	{
		printf("hello world mm1n");
	}
	struct MM
	{
		int age;
	};
}
namespace boy
{
	namespace mm
	{
		int age;
		void print()
		{
			printf("boy::mmn");
		}
	}
}

int main()//入口函数
{
	strcpy(mm1::name, "小可爱");
	struct mm1::MM mm = { 1 };
	printf("%dn", mm.age);
	mm1::print();

	{
		using namespace mm1;
			printf("%sn", name);
	}
	//嵌套
	boy::mm::print();
	//省略方式
	using namespace boy::mm;
	print();

	return 0;
}


c++基本输入和输出
#include 
#include 

int num = 999;
int main()
{
	{
		using namespace std;
		//No.1  c++基本输出
	//1.1单个数据的时候
		printf("我是最帅的!n");
		cout << "我是最帅的那也不错n";

		printf("%dn", 1);
		cout << 1;
		cout << "n";
		//1.2输出多个数据
		printf("%d%s%lfn", 1, "你好帅", 1.5);
		cout << 1 << "你好帅" << 1.5;//不会自动补0是多少就打印多少
		cout << "n";
		//1.3关于格式控制
		//c++依旧支持C语言格式控制字符,不需要要考虑c++的格式控制
		//目前只需要掌握基本输入和输出的用法即可,IO流课程再讲解
		cout << "520" << endl;//endl:换行(是字符'L'小写不是数字1),其他t也是这样操作
		//1.4输出多个变量
		int iNum = 1;
		char str[20] = "我好帅,小丁丁";
		double dNum = 3.14;
		cout << iNum << "t" << str << "t" << dNum << endl;


		//No.2 c++的基本输入
		//1.输入单个字符
		cin >> iNum;
		cout << "iNum=" << iNum << endl;
		//2.输入多个数据
		cin >> iNum >> str >> dNum;
		cout << "iNum=" << iNum << "str=" << str << "dNum=" << dNum << endl;
	}

	//No3.注意几个现象
	//1.命名空间,如果把命名空间放作用域里下面使用就会报错,消除错误方法:加前缀 std::
	int temp = 0;
	std::cout << 1 << std::endl;
	//2.别致写法
	std::cout << "uloveyou" << std::endl;
	int num=111;
	std::cout << num << std::endl;//就近原则
	std::cout << ::num << std::endl;//::标识全局的变量
    //强制类型转换
    double a=66.666;
    std::cout< 

运行结果:

 c++新类型
#include 
#include 
using namespace std;


void arr(const char* a)
{
	cout << a << endl;
}
//指针传参
void Cop(int& a)//不要理解为指针,int& a=实参,修改a就是修改实参
{
	a = 999;
}

int pai = 666;
void Cop2(int*& p)
{
	p = &pai;
}
//右值引用
void printRightValue(int&& a)
{
	a+=2000;//增加一个可以修改的功能,常引用是不能修改的
	cout << a << endl;//输出3001
}

int num = 999;
int main()
{
	bool bNum =true;
	cout << bNum << endl;
	//boolalpha 用true和false方式打印bool类型
	cout << boolalpha << bNum << endl;
	int* p = nullptr;//表示空
	//类型& 别名=要起别名的东西
	int 女朋友 = 9;
	int& 小可爱 = 女朋友;//小可爱就是女朋友的别名
	小可爱 = 777;
	cout << 女朋友 << endl;//输出:777
	//c++中常量要求更严格
	//想要传变量和常量必须用const修饰
	arr("love");//形参必须要有const修饰
	//常引用
	//int& x = 1;//直接报错,c++对const更加严格
	int a=1;
	const int& xx = 1;//第一种写法:const修饰,可以给左值起引用也可以给右值引用
	const int& x2 = a;
	//右值引用
	int&& x = 1;//常量是右值(运算符的右边),给常量起别名
	//int&& x = a;//错误,右值引用只能给右值起别名
	//引用一般用在什么地方?
	//1.函数参数(防止拷贝产生);2.函数返回值(增加左值使用)3.不能返回局部变量引用
	int x3 = 8;
	Cop(x3);
	cout << x3 << endl;
	int pp = 1001;
	int* pp2 = &pp;
	Cop2(pp2);
	cout << *pp2 << endl;//输出:666
	printRightValue(1001);

	return 0;
}
		

函数思想
#include 
#include 
using namespace std;



inline int Max(int a, int b)
{
	return a > b ? a : b;
}
//可以同名,数目不同
void print(int a)
{
	cout << a << endl;
}
void print(int a, int b)
{
	cout << "迫不得已"< 

 答疑

auto与C语言auto区别?

答:c++中淘汰了C语言用法,只有自动推断用法

auto可以在声明变量时根据变量初始值的类型自动为此变量选择匹配的类型。

举例:对于值x=1;既可以声明: int x=1 或 long x=1,也可以直接声明 auto x=1

auto f = 3.14159;  //double
auto s("你好啊");  //const char*
auto z = pp auto(9);  //int *
auto x1 = 5, x2 =9.99, x3 = 'a';   //错误,必须是初始化为同一类型

 结构体区别
#include 
#include 
#include 
using namespace std;


struct MM
{
	//属性,特征
	//数据成员
	char name[20];
	int age;
	//.....
	//行为(方法)
	//成员函数
	void print()
	{
		cout << name << "t" << age << endl;
	}
	void printData();//在里面声明在外面实现
};
//结构体限定,告诉别人这个函数来自哪里
void MM::printData()
{
	cout << name << "t" << age << endl;
}
//结构体中的变量必须通过结构体变量(结构体指针)去访问
//c++结构体中的函数访问属性,可以直接访问
int main()
{
	struct MM girl = {"小芳",20};
	MM mm = {"小狗",43};
	girl.print();
	(&girl)->print();
	MM* p = &girl;
	(*p).print();
    girl.age=666;
    girl.print();
	return 0;
}
		

动态内存申请
#include 
#include 
#include 
using namespace std;


struct MM
{
	char* name;//需要加const
	int age;
	//成员函数
	void print()
	{
		cout << name<<"t" << age << endl;
	}
};
void test1()
{
	//申请不初始化
	int* p = new int;
	*p = 999;
	cout << *p << endl;
	delete p;//释放完后还可以用,就像穿衣服一样脱了还能穿
	p = nullptr;
	 p = new int;
	*p = 666;
	cout << *p << endl;
	delete p;
	p = nullptr;
	//申请内存加初始化
	int* p2 = new int(520);//()给单个数据初始化
	cout << *p2 << endl;
	delete p2;
	p2 = nullptr;

}

void test2()
{
	//一维数组,不带初始化
	int* arr = new int[3];//等效于产生 int arr[3]
	//const char* s = new char[20];//你--->大老婆
	//const char* s2 = s;//朋友--->大老婆
	//s = "斤斤计较";//你--->二老婆
	//cout << s << endl;//输出"斤斤计较"
	//cout << s2 << endl;//输出乱码

	//通常字符串赋值是用strcpy
	char* arr2 = new char[20];
	strcpy(arr2, "你好呀");
	cout name = new char[20];
	// 不能修改
	//strcpy(pmm->name, "来来来");//error E0167	"const char *" 类型的实参与 "char *" 类型的形参不兼容
	MM* pmm = new MM;
	//结构体中指针要做二次申请,才能strcpy,或者赋值
	pmm->name = new char[20];
	strcpy(pmm->name,"你好");//不能直接赋值还要二次申请
	pmm->age = 6;
	pmm->print();
	//申请顺序和释放顺序是相反的,就是个栈,反过来会中断
	delete[] pmm->name;
	delete pmm;
}

int main()
{
	test1();
	test2();
	test3();
	return 0;
}

内存池

#include 
#include 
#include 
using namespace std;


void test()
{
	char* memorySum = new char[1024];
	//....事情的处理,需要内存,程序所有的内存源自于memorySum
	//memorySum是申请内存的首地址
	int* pNum = new(memorySum)int[3]{ 1,2,3 };//格式:new(申请内存的开始位置) 申请类型和申请多少
	//char* pstr = new(memorySum+12)char[20]{ "小哥哥" };//12是因为int类型申请了三个所以是3*4=12
	//等效于下面这段,指针的偏移
	char* pstr = new(pNum + 3)char[20]{ "小哥哥" };
	for (int i = 0; i < 3; i++)
	{
		cout << ((int*)memorySum)[i]<< endl;//注意优先级用括号
	}
	cout < 

c++ string类型
#include 
#include 
#include 
#include 
using namespace std;

void test1()
{
	//一般string前不用加const,加了就不能修改
	//创建方式①
	string str1;
	str1 = "hello world";
	cout<<"str1:" << str1 << endl;
	//创建方式②
	string str2("hello world");
	cout<<"str2:" << str2 << endl;
	//创建方式③
	string str3 = "hello world";//我喜欢这种
	cout<<"str3:" << str3 << endl;
	//还可以赋值方式
	string str4(str3);
	cout<<"str4:" << str4 << endl;
	string str5 = str4;
	cout<<"str5:" << str5 << endl;
}
void test2()
{
	string str1 = "one";
	string str2 = "two";
	//字符串连接
	string str3 = str1 + str2;//加号连接两个字符串,没有减法
	cout << "str3:" << str3 << endl;
	//字符串比较
	if (str1 > str2)
	{
		cout << "str1大" << endl;
	}
	else
	{
		cout << "str1小" << endl;
	}
}
void test3()
{
	//c++中是一个自定义类型(类),目前当做结构体即可
	//c++ string不能用到C语言的字符串处理函数
	//c++如何转换为C语言的char*
	//c_str()  data()函数
	string str6 = "Like";
	//printf("%s", str6);//这样会警告输出不了
	printf("str6:%sn", str6.c_str());
	printf("str6:%sn", str6.data());
	//直接把数字转换为相应的字符串
	string str7 = to_string(123456);
	cout <<"str7:" << str7 << endl;
}
void test4()
{
	//采用下标法打印string
	string str = "i kill you";
	//c++ string中计算长度没有记录
	for (int i = 0; i <10; i++)
	{
		cout << str[i];
	}
	cout << endl;
	//其他函数操作
	//万金油函数
	//empty();
	//size();
	string str3 = "abcd";
	//cout << sizeof(str3) << endl;//输出:28
	cout << str3.size() << endl;//输出:4
	string str4;
	if (str4.empty())//return length==0; 
	{
		cout << "str4字符串为空" << endl;
	}
}
int main()
{
	test1();
	test2();
	test3();
	test4();
	return 0;
}

 答疑

为什么char*类型创建要加const?

答:这个跟赋值的值有关系,因为c++对const要求更加严格,两边类型必须一致

//指针变量=常量地址,类型不一致所以要加const
	//char* str = "i like you";//	E0144 "const char *" 类型的值不能用于初始化 "char *" 类型的实体
	 char str2[] = "i lke you";
	 char* str = str2;
	 cout << str << endl;

C语言~malloc,calloc,realloc申请
//malloc,申请内存不初始化
	int* pM = (int*)malloc(sizeof(int) * 3);
	//memset(pM, 0, sizeof(int) * 3);

	//calloc,申请内存顺便初始化为0
	int* pM2 = (int*)calloc(3,sizeof(int));
	//相当于malloc的基础上加了memset(pM,3,sizeof(int)*3);
	//realloc,原来的数据还在
	int* pp = (int*)malloc(sizeof(int));
	*pp = 100;
	pp = (int*)realloc(pp, sizeof(int) * 3);
	pp[1] = 200;
	pp[2] = 334;
	cout << pp[0] < 
//p是指针可以省略*
	auto *p = new int[3]{ 1,2,3 };
	cout << p[0] << endl;

C语言~二维数组动态申请
 
#include 
#include 
#include 
#include 
using namespace std;

int** create(int row, int cols)
{
	int** p = (int**)malloc(sizeof(int*) * row);
	if (p == NULL)
	{
		return NULL;
	}
	for (int i = 0; i  

c++动态申请一维数组和二维数组
#include 
#include 
#include 
#include 
using namespace std;


int main()
{
	//c++申请一维数组
	int* p = new int[10];
	for (int i = 0; i < 10; i++)
	{
		p[i] = i;
		cout << p[i] << endl;
	}

	cout << endl;
	//c++申请二维数组(四行三列)
	int** pp = new int* [4];
	for (int i = 0; i < 4; i++)
	{
		pp[i] = new int[3];
	}
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			pp[i][j] = i * j;
			cout << pp[i][j]<<"t";
		}
		cout << endl;
	}
	return 0;
}

目前就先写这么多,以后遇到不同版本新的知识点再补上。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/722845.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号