[22] Inheritment - Abstract Base (Abcs) (Part of C FAQ Lite, Copyright © 1991-2001, Marshall Cline, Cline@parashift.com)
体,, nicrosoft @ sunistudio.com (East day production room, East day document)
FAQS in Section [22]:
[22.1] What is the effect of separating interfaces and implementation? [22.2] How to separate the interface and implementation in C (就 象 lLA-2)? [22.3] What is ABC? [22.4] What is "pure virtual" member function? [22.5] How to define a copy constructor or assignment operator for a class define a class that pointing (abstract) base class?
[22.1] What is the effect of separating interfaces and implementation?
The interface is the most valuable resource company. The design interface is more time for this interface than using a pile. And the interface requires more expensive human time. Since the interface is valuable, they should be protected to avoid being destroyed because of the data structure and other implementations. Therefore, the interface should be separated and implemented.
[TOP | BOTTOM | Previous Section | Next Section]
[22.2] How to separate the interface and implementation in C (就 象 lLA-2)?
Use ABC. (Translation: Abstract base class Abstract base class)
[TOP | BOTTOM | Previous Section | Next Section]
[22.3] What is ABC?
[Recently Renamed "Subclass" to "Derived Class" (on 7/00). Click Here to Go To The Next FAQ in The "Chain" of Recent Changes
]
Abstracta class (Abstract base class).
At the design level, the abstract base class (ABC) corresponds to the abstract concept. If you ask if he repairs a means of transportation, he may want to know which kind of means of means you said. He does not repair space aircraft, ocean wheel, bicycle or nuclear submarine. The problem is that the "Transportation" is an abstract concept (for example, unless you know which means of transportation you want to build, you can't build a "means of transport"). In C , the Vehicle class is an ABC, and Bicycle (bicycle), SpaceShuttle is a derived class (OceanLiner). In the true world of OO, ABC is everywhere. At the program language level, the abstract base class (ABC) is a class with one or more pure event functions. Objects (examples) of abstract base classes cannot be established.
[TOP | BOTTOM | Previous Section | Next Section]
[22.4] What is "pure virtual" member function?
[Recently Renamed "Subclass" to "Derived Class" (ON 7/00). Click Here to Go To The next FAQ in The "chain" of real Changes.]
The common class becomes a member function of an abstract base class (that is, ABC). It is usually only implemented in the derived class.
Some member functions are only existed in the concept without reasonable definitions. For example, suppose I let you draw a graphic in coordinates (x, y), size is 7. You will ask me: "Which graphic should I draw?" (Circle, rectangle, hexagon, etc.). In C , we must point out the real thing of the Draw () member function (by this user to call it when there is a Shape * or Shape &, we realize that it is logically, it can only be in the subclass. definition:
Class Shape {public: Virtual Void Draw () const = 0;
// = 0 means it is "pure virtual"
// ...
}
This pure virtual function makes Shape become ABC (abstract base class). If you like, you can see "= 0;" syntax as the code located at the NULL pointer. Therefore, Shape promises to its users, but Shape cannot provide any code to implement this commitment. This makes it possible to give a member function if there is no information that does not have enough information to actually define a member function.
Note that it is possible to provide a pure virtual function, but this usually makes the beginner confused, and it is best to avoid this until it is skilled.
[TOP | BOTTOM | Previous Section | Next Section]
[22.5] How to define a copy constructor or assignment operator for a class define a class that pointing (abstract) base class?
If the class has a (abstract) base class pointer pointing to the object, the fictional creation function is used in the (abstract) base class. Just like the general usage, declare a pure virtual method in the base class clone ():
Class shape {public:
// ...
Virtual Shape * Clone () const = 0;
// Virtual (copy) constructor
// ...
}
Then implement a clone () method in each derived class:
Class Circle: Public Shape {public:
// ...
Virtual Shape * Clone () const {return new circle (* this);
// ...
}; Class Square: public shape {public:
// ...
Virtual Shape * Clone () const {return new square (* this);
// ...
}
Now suppose each Fred object has a shape object. Fred objects naturally don't know how Shape is round or rectangular or ... Fred's copy constructor and assignment operator calls the shape's clone () method to copy the object:
Class Fred {public: Fred (Shape * P): p_ (p) {assert (p! = null);
// pmust not be null
~ Fred () {delete p_;} fred (const fly & f): p_ (f.p _-> clone ()) {} fred & operator = (const fly & f) {i (this! = & F) {// check Value
Shape * p2 = f.p_-> clone ();
// Create The New One First ...
delete p_;
// ... the delete the old one
P_ = p2;} return * this;
// ...
PRIVATE: Shape * p_;};
[TOP | BOTTOM | Previous Section | Next Section]
E-mail the author [C FAQ Lite | Table of Contents | Subject Index | About The Author | © | Download Your Own Copy] Revised Apr 8, 2001