虚函数的一个例子

本文通过一个具体的例子展示了虚函数在C++中的应用。在父类中定义虚函数print后,可以实现在运行时调用子类重写的方法。文中详细介绍了如何通过指针和引用实现这一特性,并对比了直接赋值时的行为。
做了一个虚函数的例子。在父函数中指定了print是虚函数,那么父类就可以调用子类的print方法。
答案是:
A::construct
B::construct
This is B
B::destroy
A::destroy
请按任意键继续. . .

注意:
1,2,用指针或者引用将B传个A,虚函数生效。3直接值传递,不生效。




#include <iostream>
using namespace std;


class A
{
public:
A()
{
cout<<"A::construct"<<endl;
}
virtual ~A()
{
cout<<"A::destroy"<<endl;
}
virtual void print()
{
cout<<"This is A"<<endl;
}
};


class B : public A
{
public:
B()
{
cout<<"B::construct"<<endl;
}
~B()
{
cout<<"B::destroy"<<endl;
}
void print()
{
cout<<"This is B"<<endl;
}
};


void main()
{
//A a;
//a.print();
//B b;
//b.print();


//B b;
//A *a;  ---》调用B中的print   (1)
//a = &b;    
//a->print();


B b;
A &a = b;   //---》调用B中的print  (2)
a.print();


//B b;   
//A a;    ->>虚函数不会调用B中的print  (3)
//a = b;  
//a.print();
}















以下为几个不同类型的C++虚函数示例: ### 简单虚函数示例 此示例展示了如何借助基类指针调用派生类的函数,实现运行时多态: ```cpp #include <iostream> class Animal { public: // 定义虚函数 virtual void makeSound() { std::cout << "Animal sound!" << std::endl; } }; class Dog : public Animal { public: // 重基类的虚函数 void makeSound() override { std::cout << "Woof!" << std::endl; } }; class Cat : public Animal { public: // 重基类的虚函数 void makeSound() override { std::cout << "Meow!" << std::endl; } }; int main() { Animal* animal1 = new Dog(); Animal* animal2 = new Cat(); animal1->makeSound(); animal2->makeSound(); delete animal1; delete animal2; return 0; } ``` 在这个示例中,`Animal`类的`makeSound`函数被定义成虚函数,`Dog`和`Cat`类对其进行了重。通过基类指针`Animal*`,能在运行时调用派生类的`makeSound`函数,实现多态[^2]。 ### 纯虚函数与抽象类示例 纯虚函数没有函数体,包含纯虚函数的类为抽象类,不能直接实例化,派生类必须实现所有纯虚函数[^3][^4]。 ```cpp #include <iostream> // 定义抽象类 class Shape { public: // 定义纯虚函数 virtual double area() = 0; }; // 派生类实现纯虚函数 class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) {} double area() override { return 3.14 * radius * radius; } }; // 派生类实现纯虚函数 class Rectangle : public Shape { private: double length; double width; public: Rectangle(double l, double w) : length(l), width(w) {} double area() override { return length * width; } }; int main() { Shape* shape1 = new Circle(5.0); Shape* shape2 = new Rectangle(4.0, 6.0); std::cout << "Circle area: " << shape1->area() << std::endl; std::cout << "Rectangle area: " << shape2->area() << std::endl; delete shape1; delete shape2; return 0; } ``` 在这个示例中,`Shape`类是抽象类,包含纯虚函数`area`。`Circle`和`Rectangle`类继承自`Shape`类,并且实现了`area`函数。 ### 虚析构函数示例 当使用基类指针指向派生类对象时,为了保证在删除基类指针时能正确调用派生类的析构函数,基类的析构函数需要定义为虚析构函数。 ```cpp #include <iostream> class Base { public: Base() { std::cout << "Base constructor" << std::endl; } // 定义虚析构函数 virtual ~Base() { std::cout << "Base destructor" << std::endl; } }; class Derived : public Base { public: Derived() { std::cout << "Derived constructor" << std::endl; } ~Derived() override { std::cout << "Derived destructor" << std::endl; } }; int main() { Base* base = new Derived(); delete base; return 0; } ``` 在这个示例中,`Base`类的析构函数被定义为虚析构函数,这样在删除`Base`类指针时,会先调用`Derived`类的析构函数,再调用`Base`类的析构函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值