Pure Virtual Functions and Abstract Classes in C++
来源:互联网 发布:人体透视镜软件下载 编辑:程序博客网 时间:2024/05/22 05:05
reference http://quiz.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/
1) A class is abstract if it has at least one pure virtual function.
2) We can have pointers and references of abstract class type.
3) If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.
4) An abstract class can have constructors.
For example, the following program compiles and runs fine.
#include<iostream>
using
namespace
std;
// An abstract class with constructor
class
Base
{
protected
:
int
x;
public
:
virtual
void
fun() = 0;
Base(
int
i) { x = i; }
};
class
Derived:
public
Base
{
int
y;
public
:
Derived(
int
i,
int
j):Base(i) { y = j; }
void
fun() { cout <<
"x = "
<< x <<
", y = "
<< y; }
};
int
main(
void
)
{
Derived d(4, 5);
d.fun();
return
0;
}
Interface vs Abstract Classes:
An interface does not have implementation of any of its methods, it can be considered as a collection of method declarations. In C++, an interface can be simulated by making all methods as pure virtual.
An interface does not have implementation of any of its methods, it can be considered as a collection of method declarations. In C++, an interface can be simulated by making all methods as pure virtual.
0 0
- Pure Virtual Functions and Abstract Classes in C++
- Abstract Classes and Pure Virtual Functions
- Pure Virtual Functions and Abstract Classes
- Some tips about Abstract base classes and pure virtual functions
- Error 2259 : cannot instantiate abstract class with one or more pure virtual functions
- c / c++ virtual function and pure virtual function summarize
- How do virtual functions work in C++?
- URL functions and classes
- Friend functions and classes
- C#: Interface, Abstract and Virtual 的区别
- Pure virtual destructor in C++
- Abstract Classes and Factory Design Pattern in Python
- non-virtual and virtual functions
- Inheritance and virtual functions
- Abstract Classes (C++)
- C++: Pure Virtual Function Called
- Closure and anonymous functions in Objective-C
- Java-Abstract classes and methods
- 【cordova】hybrid app调试(同样适用于移动端浏览器或webview)
- Redis持久化存储(AOF与RDB两种模式)
- jQuery 文档操作方法
- GreenDao3.2的使用,以及实体类添加字段时进行升级。
- 转码
- Pure Virtual Functions and Abstract Classes in C++
- Android将GPS封装为Service
- dubbo 实践笔记
- 网站seo优化的时候标题是重中之重,必须做写好标题
- 在Vue上初级使用ECharts
- mysql proxy user代理用户(伪装用户)
- 亦是美网络,致力于操作系统应用与计算机网络技术的IT网站。
- java面试③Web部分
- struts2中配置文件的加载顺序