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;
}
c++-多态小案例的更多相关文章
- JAVA之旅(八)——多态的体现,前提,好处,应用,转型,instanceof,多态中成员变量的特点,多态的案例
JAVA之旅(八)--多态的体现,前提,好处,应用,转型,instanceof,多态中成员变量的特点,多态的案例 学习是不能停止的 一.多态 我们今天又要学习一个新的概念了,就是多态,它是面向对象的第 ...
- 机械表小案例之transform的应用
这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...
- shell讲解-小案例
shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...
- [jQuery学习系列六]6-jQuery实际操作小案例
前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...
- 02SpringMvc_springmvc快速入门小案例(XML版本)
这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:
- React.js入门小案例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- SqlDependency缓存数据库表小案例
SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...
- JavaScript apply函数小案例
//回调函数1 function callback(a,b,c) { alert(a+b+c); } //回调函数2 function callback2(a,b) { alert(a+b); } / ...
- Session小案例------完成用户登录
Session小案例------完成用户登录 在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...
随机推荐
- 解决 bash cd too many arguments 报错
解决 bash: cd: too many arguments 本来想着用git bash进入文件夹,但是文件夹名称中带有空格,例如:my blog,导致出错. 在查找资料后,找到一种并不可行的方案, ...
- 2019-11-4:渗透测试,bypass学习,笔记
编码方式过wafurl编码,针对特殊情况可以两次URL编码十六进制编码,针对某些数据,如特殊字符,特殊字符串等unicode编码,使用两个字节编码一个字符,高位不足使用0填充单引号:%u0027.%u ...
- 【Luogu P4779】dijkstra算法的堆优化
Luogu P4779 利用堆/优先队列快速取得权值最小的点. 在稠密图中的表现比SPFA要优秀. #include<iostream> #include<cstdio> #i ...
- Git同步本地项目文件到github
1.登录自己的github账号,并创建一个存放项目代码的仓库 输入仓库名称后,点击create,然后来到完成后的页面,copy下以下内容,后面会用到 2.打开本地的git 安装好git后,打开git的 ...
- Java多线程面试题:线程锁+线程池+线程同步等
1.并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行. 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对变量 ...
- OC 初次接触
初次接触ObjC时,会发现许多和其它语言不同的地方,会看到很多的+,- ,[ ,] ,@, NS等符号,这些符号在以后的编程中将经常看到,这部分内容在第二节中介绍.先熟悉一下ObjC的代码: #imp ...
- Djangoday2第二个app加减法
第二个app 计算新建一个app在view定义显示的内容修改urls指定连接对应的视图测试另一种通过路径传参的方式访问网址路径传参的urls定义方法网址路径传参测试urls的urlnamedjango ...
- php踩过的那些坑(2) strpos引发的血案
一.前方有坑 php某些自带函数,如果使用不当,也会坑得你人仰马翻.比如:strpos() 先了解一下strpos()函数是干啥的. strpos — 查找字符串首次出现的位置 用法: int str ...
- 翻转二叉树(深搜-先序遍历-交换Node)
题目:翻转二叉树,例如 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 已知二叉树的节点定义如下: class TreeNode { in ...
- python2和python3编码问题
欢迎加入python学习交流群 667279387 一.什么是编解码 1.什么是unicode 2.编码方式 二.python中的编解码 1.python2 (1).encode() 和 .decod ...