Effective C + + notes (31-34)

Source: Internet
Author: User



---------------------------15/04/20----------------------------



#32 determine your public inheritance plastic film out is-a relationship

{

/*

1> Subclass is-a parent class.

If you inherit as public, you tell the C + + compiler that every object of the type subclass is also an object of the parent class.

that is, the parent class shows a more generalized concept.

2> As long as the parent class can be used, the subclass will certainly be useful, but if the subclass can be used, the parent class will not necessarily be able to use it.

3>is-A 's confusion points:

1) Bird can fly, penguins are bird, so can also fly:

here first bird not all can fly, so here should classify into flybird and notflybird. Penguins can inherit .

it's notflybird.

However, some systems do not use penguins, and can fly bird is still a lot of, so you can properly consider not to distinguish can fly

That is, learning to be flexible.

2) The square is a rectangle:

When the rectangle adjusts the length, the width will not change, but when the square adjusts the length, the width should change accordingly.

So the solution is to rewrite the setwidth and setheightof the squares so that the two functions change one and the other changes.

after all, the square is a special rectangle, you have to change my long,OK, width also must be changed.

*/

}


//#33 avoid hiding the inherited name

{

/// First look at the following code:

class Base

{

public:

Virtual void mf1 ();

Virtual void mf1 (int);

Virtual void mf3 ();

Virtual void mf3 (int);

};

class Derived: Public Base

{

virtualvoid mf1 (); MF1 (int) is not defined , so the base class's mf1 (int) is obscured.

using    BASE::MF1; // this will allow mf1 (int) be seen.

Virtual void mf3 ()

{

base::mf3 (); //!!!!! if the private inheritance !!!!!!! ( Don't do this if you don't have private inheritance. ) , so to do

// can deliberately obscure the base class mf3 (int), leaving only the base class of mf3 ()

}

};

}


//#34 differentiate between interface inheritance and implementation inheritance

{

/*

1: subclass inherits interfaces There are always three cases:

1> only inherits the function interface, does not inherit the implementation:pure virtual function can implement such a function.

2> inherited interfaces and missing implementations: impure virtual functions. This can lead to dangerous situations. Because this interface does not require the customer to force

Implementation, it can cause errors if the customer is not implemented if necessary.

The solution is: the base class provides a missing implementation of the member function, when the subclass inherits the interface, if you want the default implementation, call the base class directly

the provided member function is the line. At this point the interface is declared pure virtual , the subclass must be re-implemented, as to the default, the sub-class

self-tuning does not call anymore.

3> inherited interfaces and mandatory implementations ( not allowed to modify ),non_virtual functions. Declaring a non_virtual means that the base class does not want the subclass to redefine this

interface.

2: error Condition:

1> is all implemented as a non_virtual function, which makes it impossible for subclasses to customize their own case. And the destructor must be virtual (for example ,

If you want this class to be a base class )

2> all implemented as virtual, Exception:interface Classes, in addition to this class interface, other classes should not be fully set interface to virtual

Because this could be a precursor to the lack of firm stance of class designers. Some functions are not meant to be redefined!

*/

}


Effective C + + notes (31-34)

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.