Cla36: differentiate interface inheritance and implement inheritance

Source: Internet
Author: User

As a class designer, you sometimes want the derived class to inherit only the interface (Declaration) of the member function. Sometimes, you want the derived class to inherit both the interface and Implementation of the function, but allow the class to be rewritten; sometimes you want to inherit both interfaces and implementations, and do not allow the derived class to rewrite anything.

class Shape {public:  virtual void draw() const = 0;  virtual void error(const string& msg);  int objectID() const;  ...};class Rectangle: public Shape { ... };class Ellipse: public Shape { ... };

First, look at the pure virtual function draw. The most notable feature of pure virtual functions is that they must be re-declared in any specific class that inherits them, and they are often not defined in abstract classes. When we put these two features together, we will realize that:

· The purpose of defining a pure virtual function is to make the derived class only inherit the function interface.

It is also possible to provide definitions for a pure virtual function. That is to say, you can provide implementation for shape: draw, and the C ++ compiler will not block it, but the only way to call it is to specify which call is complete through the Class Name:

Shape * PS = new shape; // error! Shape is an abstract shape * PS1 = new rectangle; // correct PS1-> draw (); // call rectangle: drawshape * PS2 = new ellipse; // correct PS2-> draw (); // call ellipse: drawps1-> shape: Draw (); // call shape: drawps2-> shape :: draw (); // call shape: Draw

Sometimes it is useful to declare a class that does not contain anything except pure virtual functions. This class is called the Protocol class. It provides only function interfaces for the derived classes and is not implemented at all. The terms of agreement have been introduced in Clause 34 and will be mentioned again in Clause 43.

The purpose of declaring a simple virtual function is to make the derived class inherit the interface and default Implementation of the function.

The base class provides default behavior for the sub-classes, and implements the sub-classes only when the sub-classes want them: disconnects the interface of the virtual function from its default implementation.

Method 1:

Class airplane {public: Virtual void fly (const airport & Destination) = 0 ;... protected: void defaultfly (const airport & Destination) ;}; void airplane: defaultfly (const airport & Destination) {default code for flights to a certain destination}

Note airplane: fly has become a pure virtual function, which provides the flight interface. The default implementation still exists in the airplane class, but now it exists in the form of an independent function (defaultfly. If modela and modelb classes want to execute default behaviors, they simply make an inline call to defaultfly in their fly function bodies.

class ModelA: public Airplane {public:  virtual void fly(const Airport& destination)  { defaultFly(destination); }  ...};class ModelB: public Airplane {public:  virtual void fly(const Airport& destination)  { defaultFly(destination); }  ...};

For modelc classes, it is impossible to inherit the incorrect fly implementation accidentally. Because the pure virtual function in airplane forces modelc to provide its own fly version.

Class modelc: Public airplane {public: Virtual void fly (const airport & Destination );...}; void modelc: Fly (const airport & Destination) {code for modelc to fly to a destination}

Method 2: The pure virtual function must be declared again in the subclass, but it can still have its own implementation in the base class.

Class airplane {public: Virtual void fly (const airport & Destination) = 0 ;...}; void airplane: Fly (const airport & Destination) {default code for flights to a certain destination} class modela: Public airplane {public: Virtual void fly (const airport & Destination) {airplane: Fly (destination );}...}; class modelb: Public airplane {public: Virtual void fly (const airport & Destination) {airplane: Fly (destination );}...}; class modelc: Public airplane {public: Virtual void fly (const airport & Destination );...}; void modelc: Fly (const airport & Destination) {code for modelc to fly to a destination}

The purpose of declaring a non-virtual function is to make the derived class inherit the interface and mandatory implementation of the function. When a member function is a non-virtual function, its behavior in the derived class should not be different. In fact, a non-virtual member function represents a kind of special immutability, because it represents a behavior that will not change-no matter how special a derived class has.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.