现在的位置: 首页 > 综合 > 正文

Abstract Classes and Pure Virtual Functions

2014年02月26日 ⁄ 综合 ⁄ 共 1534字 ⁄ 字号 评论关闭

1. What is an Abstract Class?
Simply stated, an abstract class is a class which does not
fully represent an object. Instead, it represents a broad range of
different classes of objects. However, this representation
extends only to the features that those classes of objects have in
common. Thus, an abstract class provides only a partial
description of its objects.

抽象类不能表现一个完整的对象,它只提供了对一类对象的共有特征的描述.
它和普通类是一般和特殊的关系.在语言中,这些共有的特征由方法描述.

Rumbaugh: “Abstract classes organize features
common to several classes.”

In C++, abstract class is the class that has pure virtual function.

2. STEREOTYPES And POLYMORPHISM.

stereotype
n.
固定形式;老一套

Abstract class can be used to referred to sub class, eventhrough it is an incomplete
description of sub class.

Object is fixed, but under different context, the same object can have different features.

Lippman says: “[An abstract class] provides a common public interface for the entire
class hierarchy.”

The benefit of abstract:(Program to the interface, not to Implementation.)
By using the abstract as placehold, we can send request to sub object for the common feature.
1). Write less code.
2). Stable for change.

3. Designing Applications with Abstract Classes
The steps:
1) find out the concrete class that we need.
2) Spot common feature from the concrete classes.
Note:
  In a more complex application, commonality can be much
harder to detect. The names and forms of the methods can
disguise the intrinsic similarities. Thus, care should be exercised
in the search for commonality.
3) Factoring. (分解, 提升共有特征)
“Factor common responsibilities as high as possible.”

4) Evolution of concrete classes.

4. PURE INTERFACES.
Definition in C++:

class cAbstract
{
private:
  virtual void MemFun() = 0
};

MemFun is a pure interfaces, which has no implementation, but not alwaylse.

 

抱歉!评论已关闭.