c/c++语言开发共享c++-多态小案例

多态小案例 + C面向接口编程和C多态 函数类型语法基础 函数指针做函数参数(回调函数)思想剖析 函数指针做函数参数两种用法(正向调用、反向调用) + 纯虚函数 抽象类 抽象类基本概念 抽象类在多继承中的应用 面向抽象类编程案例强化 + 面向抽象类编程案例强化 + 抽象类在多继承中的应用 + 抽象类 …


多态小案例

  • c面向接口编程和c多态
    • 函数类型语法基础
    • 函数指针做函数参数(回调函数)思想剖析
    • 函数指针做函数参数两种用法(正向调用、反向调用)
  • 纯虚函数 抽象类
    • 抽象类基本概念
    • 抽象类在多继承中的应用
    • 面向抽象类编程案例强化
  • 面向抽象类编程案例强化
  • 抽象类在多继承中的应用
  • 抽象类基本概念

多态图形案例

#define _crt_secure_no_warnings #include <iostream>   using namespace std;   //抽象的图形类 class shape { public:     //打印出图形的基本你属性     virtual void show() = 0;     //得到图形的面积     virtual double getarea() = 0;      virtual ~shape() {      } };  //圆类 class circle :public shape { public:     circle(double r) {         this->r = r;     }      //打印出图形的基本你属性     virtual void show()  {         cout << "圆的半径是 " << r << endl;     }     //得到图形的面积     virtual double getarea()  {         cout << "获取圆的面积" << endl;         return this->r*this->r *3.14;     }     ~circle() {         cout << "圆的析构函数。。" << endl;     } private:     double r; };  class square :public shape { public:     square(double a) {         this->a = a;     }      //打印出图形的基本你属性     virtual void show() {         cout << "正方形的边长是" << this->a << endl;     }     //得到图形的面积     virtual double getarea() {         cout << "得到正方形的面积" << endl;         return a*a;     }       ~square() {         cout << "正方形的析构函数" << endl;     } private:     double a; };    int main(void) {     shape *array[2] = { 0 };      for (int i = 0; i < 2; i++) {         //生成一个圆         if (i == 0) {             double r;             cout << "请输入圆的半径" << endl;             cin >> r;             array[i] = new circle(r);         }         //生成一个正方形         else {             double a;             cout << "请输入正方形的边长" << endl;             cin >> a;             array[i] = new square(a);         }     }       //遍历这个array数组     for (int i = 0; i < 2; i++) {         array[i]->show();         cout << array[i]->getarea() << endl;          delete array[i];     }      return 0; }

多态案例-程序员工资

#define _crt_secure_no_warnings #include <iostream>   using namespace std;  class programmer { public:     programmer(double salary)     {         this->salary = salary;     }     virtual void printmoney() = 0;     virtual ~programmer() {      }  protected:     double salary; };   class junior_programmer :public programmer { public:     junior_programmer(double salary) :programmer(salary) {      }     virtual void printmoney(){         cout << "初级程序员的工资是" << this->salary << endl;     } };  class mid_programmer :public programmer { public:     mid_programmer(double salary) :programmer(salary) {      }     virtual void printmoney(){         cout << "中级程序员的工资是" << this->salary << endl;     } };  class adv_programmer :public programmer { public:     adv_programmer(double salary) :programmer(salary) {      }     virtual void printmoney(){         cout << "高级程序员的工资是" << this->salary << endl;     } };      int main(void) {      programmer * pro1 = new junior_programmer(12000);      pro1->printmoney();      delete pro1;       programmer * pro2 = new mid_programmer(15000);     pro2->printmoney();     delete pro2;      programmer *pro3 = new adv_programmer(30000);     pro3->printmoney();     delete pro3;            return 0; }

数组类型和数组指针

#define _crt_secure_no_warnings #include <iostream>   using namespace std;  //方法一: 直接定义一个数组类型 typedef int(array_int_10)[10];   //方法二: typedef int(*array_int_10_p)[10];  int main(void) {     int array[10]; //array 应该是一个指向int类型指针。      //方法一:     //array_int_10 *array_10_p = &array; //? *array_10_p === array      //方法二:     array_int_10_p array_10_p = &array;      for (int i = 0; i < 10; i++) {         (*array_10_p)[i] = i + 10;     }      for (int i = 0; i < 10; i++) {         cout << array[i] << endl;     }      //方法三:     int(*p)[10] = &array;      cout << "------" << endl;     for (int i = 0; i < 10; i++) {         cout << (*p)[i] << endl;     }      return 0; }

函数指针

#define _crt_secure_no_warnings #include <iostream>   using namespace std;  int func(int a, int b) {     cout << " 1999 年写的 func" << endl;      return 0; }  int func2(int a, int b) {     cout << "1999 写的 func2" << endl;     return 0; }  int func3(int a, int b)  {     cout << "1999年 写的 func3 " << endl;     return 0; }  //2018想添加一个新的子业务 int new_func4(int a, int b) {     cout << "2018 新写的子业务" << endl;     cout << "a = " << a << ", b = " << b << endl;     return 0; }  //方法一:  函数的返回值, 函数的参数列表(形参的个数,类型,顺序) //定义一个函数类型。  typedef int(func)(int, int);  //方法二:   定义一个函数指针 typedef int(*func_p)(int, int);   //定义一个统一的接口 将他们全部调用起来。  void my_funtion(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(void) { #if 0     //方法一:     func *fp = null;      fp = func;     fp(10, 20);      func_p fp2 = null;      fp2 = func;      fp2(100, 200);      //方法三:     int(*fp3)(int, int)   = null;     fp3 = func;     fp3(1000, 3000); #endif     my_funtion(func, 10, 20);     my_funtion(func2, 100, 200);     my_funtion(func3, 1000, 2000);      my_funtion(new_func4, 2000, 3000);          return 0; }

锦囊妙计

#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream>   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 destory_tip(struct tip *tp) {     if (tp != null) {         free(tp);         tp = 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(void) {     //创建出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);      destory_tip(tip1);     destory_tip(tip2);     destory_tip(tip3);     destory_tip(tip4);          return 0; }

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/600626.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐