您的位置:首页 > 汽车 > 时评 > 网页设计与制作实训报告800字_重庆市工程建设信息网2021_seo在线教学_公司推广策划方案

网页设计与制作实训报告800字_重庆市工程建设信息网2021_seo在线教学_公司推广策划方案

2025/6/20 3:25:43 来源:https://blog.csdn.net/qq_39400324/article/details/144313072  浏览:    关键词:网页设计与制作实训报告800字_重庆市工程建设信息网2021_seo在线教学_公司推广策划方案
网页设计与制作实训报告800字_重庆市工程建设信息网2021_seo在线教学_公司推广策划方案

7.2 纯虚函数和抽象类


  纯虚函数是在基类中声明的虚函数,它在基类中 没有定义具体的函数体,而是通过在函数声明的结尾添加= 0来表示。纯虚函数的目的是 为派生类提供一个统一的接口规范,要求派生类 必须实现这个函数。

     class Base {public:virtual void pureVirtualFunction() = 0;};
#include <iostream>class Base {
protected:int x, y;public:void setxy(int i, int j = 0) {x = i;y = j;}virtual void getarea() = 0; // pure virtual function
};
// square
class Square : public Base {
public:void getarea() {std::cout << "x is " << x << std::endl;std::cout << "suqare area is " << x * x << std::endl;}
};
// rectangle
class Rectangle : public Base {
public:void getarea() {std::cout << "x is " << x << std::endl;std::cout << "y is " << y << std::endl;std::cout << "rectangle area is " << x * y << std::endl;}
};
// cube
class Cube : public Base {
public:void getarea() {std::cout << "x is " << x << std::endl;std::cout << "cube area is " << 6 * x * x << std::endl;}
};int main(int argc, char const *argv[]) {Base *ptr;Square a;Rectangle b;Cube c;ptr = &a;ptr->setxy(4);ptr->getarea();ptr = &b;ptr->setxy(4, 5);ptr->getarea();ptr = &c;ptr->setxy(4);ptr->getarea();
}
x is 4
suqare area is 16
x is 4
y is 5
rectangle area is 20
x is 4
cube area is 96

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com