帶有默認值形參的函數(shù)
#include <iostream>
#include <string.h>
using namespace std;
struct Student
{
int iId;
char caName[32];
float fScore;
char caPwd[32];
void info()
{
cout << iId << ' ' << caName
<< ' ' << fScore
<< ' ' << caPwd << endl;
}
};
//函數(shù)重載時,可能會存在歧義
void initStu(Student &stu, int id
, const char *pName)
{
}
//帶有默認值的形參
//一般將帶有默認值的形參寫在形參列表最后
void initStu(Student &stu, int id
, const char *pName
, float score = 60
, const char *pPwd="123456")
{
stu.iId = id;
strcpy(stu.caName, pName);
stu.fScore = score;
strcpy(stu.caPwd, pPwd);
}
int main(void)
{
Student stu;
//initStu(stu, 10001, "張三", 89, "abc@123");
initStu(stu, 10001, "張三", 89);
//initStu(stu, 10001, "張三");
//initStu(stu, 10001, "張三", 89, "abc@123");
stu.info();
return 0;
}
inline 函數(shù)
和宏定義的區(qū)別,宏定義原樣替換,inline(),先進行運算,后替換
將函數(shù)的具體實現(xiàn),替換到出現(xiàn)函數(shù)調(diào)用的地方;沒有函數(shù)調(diào)用的消耗;
實參若存在表達式,會先將表達式進行計算,在進行相應的操作
使用場合,若某段代碼被頻繁調(diào)用,并且該段代碼邏輯結(jié)構(gòu)比較簡單,可用將該段代碼定義為inline()函數(shù)
邏輯結(jié)構(gòu)簡單:沒有循環(huán),沒有嵌套,沒有多重條件結(jié)構(gòu)
#include <iostream>
using namespace std;
#define Test(M, N) M*N //3+4*5+6
void test(int m, int n)
{
cout << m*n << endl;
}
//將函數(shù)的具體實現(xiàn)替換到出現(xiàn)函數(shù)調(diào)用的地方
//沒有函數(shù)調(diào)用的消耗
//實參若存在表達式,會先進行表達式計算
//再進行相應操作
//若某段代碼被頻繁的調(diào)用,并且該段代碼邏輯
//結(jié)構(gòu)比較簡單,那么可以將該段代碼定義為
//inline函數(shù)
//碼邏輯結(jié)構(gòu)比較簡單:沒有循環(huán),沒有嵌套,
//沒有多重條件結(jié)構(gòu)
inline void fun(int m, int n)
{
cout << m*n << endl;
}
int main(void)
{
cout << Test(3+4, 5+6) << endl;
test(3+4, 5+6);
fun(3+4, 5+6);
return 0;
}
函數(shù)模板
template
#include <iostream>
using namespace std;
template <typename U, typename M>
int myMax(U a, M b)
{
int ret = 0;
if (a > b)
{
ret = 1;
}
else
{
ret = 0;
}
return ret;
}
int main(void)
{
#if 0
myMax(12, 45); //-->myMax(int, int); //模板函數(shù)
myMax(12, 4.5); //-->myMax(int, double);
myMax('a', 3.4);//-->myMax(char, double);
#endif
int ret = 0;
ret = myMax('a', 3.4);
cout << ret << endl;
return 0;
}
qsort() 數(shù)組排序
字符串 數(shù)據(jù)類型
string
//字符串操作
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
//string 是類似于結(jié)構(gòu)體的一種類型,含有各式各樣的成員函數(shù)
//定義,復制
string str = "hello,world";
string str1("hello,world");
string str2 = str1;
string str3;
str3 = str;
cout << str <<endl;
cout << str1 <<endl;
cout << str2 <<endl;
cout << str3 <<endl;
//拼接
//字符串拼接時,第一個或者第二個操作數(shù),必須是string對象
string str4 = str+"I like it";
cout << str4 <<endl;
//錯誤拼接
//string str5 = "orange"+"I like it";
//大小,不包含'\0'的長度
cout <<str.length()<<endl;
return 0;
}
new 動態(tài)申請內(nèi)存-----運算符
delete 釋放空間-------運算符
//實例1
#include <iostream>
using namespace std;
struct Student
{
int iId;
char caName[32];
float fScore;
void info()
{
cout << iId << ' ' << caName
<< ' ' << fScore << endl;
}
};
int main(void)
{
//char *p = (char *)malloc(32);
//int *p = new int; //申請存放int類型數(shù)據(jù)的空間:4字節(jié)
//使用87初始化空間
//new和delete是運算符
int *p = new int(87);
cout << *p << endl;
delete p;
char *pc = new char[1];
delete []pc;
int *pArr = new int[7]; //4*7 字節(jié)
delete []pArr;
Student *pStu = new Student;//sizeof(Student)字節(jié)
delete pStu;
return 0;
}
//實例2
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int (*PARR)[4];
typedef int (*PARR2)[5][6];
int main(void)
{
//int arr[3][4];
int (*pa)[4] = (PARR)malloc(sizeof(int)*3*4);
int (*pa2)[4] = new int[3][4];
delete []pa;
delete []pa2;
//fun(int a[3][4]) -->fun(int (*p)[4])
//int arr[4][5][6];
int (*pp)[5][6] = (PARR2)malloc(sizeof(int)*4*5*6);
PARR2 pp2 = new int[4][5][6];
delete []pp;
delete []pp2;
//fun(int a[4][5][6]) -->fun(int (*p)[5][6])
return 0;
}
類的使用
class的使用方法
//類的使用形式
#include <iostream>
using namespace std;
class TDD
{
public:
//函數(shù)的聲明,和傳參
//變量的聲明
void exer();
int exer2();
private:
//函數(shù)的聲明,和傳參
//變量聲明
};
void TDD::exer()
{
}
int TDD::exer2()
{
}
int main(void)
{
TDD tdd;
return 0;
}