子类不重写纯虚函数,那么这个子类也是抽象类吗,请用代码讲解
时间: 2024-03-12 15:49:28 浏览: 63
是的,如果一个子类不重写其父类的纯虚函数,那么这个子类也是一个抽象类,无法实例化对象。
以下是一个示例代码来说明这一点:
```
class AbstractClass {
public:
virtual void doSomething() = 0; // 纯虚函数
};
class ConcreteClass : public AbstractClass {
// 没有重写 doSomething 函数
};
int main() {
// AbstractClass abstract; // 编译错误,抽象类无法实例化对象
ConcreteClass concrete; // 编译通过,ConcreteClass 是 AbstractClass 的子类,但是也是抽象类
return 0;
}
```
在上面的代码中,`AbstractClass` 是一个抽象类,因为它有一个纯虚函数 `doSomething()`。`ConcreteClass` 继承自 `AbstractClass`,但是没有重写 `doSomething()` 函数,因此它也是一个抽象类。在 `main()` 函数中,我们尝试实例化 `AbstractClass` 对象,但是编译会报错。虽然 `ConcreteClass` 继承了 `AbstractClass`,但是它也是一个抽象类,不能实例化对象。
相关问题
1、声明纯虚函数的一般形式是什么?纯虚函数的作用是什么? 2、什么是抽象类?抽象类的作用是什么?
1、声明纯虚函数的一般形式是 virtual 返回类型 函数名 (参数列表) = 0;纯虚函数没有实现,只有声明,子类必须重写这个函数才能被实例化。纯虚函数的作用是为了实现多态性,允许子类在不同的情况下执行不同的操作。
2、抽象类是包含纯虚函数的类,不能被实例化,只能被作为基类使用。抽象类的作用是为了实现多态性,提供一种模板,让子类继承并实现自己的方法,从而达到代码重用和扩展的目的。抽象类还可以用来规范派生类的行为,限制派生类的功能和特性。
纯虚函数与抽象类的定义
### C++ 中纯虚函数与抽象类的概念
#### 纯虚函数的定义
在 C++ 中,当一个类中的某个成员函数被声明为 `virtual` 并赋值为 `= 0` 时,则该函数被称为纯虚函数。这意味着派生类必须重写此函数来提供具体实现[^1]。
```cpp
class Shape {
public:
virtual double circum() const = 0; // 周长, 纯虚函数
};
```
上述代码展示了如何在一个名为 `Shape` 的类中定义了一个返回双精度浮点数类型的纯虚函数 `circum()` 。注意这里的 `= 0` 表明这是一个纯虚函数[^3]。
#### 抽象类的特点
如果一个类至少含有一个未被完全定义(即没有给出具体的实现版本)的纯虚函数,那么这个类就称为抽象类。由于存在无法实例化的纯虚函数,因此不能创建此类的对象;然而可以基于这些抽象基类去构建更具体的子类对象[^2]。
```cpp
// 图形类作为抽象基类
class Shape {
public:
virtual double circum() const = 0;
virtual double area() const = 0;
protected:
int sidesCount_; // 成员变量
};
// 尝试直接实例化会报错
Shape shapeObject; // compile error: cannot declare variable 'shapeObject' to be of abstract type 'Shape'
```
以上例子表明尝试直接实例化 `Shape` 类将会失败,因为它是抽象类。不过可以在继承自它的其他非抽象类里边正常使用:
```cpp
#include <iostream>
using namespace std;
class Rectangle : public Shape {
private:
double width_, height_;
public:
Rectangle(double w, double h):width_(w),height_(h){}
virtual double circum() const override{
return (width_ + height_) * 2;
}
virtual double area() const override{
return width_*height_;
}
void showInfo(){
cout << "Rectangle Info:" << endl;
cout << "Width=" << width_
<< ", Height="<< height_
<< ", Circumference="<< this->circum()
<< ", Area="<<this->area()<<endl;
}
};
int main(){
Rectangle rect(5.0f,7.0f);
rect.showInfo();
}
```
这段程序成功编译并运行良好,因为它实现了所有的纯虚方法,并且提供了额外的功能。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)