文章目录
- 虚函数
- 1. 虚函数
- 1.1 虚函数案例1
- 1.2 虚函数案例2
- 1.2 纯虚函数
- 1.3 纯虚函数语法要求总环
- 1.4 纯虚函数应用
-
虚函数
1. 虚函数
1.1 虚函数案例1
#include <iostream>using namespace std;class Animal
{
public:// Animal 类内 eat 函数加入的 virtual 修饰virtual void eat() { cout << "动物吃饭" << endl; }
};class Dog : public Animal
{
public:void eat() { cout << "Dog 吃饭" << endl; }
};
class Cat : public Animal
{
public:void eat() { cout << "Cat 吃饭" << endl; }
};
class Bird : public Animal
{
public:void eat() { cout << "Bird 吃饭" << endl; }
};// 当前函数所需类型为 Animal 类型
void test_eat(Animal *a);int main(int argc, char const *argv[])
{Animal *a = new Animal;Dog *dog = new Dog;Cat *cat = new Cat;Bird *bird = new Bird;/*test_eat 函数可以使用 dog cat 和 bird 作为实际参数【注意】当前函数所需参数为 Animal 类型,使用 dog cat 和 bird 作为实际参数是符合开发要求的,因为 dog cat 和 bird 都是 Animal 的子类,可以看作是【Animal】类型从某种意义上来说,dog cat 和 bird 都是 Animal 类型【知识点】函数参数为父类类型,可以使用父类本身或者子类作为函数的实际参数*/ test_eat(dog);test_eat(cat);test_eat(bird);return 0;
}void test_eat(Animal *a)
{/*当前情况下,使用子类作为当前函数的参数,可以执行对应的代码,单数执行的内容是 Animal 内容,非对应子类内容因为对于编译器而言,a 是 Animal 类型,编译器会首先选择调用Animal 类当前中的 eat 函数期望的是:传入的具体数据类型,也就是子类的函数被执行父类的指针指向子类的对象,并且想调用子类的函数,这个操作很 virtual父类函数没有 virtual 修饰之前动物吃饭动物吃饭动物吃饭父类函数使用 virtual 修饰之后Dog 吃饭Cat 吃饭Bird 吃饭编译器根据【真实类型】调用目标函数*/a->eat();
}
1.2 虚函数案例2
#include <iostream> using namespace std;class Base
{
public:virtual void test() { cout << "Base 类 test 函数" << endl; }
};class Sub : public Base
{
public:void test() { cout << "Sub 类 test 函数" << endl; }
};int main(int argc, char const *argv[])
{Base * b = new Sub;b->test();delete b;return 0;
}
1.2 纯虚函数
#include <iostream> using namespace std;class Base
{
public:/*virtual 修饰的虚函数,存在函数体,子类通过继承之后,得到当前函数内容,并且执行使用*/virtual void test() { cout << "虚函数" << endl; }/*【纯】虚函数1. 使用 virtual 修饰2. 函数有且只有声明3. 函数声明之后又 = 0 操作,特定格式!告知编译器当前函数没有函数体,只有函数声明!*/virtual void test_pure() = 0;
};/*
Sub 是 Base 类中的一个子类,继承 Base 类中的 public 和 protected 修饰内容
当前 Sub 从 Base 类中获取到virtual void test() {......} 存在函数体virtual void test_pure() = 0; 没有函数体的虚函数提示报错:
不允许使用抽象类类型 "Sub" 的对象:C/C++(322)
02_纯虚函数.cpp(42, 18): 纯虚拟 函数 "Base::test_pure" 没有强制替代项报错中可以获取到的信息:1. 一个类内存在【纯虚函数】,当前类 C++ 认为是一个【抽象 abstract 类】2. 纯虚函数没有函数体,如果实例化对象想要调用纯虚函数,语法错误,所有编译器前置语法报错告知【纯虚拟 函数 "Base::test_pure" 没有强制代替项】理解为:子类继承父类的纯虚函数,需要【实现/完成函数体】*/
class Sub : public Base
{
public:void test_pure() { cout << "子类【强制】实现纯虚函数" << endl; }
};int main(int argc, char const *argv[])
{Sub *m = new Sub;m->test();m->test_pure();delete m;return 0;
}
1.3 纯虚函数语法要求总环
- 纯虚函数没有函数体,只有函数声明和特定的 = 0 格式
- 纯虚函数所在的类,是一个【抽象 abstract 类】
- 抽象无法进行实例化操作。因为抽象类有纯虚函数没有函数体,实例化对象无法调用,所有语法前置报错,提示不能实例化
- 子类继承抽象类,如果实现抽象类中的纯虚函数,子类不再是一个抽象类,如果未实现,还是一个抽象类
1.4 纯虚函数应用
1.4.1 生活案例

1.4.2 虚函数引用代码
#include <iostream>using namespace std;// Base 是一个【抽象类】
class Base
{
public:// 使用【纯虚函数】制定函数实现规则,限制【函数三要素:返回值类型、函数名、形式参数列表】virtual void boil() = 0;virtual void make_tea() = 0;virtual void add_spices() = 0;virtual void seal_cup() = 0;
};/*
通用的 Base.实现 Base 抽象类中部分函数泡茶喝添加小料操作尚未实现,具备一定的【特征性和唯一性】
*/
class GenericBase : public Base
{
public:virtual void boil() { cout << "纯净水烧制 100 ° " << endl; }virtual void seal_cup() { cout << "自动封杯机工作" << endl; }
};/*
【实现类】具备一定的特征性和使用性
*/
class BYJX : public GenericBase
{
public: BYJX() { name = "伯牙绝弦"; }void make_tea() { cout << "茉莉花茶" << endl; }void add_spices() { cout << "牛乳" << endl; }string getName() { return name; } private:string name;
};class QGPE : public GenericBase
{
public:QGPE() { name = "青柑普洱"; }void make_tea() { cout << "青柑普洱" << endl; }void add_spices() { cout << "三分糖" << endl; }string getName() { return name; }
private: string name;
};class GreenTea : public GenericBase
{
public:GreenTea() { name = "绿茶"; }void make_tea() { cout << "信阳毛尖" << endl; }void add_spices() { cout << "三分糖" << endl; }string getName() { return name; }private: string name;
};class Frappuccino : public GenericBase
{
public:Frappuccino() { name = "摩卡可可碎星冰乐"; }void boil() { cout << "常温纯净水" << endl; }void make_tea() { cout << "咖啡" << endl; }void add_spices() { cout << "摩卡酱 + 可可碎" << endl; }void seal_cup() { cout << "自动封杯机工作" << endl; }string getName() { return name; }
private:string name;
};int main(int argc, char const *argv[])
{BYJX *b = new BYJX;b->boil(); // 纯净水烧制 100 ° b->make_tea(); // 茉莉花茶b->add_spices(); // 牛乳b->seal_cup(); // 自动封杯机工作cout << b->getName() << endl; // 伯牙绝弦cout << "------------------------" << endl;QGPE *q = new QGPE;q->boil(); // 纯净水烧制 100 ° q->make_tea(); // 青柑普洱q->add_spices(); // 三分糖q->seal_cup(); // 自动封杯机工作cout << q->getName() << endl; // 青柑普洱cout << "------------------------" << endl;GreenTea *g = new GreenTea;g->boil(); // 纯净水烧制 100 ° g->make_tea(); // 信阳毛尖g->add_spices(); // 三分糖g->seal_cup(); // 自动封杯机工作cout << g->getName() << endl; // 绿茶cout << "------------------------" << endl;Frappuccino *f = new Frappuccino;f->boil(); // 纯净水烧制 100 ° f->make_tea(); // 咖啡f->add_spices(); // 摩卡酱 + 可可碎f->seal_cup(); // 自动封杯机工作cout << f->getName() << endl; // 摩卡可可碎星冰乐delete b;delete q;delete g;delete f;return 0;
}
----------------------" << endl;Frappuccino *f = new Frappuccino;f->boil(); // 纯净水烧制 100 ° f->make_tea(); // 咖啡f->add_spices(); // 摩卡酱 + 可可碎f->seal_cup(); // 自动封杯机工作cout << f->getName() << endl; // 摩卡可可碎星冰乐delete b;delete q;delete g;delete f;return 0;
}