二十三. 数组指针和数组类型
音频和视频不同步,,,有空在看。
二十四. 中午回顾(略)
二十五. 函数指针的语法和意义
#include <iostream>
using namespace std;
int func(int a,int b)
{
cout<<"1999年写的 func(" <<a<<","<<b<<")"<<endl;
return 0;
}
int func2(int a,int b)
{
cout<<"1999年写的 func2(" <<a<<","<<b<<")"<<endl;
return 0;
}
int func3(int a,int b)
{
cout<<"1999年写的 func3(" <<a<<","<<b<<")"<<endl;
return 0;
}
//2018年想添加一个新的子业务
int new_func4(int a,int b)
{
cout<<"2018年写的 new_func4(" <<a<<","<<b<<")"<<endl;
return 0;
}
//方法一: 函数的返回值,函数的参数列表(形参的个数,类型,顺序)
//定义一个函数类型
typedef int(FUNC)(int,int);
//方法二: 定义一个函数指针
typedef int(*FUNC_P)(int,int);
//定义一个统一的接口,将他们全部调用起来
void my_function(int(*fp)(int,int), int a,int b)
{
cout<<"1999年写的 架构业务"<<endl;
cout<<"固定业务1"<<endl;
cout<<"固定业务2"<<endl;
fp(a,b); //可变业务
cout<<"固定业务3"<<endl;
}
int main()
{
#if 0
//方法一
FUNC* fp = NULL;
fp = func;
(*fp)(10,20);
//方法二
FUNC_P fp2 = NULL;
fp2 = func;
fp2(200,300);
//方法三
int (*fp3)(int,int) = NULL;
fp3 = func;
fp3(1000,2000);
#endif
my_function(func,10,20);
cout<<"---------------------"<<endl;
my_function(func2,100,200);
cout<<"---------------------"<<endl;
my_function(func3,1000,2000);
cout<<"---------------------"<<endl;
my_function(new_func4,123,456);
return 0;
}
二十六. C语言实现多态现象案例——诸葛亮的锦囊妙计1(略)
二十七. C语言实现多态现象案例——诸葛亮的锦囊妙计2
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
//-----------抽象层---------------
//定义拆开锦囊方法的类型,
typedef void(TIPS)(void);
//定义锦囊
struct tip
{
char from[64];//谁写的
char to[64]; //写给谁的
//锦囊的内容
TIPS* tp; //相当于抽象类的 纯虚函数
};
//需要一个打开锦囊的架构函数
void open_tips(struct tip* tip_p)
{
cout<<"打开了锦囊"<<endl;
cout<<"此锦囊是由"<<tip_p->from << "写给"<< tip_p->to <<"的"<<endl;
cout<<"内容是"<<endl;
tip_p->tp(); //此时就发生了多态现象
}
//提供一个创建锦囊的方法
struct tip* create_tip(char *from, char *to, TIPS*tp)
{
struct tip *temp = (struct tip *)malloc(sizeof(struct tip));
if(temp == NULL){
return NULL;
}
strcpy(temp->from,from);
strcpy(temp->to,to);
//给一个回调函数赋值,一般称为注册回调函数
temp->tp = tp;
return temp;
};
//提供一个销毁锦囊的方法
void destroy_tip(struct tip* tip)
{
if(tip != NULL)
{
free(tip);
tip = NULL;
}
}
//-----------实现层---------------
//诸葛亮写了3个锦囊
void tip1_func(void)
{
cout<<"一到东吴九拜会乔国老"<<endl;
}
void tip2_func(void)
{
cout<<"如果主公乐不思蜀,就谎称曹贼来袭"<<endl;
}
void tip3_func(void)
{
cout<<"如果被孙权追杀,就向孙尚香求救"<<endl;
}
void tip4_func(void)
{
cout<<"换人,别让关羽守荆州,联吴抗曹"<<endl;
}
//-----------业务层---------------
int main()
{
//创建出3个锦囊
struct tip *tip1 = create_tip("孔明","赵云",tip1_func);
struct tip *tip2 = create_tip("孔明","赵云",tip2_func);
struct tip *tip3 = create_tip("孔明","赵云",tip3_func);
//庞统的锦囊
struct tip *tip4 = create_tip("庞统","赵云",tip4_func);
//由赵云进行拆锦囊
cout<<"刚刚来到南徐,打开第一个锦囊"<<endl;
open_tips(tip1);
cout<<"---------------------------------"<<endl;
cout<<"刘备乐不思蜀,赵云打开第二个锦囊"<<endl;
open_tips(tip2);
cout<<"---------------------------------"<<endl;
cout<<"孙权大军追杀,赵云打开第三个锦囊"<<endl;
open_tips(tip3);
cout<<"---------------------------------"<<endl;
cout<<"庞统被收编,赵云打开第四个锦囊"<<endl;
open_tips(tip4);
destroy_tip(tip1);
destroy_tip(tip2);
destroy_tip(tip3);
destroy_tip(tip4);
return 0;
}
二十八. 复习1(略)
二十九. 复习2(略)
该系列网课,至此结束