只要類別含有任何一個 pure virtual function 就稱為 abstract class.
ex.
class Shape { // abstract class
virtual void rotate(int) = 0; // pure virtual function
virtual void draw() = 0; // pure virtual function
virtual bool is_closed = 0; // pure virtual function
...
};
abstract class 無法建立任何 object,只能做為其他 object 的 interface & base object
base:
ex.
class Point { /* */ };
class Circle : public Shape {
public:
void rotate(int) {} // 覆蓋 Shape::rotate
void draw(); // 覆蓋 Shape::draw
bool is_closed() { return true; } // 覆蓋Shape::is_closed
Circle(Point p, int r);
private:
Point center;
int radius;
};
interface:
ex. driver
class Character_device {
public:
virtual int open(int opt) = 0;
virtual int close(int opt) = 0;
virtual int read(char* p, int n) = 0;
virtual int write(const char* p, int n) = 0;
virtual int ioctl(int ...) = 0;
virtual ~Character_device() {}
};
可從 Character_device 衍生出各式各樣的 driver object
有 abstract class 便能以 object 拼出完整模組化的系統。
No comments:
Post a Comment