C语言到C++ 基础2

指针的引用

#include <iostream>

using namespace std;

struct Teacher
{
	char name[64];
	int age;
};

//二级指针的用法
int getTeachar(Teacher **p)
{
	Teacher *tmp = NULL;
	if (p == NULL)		
		return -1;
	tmp = (Teacher *)malloc(sizeof(Teacher));
	if (tmp == NULL)	
		return -2;

	tmp->age = 33;
	*p = tmp;
	return 0;
}
//关于指针的引用
int getTeachar2(Teacher * &p)
{

	p = (Teacher *)malloc(sizeof(Teacher));
	if (p == NULL)
		return -1;
	p->age = 22;
	return 0;
}

int main01()
{
	Teacher *pT1 = NULL;
	getTeachar(&pT1);	//二级指针的用法
	cout << "二级指针修改age:" << pT1->age << endl;
	free(pT1);	//释放

	getTeachar2(pT1);	//指针的引用
	cout << "指针的引用修改age:" << pT1->age << endl;
	free(pT1);	//释放

	cout << "hello..." << endl;
	system("pause");

	return 0;
}

常量的引用

#include <iostream>

using namespace std;

int main02()
{
	//普通引用 
	int a = 10;
	int &b = a;
	b = 20;
	cout << "a:" << a << "   " << "b:" << b << endl;

	//常引用	
	int x = 31;
	const int &y = 31;	//常引用 变量只有只读属性
	cout << "x= " << x << "   " << "y = " << y << endl;
	//用法1  用变量初始化常引用
	//用法2  用常量初始化常引用 引用是没有内存的
	//int &x1 = 35; 错误
	const int &z = 63;

	
	system("pause");
	return 0;
}

内联函数

#include <iostream>

using namespace std;

//声明定义必须要在一起 编译器不一定允许内联请求
//宏代码片段 由预处理器处理
inline int add(int a,int b)
{
	return a + b;
}

int main03()
{
	cout << "使用内联函数" << add(12, 15) << endl;
	system("pause");
	return 0;
}

函数默认参数

#include <iostream>

using namespace std;

void myprint(int x = 3) //默认参数
{
	cout << "x:" << x << endl;
}

//默认参数必须在形参列表的最右边
void myprint1(int  n,int m,int x,int y = 3) //默认参数
{
	cout << "y:" << y << endl;
}


int main04()
{
	myprint(); //默认参数

	system("pause");
	return 0;
}

函数占位参数

#include <iostream>

using namespace std;

//函数占位参数 函数调用时,必须写够参数
void printfA(int x, int y, int)
{
	cout << "x:" << x << "   y:" << y << endl;

}

//可以将占位参数和默认参数结合起来用
//为以后程序扩展留下线索,兼容C语言中不规范的写法
void printfB(int x, int y, int = 0)
{
	cout << "x:" << x << "   y:" << y << endl;

}

int main05()
{
	printfA(4,5,6);	//调用时必须写够参数 
	printfB(8,9);
	printfB(11,22,33);
	system("pause");
	return 0;
}

函数重载

#include <iostream>
#include <string>

using namespace std;

//当函数名和不同的参数搭配时函数的含义不同
//判断标志   1名称  2参数  3返回值
//名称相同 参数不同  返回值不作为重载的依据
void myPrint(int a)
{
	cout << "a:" << a << endl;
}

void myPrint(char a)
{
	cout << "a: " << a << endl;
}

void myPrint(string a)
{
	cout << "a:" << a << endl;
}


void myPrint(int a,int b)
{
	cout << "a:" << a << "  b:" << b << endl;
}


int main06()
{
	myPrint(100);
	myPrint("52,85");
	myPrint('b');
	myPrint("梵高先生");


	system("pause");
	return 0;
}

函数重载和函数指针

#include <iostream>
#include <string>

using namespace std;

void myPrintA(int a)
{
	cout << "a:" << a << endl;
}

void myPrintA(char a)
{
	cout << "a: " << a << endl;
}

void myPrintA(string a)
{
	cout << "a:" << a << endl;
}


void myPrintA(int a, int b)
{
	cout << "a:" << a << "  b:" << b << endl;
}

//函数指针
//声明一个函数类型
typedef void (myPrintATypeA)(int a);  //定义一个函数指针类型
//myPrintATypeA *p = NULL;	//定义一个函数指针 指向函数的入口地址

//声明一个函数指针类型
typedef void (*myPrintATypeB)(int a);  //定义一个函数指针类型
//myPrintATypeB p = NULL;	//定义一个函数指针类型 定义了一个指针	

//定义一个函数指针变量
void  (*myPrintATypeC)(int a);

int main()
{
	myPrintATypeA *p = NULL;
	p = myPrintA;
	p(10);
	//p(10, 12);   //err  编译器会进行严格的函数检测
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36795563/article/details/81395444