Objective C ++ Reading Notes clause 12 and Clause 13

Source: Internet
Author: User

Objective C ++ Reading Notes clause 12 and Clause 13

Clause 12: Determine whether your public inherited plastic film has a-a relationship:

This clause mainly applies to some special cases: for example, if a penguin is a bird, it can inherit from the bird, but the bird can fly, but the penguin cannot fly; it may also be embarrassing to let the square inherit the rectangle!

I have thought about this problem before, but I don't know how to solve it. If it is used in real life, many animals, such as cats, dogs, pigs, and so on, inherit the Animal class, but cats, dogs, and so on won't swim,

If there are many animals, you cannot add a method to the fish! I haven't figured out this yet. Clause 12 has not explained how to deal with this problem, so it will be avoided.

Is-;

In object-oriented programming, ia-a refers to the parent-child inheritance relationship of classes;

Public inheritance means that is-a. Everything that applies to base-class must also be applied to derived class. Therefore, each subclass object is also a parent class object.

Clause 13: Avoid hiding inherited names:

This clause focuses on covering inheritance. In fact, this is what we often call overwrite (overwrite) hiding. Next we will look at the differences between overload rewriting and hiding:

1. Reload features: in the same class, functions have the same name, parameters are different, and virtual keywords are optional.
2. Rewrite (overwrite) features: these features are respectively located in the derived class and the base class; the function name is the same; the parameter is the same; the base class function must have the virtual keyword (this should be noted ).

About hiding:


1. If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
2. If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. In this case, the function of the base class is hidden (Be sure not to confuse it with rewriting ).

// 06.cpp: defines the entry point of the console application. ///*************************************: Find out the time difference between Overloading, rewriting, and hiding: december 1, 2014 19:06:03 *//*********************************** * ***********************************/# include "stdafx. h "# include
 
  
Using namespace std; class Base {private: int x; public: virtual void mf1 () = 0; virtual void mf1 (int) // overload, in the same class or within the same work interval. {Cout <"Call the mf1 function of the base class" <
  
   


The above code should explain the differences among the three. In Clause 33, if you do not want the subclass to hide the function of the parent class, use the using declarative method in the subclass to achieve the goal:

Class Derived: public Base {public: virtual void mf1 () {cout <"Call the mf1 function of the subclass" <
    
     





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.