虚函数重写代码
时间: 2023-08-06 15:15:33 浏览: 53
以下是一个简单的虚函数重写的示例代码:
```c++
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout << "Drawing a shape..." << endl;
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a circle..." << endl;
}
};
int main() {
Shape* shape = new Circle(); // 基类指针指向派生类对象
shape->draw(); // 输出 "Drawing a circle..."
delete shape;
return 0;
}
```
在上面的代码中,`Shape` 类有一个虚函数 `draw()`,它被派生类 `Circle` 重写了。在 `main()` 函数中,我们通过 `Shape*` 声明了一个基类指针 `shape`,并将其指向一个 `Circle` 类的对象。然后调用 `shape->draw()`,实际上调用的是 `Circle` 类中重写的 `draw()` 函数。这样就实现了多态性。
相关问题
c++ 虚函数示例代码
以下是一个简单的 C++ 虚函数示例代码:
```cpp
#include <iostream>
using namespace std;
// 基类
class Shape {
protected:
double width, height;
public:
Shape(double w = 0, double h = 0) {
width = w;
height = h;
}
virtual double area() {
cout << "Parent class area :" << endl;
return 0;
}
};
// 派生类
class Rectangle : public Shape {
public:
Rectangle(double w = 0, double h = 0) : Shape(w, h) {}
double area() override {
cout << "Rectangle class area :" << endl;
return (width * height);
}
};
int main() {
Shape* shape;
Rectangle rect(5, 6);
shape = ▭
// 调用矩形的求面积函数
cout << shape->area() << endl;
return 0;
}
```
这个程序中,我们定义了一个基类 Shape 和一个派生类 Rectangle。在 Shape 中,我们定义了一个虚函数 area(),在 Rectangle 中重写了这个函数。在 main() 函数中,我们定义了一个指向 Shape 类型的指针,用一个 Rectangle 对象的地址初始化它。然后我们调用这个指针的 area() 函数,由于 area() 是虚函数,因此程序会调用 Rectangle 中的 area() 函数。
c 子类重写父类虚函数
在C++中,子类可以重写父类的虚函数。为了实现函数的重写,子类需要继承父类,并在子类中使用相同的函数名和参数列表重新定义虚函数。
下面是一个示例代码:
```cpp
#include <iostream>
class Parent {
public:
virtual void display() {
std::cout << "This is the parent class." << std::endl;
}
};
class Child : public Parent {
public:
void display() override {
std::cout << "This is the child class." << std::endl;
}
};
int main() {
Parent* ptr = new Child(); // 创建子类对象的基类指针
ptr->display(); // 调用子类重写的虚函数
delete ptr;
return 0;
}
```
在上述代码中,Parent类中的display()函数被声明为虚函数,Child类继承自Parent类,并重写了display()函数。在main函数中,创建了一个Child类对象的Parent类指针,然后通过调用ptr->display()来调用子类重写的虚函数。
运行上述代码将输出:
```
This is the child class.
```
可以看到,子类重写了父类的虚函数,实现了运行时多态性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)