10视频—指针和数组
#include <iostream.h>
int main()
{
int array[5]={1,2,3,4,5};
int* ptr=array;
cout << *ptr+2 << endl;//输出3。*ptr = 1
cout << *(ptr+1) << endl;//输出为2
return 0;
}
重载
#include <iostream.h>
void print(int *pBegin, int *pEnd)
{
while(pBegin != pEnd)
{
cout << *pBegin;
pBegin++;
}
}
void print(char *pBegin, char *pEnd)
{
while(pBegin != pEnd)
{
cout << *pBegin;
pBegin++;
}
}
int main()
{
int num[5] = {1,2,3,4,5};
char ch[5] = {'h', '1', '2', '3', '4'};
print(num, num + 5);
cout << endl;
print(ch, ch + 5);
cout << endl;
return 0;
}
模板template
#include <iostream.h>
template <typename T>//模板template
void print(T *pBegin, T *pEnd)
{
while(pBegin != pEnd)
{
cout << *pBegin;
pBegin++;
}
}
int main()
{
int num[5] = {1,2,3,4,5};
char ch[5] = {'h', '1', '2', '3', '4'};
print(num, num + 5);
cout << endl;
print(ch, ch + 5);
cout << endl;
return 0;
}
11视频—结构
#include <iostream>
using namespace std;
#include <string>
#include <fstream>//文件操作
#include <Windows.h>//Sleep()
//定义结构体
struct fishOil
{
string name;
string ID;
char sex;
};
//函数声明
bool writeDate(fishOil *pDate);
bool fishInit();
bool print();
bool record();
int main()
{
//初始化
fishInit();
//实现数据的读取,记录
while (true)
{
cout << "请选择进行的操作:" << endl;
cout << "1.打印数据到屏幕 \n";
cout << "2.录入数据 \n";
cout << "3.退出程序 \n";
char ch;
cin >> ch;
switch (ch)
{
case '1':
print();
break;
case '2':
record();
break;
case '3':
return 0;
default:
break;
}
}
system("pause");
return 0;
}
//录入数据
bool record()
{
fishOil tempOil;
cout << "请输入姓名:";
cin >> tempOil.name;
cout << "请输入ID:";
cin >> tempOil.ID;
cout << "请输入性别:";
cin >> tempOil.sex;
//将数据写入到文件中
if (writeDate(&tempOil))
{
return true;
}
else
{
return false;
}
}
//输出文件中的数据
bool print()
{
string temp;//临时变量
//打开文件
ifstream inFile("test.doc");
if (inFile.is_open())
{
cout << "\n 正在输出数据……";
//模拟进度条
for (int i = 0; i < 100; i++)
{
cout.width(3);//字段宽度设置为3
cout << i << '%';
Sleep(50);
cout << "\b\b\b\b";
}
cout << "\n\n";
cout << " 姓名" << " 身份证" << " 性别" << endl;
while (getline(inFile, temp))
{
cout << temp << endl;
}
cout << "\n\n";
return true;
}
else
{
cerr << "文件读取错误" << endl;
return false;
}
}
bool writeDate(fishOil *pDate)
{
ofstream outFile("test.doc", ios::app);//以追加的形式写入文件
//检查文件打开正确否
if (outFile.is_open())
{
outFile << pDate->name << " ";
outFile << pDate->ID << " ";
outFile << pDate->sex << '\n';
outFile.close();//关闭文件
cout << "数据已经保存到test.doc" << endl;
return true;
}
else
{
cerr << "文件已被占用,打开错误" << endl;
return false;
}
}
bool fishInit()
{
//初始化
fishOil OilInit = { "小甲鱼", "fish_001", 'M'};
//将初始化的内容写入到文件中
if( writeDate(&OilInit) )
{
cerr << "初始化成功^_^" << endl;
return true;
}
else
{
cerr << "初始化失败@_@" << endl;
return false;
}
}
视频12—传值、传址和传引用
值传递
#include <iostream.h>
void change(int age1, int age2);
int main()
{
int age = 24;
change(age, age + 1);//值传递,不是址传递,输出为25
cout << "age = " << age << endl;//输出为24
return 0;
}
void change(int age1, int age2)//值传递,不是址传递
{
age1 = age2;
cout << "age1 = " << age1 << endl;
}
址传递
#include <iostream.h>
void change(int *age1, int age2);
int main()
{
int age = 24;
change(&age, age + 1);//址传递,输出为25
cout << "age = " << age << endl;//输出为25
return 0;
}
void change(int *age1, int age2)//址传递
{
*age1 = age2;
cout << "age1 = " << *age1 << endl;
}
//交换两个变量【址传递】
#include<iostream.h>
void swap(int *n1, int *n2);
int main()
{
int a,b;
a = 1;
b = 2;
cout << "a = " << a
<< ", b = " << b << endl;
//交换两个变量的值
swap(&a, &b);
cout << "交换后的值为:" << "a = " << a
<< ", b = " << b << endl;
return 0;
}
void swap(int *n1, int *n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
传引用
//引用传递
#include<iostream.h>
void swap(int &n1, int &n2);
int main()
{
int a,b;
a = 1;
b = 2;
cout << "a = " << a
<< ", b = " << b << endl;
//交换两个变量的值
swap(a, b);
cout << "交换后的值为:" << "a = " << a
<< ", b = " << b << endl;
return 0;
}
void swap(int &n1, int &n2)
{
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}
视频13—联合、枚举和类型别名
联合
#include <iostream.h>
union T
{
char *name;
int a;
char ch;
};
int main()
{
T birthday;
birthday.name = "WaHaHa";
cout << "birthday.name = " << birthday.name << endl;
birthday.a = 3;//此时birthday.name中的内容被覆盖
cout << "birthday.a = " << birthday.a << endl;
return 0;
}
枚举
//枚举
#include <iostream.h>
int main()
{
//枚举中的值不是字符串,所以不用引号
enum weekends{ Monday, Tuesday, Wednesday, Thursday, Friday};
weekends today;
today = Monday;
cout << "today = " << today << endl;//输出0
today = Wednesday;
cout << "today = " << today << endl;//输出2
return 0;
}
类型别名typedef
//类型别名typedef
#include <iostream.h>
typedef int* p;
int main()
{
p point;
int a = 123;
point = &a;
cout << "a = " << *point << endl;
return 0;
}