Valid tive C ++ Clause 32: Determine whether your public inheritance is-

Source: Internet
Author: User

If you make class D inherit Class B in the form of public, you will tell the C ++ compiler that every object of the D type is also an object of the B type, and vice versa.

B is more general than D, and D is more special than B. If any function is expected to obtain a real parameter of type B (or pointer to B or reference-to-B, are willing to accept a D object (or pointer-to-D or reference-to-D ). This argument is valid only for public inheritance.

Class bird {
Virtual void fly (); // birds can fly
...
};
Class Penguin: Public bird {// penguin is a bird.
...
};

We suddenly encountered a stream of chaos, because this inheritance system said that penguins would fly, and we know that this is not true.

We became victims of not rigorous language. When we say that birds fly, we do not mean that all birds fly. What we want to say is that normal birds have flight capabilities. We came to the inheritance relationship below to create a better authenticity:

Class bird
{
... // The Fly function is not declared
};
Class flyingbird: Public bird {
Virtual void fly ();
...
};
Class Penguin: Public bird {
... // The Fly function is not declared
};

This inheritance system is more faithful to our true meaning than the original one.

If your program is busy dealing with beam and wings and does not care about flight, the original "dual-class inheritance system" may be quite satisfying. This reflects the fact that there is no perfect design "for all software" in the world. The so-called optimal design depends on what the system wants to do, including the present and future. If your program knows nothing about flight and does not intend to "know" flight in the future, it is a perfect and effective design to distinguish between flying birds and flying birds.

Another school is dealing with the problem that "All birds fly, penguins are birds, but Penguins don't fly". It is to redefine the fly function for penguins, which leads to a runtime error:

Void error (const STD: string & MSG );
Class Penguin: Public bird {
Virtual void fly () {error ("attempt to make a pengnin fly! ");}
};

You must be aware that some of the things mentioned here are different from what you think. This is not to say that "penguins will not fly", but that "penguins will fly, but trying to do so is a mistake ".

You cannot define the fly function for Penguin to demonstrate the limitation that "penguin will not fly.

Class bird
{
... // The Fly function is not declared
};
Class Penguin: Public bird {
... // The Fly function is not declared
};

Now, if you try to let penguin fly, the compiler will condemn your message.

Article 18 said: Good interfaces can prevent Invalid code from being compiled, so you would rather adopt the design of "rejecting penguin flight during compilation, instead of the design that "detects them only at runtime.

In another example, consider this Code:

Class rectangle {
Public:
Virtual void setheight (INT newheight );
Virtual void setwidth (ITN newwidth );
Virtual int height () const;
Virtual int width () const;
};
Void makebigger (rectangle & R)
{
Int oldheight = R. Height ();
R. setwidth (R. Width () + 10 );
Assert (R. Height () = oldheight );
}

Class square: Public rectangle {...}; // The square inherits the rectangle.
Square S;
Assert (S. Width () = S. Height ());
Makebigger (s );
Assert (S. Width () = S. Height ());

Before makebigger is called, the height and width of s are the same;

In the makebigger function, the width of S is changed, but the height is not changed;

After makebigger returns, the s height is the same as the width. (Note that S is passed to makebigger by reference)

? The fundamental difficulty in this example is that some things that may be executed on a rectangle cannot be implemented on a square. However, the public inheritance claim can be applied to everything on the base class object, and also to the derived class object. In the example of square and rectangle, that claim cannot be maintained, so the relationship between them is not correct by inheriting the public model. The compiler will let you pass, but as we have seen, this does not guarantee that the program is performing correctly.

Is A is not the only relationship between classes. The other two common relationships are has-a (one) and is-implemented-in-term-of (implemented based on something ).

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.