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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章