总结:

1,含有纯虚函数的类,称为抽象基类,不可实列化。即不能创建对象,存在 的意义就是被继承,提供族类的公共接口。

2,纯虚函数只有声明,没有实现,被“初始化”为 0。

3,如果一个类中声明了纯虚函数,而在派生类中没有对该函数定义,则该虚函数在派生类中仍然为纯虚函数,派生类仍然为纯虚基类。

1、基本概念

纯虚函数是一个在基类中说明的虚函数在基类中没有定义,要求任何派生类都定义自己的版本

纯虚函数为个派生类提供一个公共界面(接口的封装和设计、软件的模块功能划分)

2、纯虚函数的语法

virtual 类型 函数名(参数表) = 0;

一个具有纯虚函数的基类称为抽象类

3、C++中没有接口的概念,C++中可以使用纯虚函数实现接口

接口类中只有函数原型定义,没有任何数据的定义.

代码示例:

01纯虚函数:

#if 1
#include<iostream>
using namespace std;//图形类
//如果说一个类只要有拥有纯虚函数,就称此类为抽象类。(不管这个类有没有其他数据成员)
//抽象类 不能实例化
class Shape {
public://求图形类的面积//表示图形类声明一个方法getArea(),它是一个纯虚函数。灭有函数的实现virtual double getArea() = 0;   //纯虚函数int a;int b;
};//正方形
//如果说一个普通类继承了一个抽象类,则必须重写纯虚函数。
//若不重写所有纯虚函数,则子类仍为抽象类,依然不能被实例化
//
class Rect :public Shape {
public:Rect(int a) {this->a = a;}virtual double getArea() {cout << "Rect:getArea" << endl;return a*a;}
private:int a;  //正方形边长
};//不需要关心Rect的实现
class Circle :public Shape {
public:Circle(int r) {this->r = r;}virtual double getArea() {cout << "Circle:getArea" << endl;return 3.14*a*a;}
private:int r;  //圆半径
};class Tri :public Shape {
public:Tri(int d,int h) {this->d = d;this->h = h;}virtual double getArea() {cout << "Circle:getArea" << endl;return d*h/2;}
private:int d;   //底int h;   //高
};//面向抽象类写一个架构函数
void printArea(Shape *sp) {sp->getArea();
}//业务层 面向的抽象类编程
void test01() {//main中所有使用的变量地址 都是抽象类Shape的类型//Shape s;  //编译错误 抽象类不能实例化Shape *sp1 = new Rect(3);  //父类指针指向子类对象sp1->getArea();   //多态实现Shape *sp2 = new Circle(3);  //父类指针指向子类对象sp2->getArea();Shape *sp3 = new Tri(3,3);  //父类指针指向子类对象sp3->getArea();
}
/*
Rect:getArea
Circle:getArea
Circle:getArea
*/
int main() {test01();return 0;
}
#endif

02抽象类练习:

#if 1
#include<iostream>
using namespace std;
class Boss {
public:virtual void fight() = 0;
};
class DongFangBuBai :public Boss {
public:virtual void fight() {cout << "东方不败使出了葵花宝典" << endl;}
};
class DuanYu :public Boss {
public:virtual void fight() {cout << "段誉使出了六脉神剑" << endl;}
};
class WuYaZi :public Boss {
public:virtual void fight() {cout << "无崖子使出了北冥神功" << endl;}
};
void test01() {Boss *Dong = new DongFangBuBai;Dong->fight();Boss *Duan = new DuanYu;Duan->fight();Boss *Wu = new WuYaZi;Wu->fight();delete Dong;delete Duan;delete Wu;
}
/*
东方不败使出了葵花宝典
段誉使出了六脉神剑
无崖子使出了北冥神功
*/
int main() {test01();return 0;
}
#endif

03纯虚函数和多继承练习:

#if 1
#include<iostream>
using namespace std;
class Interface1 {
public:virtual void func1(int a, int b) = 0;
};
class Interface2 {
public:virtual void func2(int a) = 0;
};
class Child :public Interface1, public Interface2 {
public:virtual void func1(int a, int b) {cout << "func1" << endl;}virtual void func2(int a) {cout << "func2" << endl;}
};
void test01() {Child d;Interface1 *if1 = new Child;if1->func1(1,2);   //只能看见func1,看不见func2
}
/*
func1
*/
int main() {test01();return 0;
}
#endif

抽象类练习2:

Animal1.h

#pragma once
#include<iostream>
using namespace std;
class Animal1
{
public:virtual void voice() = 0;Animal1();virtual ~Animal1();};//架构函数
//让动物叫
void letAnimalCry(Animal1 *animal);
void letAnimalCrys(Animal1 *animal);

Animal1.cpp

#include "Animal1.h"Animal1::Animal1()
{cout << "Animal1()" << endl;
}Animal1::~Animal1()
{cout << "~Animal1()" << endl;
}void letAnimalCrys(Animal1 * animal)
{animal->voice();delete animal;  //这里删除,调用时不必删除
}
void letAnimalCry(Animal1 *animal) {animal->voice();
}

Cat1.h

#pragma once
#include "Animal1.h"
class Cat1 :public Animal1
{
public:Cat1();~Cat1();virtual void voice();
};

Cat1.cpp

#include "Cat1.h"
Cat1::Cat1()
{cout << "Cat1()" << endl;
}Cat1::~Cat1()
{cout << "~Cat1()" << endl;
}
void Cat1::voice() {cout << "cat is crying" << endl;
}

Dog1.h

#pragma once
#include "Animal1.h"
class Dog1 :public Animal1
{
public:Dog1();~Dog1();virtual void voice();
};

Dog1.cpp

#include "Dog1.h"
Dog1::Dog1()
{cout << "Dog1()" << endl;
}Dog1::~Dog1()
{cout << "~Dog()" << endl;
}
void Dog1::voice() {cout << "dog is crying" << endl;
}

main.cpp


#include"Animal1.h"
#include"Dog1.h"
#include"Cat1.h"void test01() {Animal1 *dog = new Dog1;dog->voice();delete dog;  //如果Animal的析构函数没有声明为虚函数,运行delete不会调用子类析构函数如果Animal的析构函数声明为虚函数,运行delete会调用子类析构函数Animal1 *cat = new Cat1;cat->voice();delete cat;
}
/*
Animal1()
Dog1()
dog is crying
~Dog()
~Animal1()
Animal1()
Cat1()
cat is crying
~Cat1()
~Animal1()
*/
void test02() {Animal1 *dog = new Dog1;Animal1 *cat = new Cat1;letAnimalCry(dog);letAnimalCry(cat);delete dog;   //架构函数中未执行删除对象操作 调用时执行删除操作delete cat;
}
/*
Animal1()
Dog1()
Animal1()
Cat1()
dog is crying
cat is crying
~Dog()
~Animal1()
~Cat1()
~Animal1()
*/
void test03() {Animal1 *dog = new Dog1;Animal1 *cat = new Cat1;letAnimalCrys(dog);//架构函数中执行删除对象操作 调用时不需要执行letAnimalCrys(cat);}
/*
Animal1()
Dog1()
Animal1()
Cat1()
dog is crying
~Dog()
~Animal1()
cat is crying
~Cat1()
~Animal1()
*/
//test03 等价于 test04
void test04() {letAnimalCrys(new Dog1);letAnimalCrys(new Cat1);
}
/*
Animal1()
Dog1()
dog is crying
~Dog()
~Animal1()
Animal1()
Cat1()
cat is crying
~Cat1()
~Animal1()
*/
int main() {//test01();cout << "-------"<<endl;//test02();cout << "-------" << endl;//test03();cout << "-------" << endl;//test04();return 0;}

C++基础17-纯虚函数和抽象类相关推荐

  1. C++学习12:C++多态、虚函数、虚析构函数、纯虚函数、抽象类

    一 多态概述 C++中的多态分为静态多态和动态多态.静态多态是函数重载,在编译阶段就能确定调用哪个函数.动态多态是由继承产生的,指同一个属性或行为在基类及其各派生类中具有不同的语义,不同的对象根据所接 ...

  2. c++ 纯虚函数和抽象类那些事(一)

    1.纯虚函数与抽象类 C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数! 纯虚函数:没有函数体的虚函数 抽象类:包含纯虚函数的类 /*** @br ...

  3. C++纯虚函数和抽象类

    基本概念 纯虚函数和抽象类 纯虚函数是一个在基类中说明的虚函数,但是在基类中没有定义,要求任何派生类都定义自己的版本 纯虚函数为个派生类提供一个公共界面(接口的封装和设计.软件模块功能的划分) 纯虚函 ...

  4. C++基本概念复习之二:多重继承、虚继承、纯虚函数(抽象类)

    一.多重继承: #include <iostream> using namespace std; class Horse { public: Horse(){cout<<&qu ...

  5. C++之纯虚函数和抽象类

    纯虚函数和抽象类 1.基本概念 2.案例 #include <iostream> using namespace std;////面向抽象类编程(面向一套预先定义好的接口编程)//解耦合 ...

  6. C++纯虚函数与抽象类

    纯虚函数 1.1纯虚函数是在声明虚函数时被"初始化"为0的函数.声明纯虚函数的一般形式为: virtual 函数类型 函数名 (参数列表) =0; 如 virtual float ...

  7. 9-2:C++多态之纯虚函数和抽象类以及接口继承和实现继承

    文章目录 (1)纯虚函数和抽象类的概念 (2)抽象类的意义 (3)接口继承与实现继承 (1)纯虚函数和抽象类的概念 如果一个类的虚函数后面写上=0,同时不写它的实现,那么这样的虚函数称之为纯虚函数,包 ...

  8. C++ 虚函数,纯虚函数,抽象类整理

    抽象类,类中包含纯虚函数的为抽象类,其中抽象类的子类必须实现抽象类的纯虚函数方法. 抽象类无法实例化 虚函数,子类可以实现或者不实现该方法都可以 如果父类调用子类的基类指针时,有虚函数的则使用子类的实 ...

  9. c/c++入门教程 - 2.4.7 多态、函数地址晚绑定(重写,虚函数,纯虚函数,抽象类,虚析构,纯虚析构)

    目录 4.7 多态 4.7.1 多态的基本概念(超级重要) 4.7.2 多态的原理刨析(超级重要) 4.7.2 多态案例一:计算器类 4.7.3 纯虚函数和抽象类 4.7.4 多态案例二 - 制作饮品 ...

  10. C++ 纯虚函数与抽象类

    1.虚函数 1.1 虚函数简介 可以毫不夸张地说虚函数是C++最重要的特性之一,我们先来看一看虚函数的概念. 在基类的定义中,定义虚函数的一般形式为: virtual 函数返回值类型 虚函数名(形参表 ...

最新文章

  1. Sql语法---DDL
  2. 手把手教你如何做建模竞赛(baseline代码讲解)
  3. 东汉末年,他们把「服务雪崩」玩到了极致
  4. Martin Fowler谈《重构HTML:改善Web应用的设计》
  5. .jQuery文档分析4-文档处理
  6. CV之API:利用Face++的人体识别接口,实现摄像头实时手势识别
  7. July面试整理系列--(5)
  8. mysq5.7 主主同步
  9. 现代软件工程 作业汇总
  10. 步步高DVD机DV603的U盘模式支持视频格式
  11. python里的class_Python中的Class的讨论
  12. 如何打造标签式IE浏览器 (共享源码)
  13. etherboot无盘启动
  14. 数据治理之元数据管理实践
  15. 计算机制谱软件finale+2011应用教程,Finale2014(打谱软件)
  16. Windows 10正式版的历史版本
  17. web3.0是什么意思(web3和元宇宙的关系)
  18. 基于openssl的3DES(ECB)加密算法
  19. 【Python】【教程】Python 教程
  20. IBM p系列小型机日常维护及故障排除时常用命令(检查IBM设备状态汇总)

热门文章

  1. pom.xml中依赖的<optional>true</optional>标签
  2. Java面向对象编程篇4——内部类
  3. Java基础篇3——流程控制
  4. c语言编写劫持dll,c语言-----劫持自己02
  5. linux远程连接最大数是多少,Linux Shell 脚本限制ssh最大用户登录数
  6. 备份恢复linux,备份和恢复Linux系统
  7. java lang报错_java.lang.UnsupportedClassVersionError:JDK版本不一致报错
  8. 正则匹配承兑的html,正则匹配闭合HTML标签(支持嵌套)
  9. pythonrandom库seed_Python
  10. android 支付模块封装,Android集成支付----支付宝支付总结与封装