Writing-related detail carding of C + + object-oriented classes

Source: Internet
Author: User

Problems with classes

Reasons for inheriting classes: To add or replace features.

1. Method of overriding class when inheriting V Replace function

① set all methods to virtual (virtual functions), just in case .

Virtual: Experience has shown that it is best to set all methods to virtual, including destructors but not constructors;

This does not have to worry about whether the rewrite method is running, and the only drawback is that it has a slight impact on performance;

Even if a class is unlikely to be extended, it is best to set the method of this class to virtual, just in case.

② The syntax of the overridden method: Re-declares the method in the subclass definition and provides a new definition in the subclass's implementation file. (Note: The virtual keyword does not need to be reused in the method definition)

③ pointers or objects that refer to objects or subclasses that can point to a class. For example, if you have a Super reference to a parent class and actually refer to a subclass sub object, the method that is called is actually a method that invokes the version of the child class.

Parent class:Super class;

Subclass:Sub class;

Sub mysub;

super& ref = MySub;

Ref.somemethod (); The somemethod () method of the subclass is called here

If the virtual keyword is omitted from the parent class , the override function cannot be completed.

④ a method or member of a subclass that is not defined in the parent class cannot be accessed even if the reference or pointer of the parent class knows that this is actually a child class. For example:

Someothermethod () is a subclass of Sub-method, the parent class does not have the method;

Sub mysub;

super& ref = MySub;

Ref.someothermethod (); BUG

Mysub.someothermethod (); can perform

⑤ non-pointer non-reference objects are feature information that does not correctly handle subclasses. For example:

Sub mysub;

Super assignedobject = mysub;

Assignedobject.somemethod (); At most, only the SomeMethod () method of the parent class Super is called .

⑥ Summary: When a pointer or reference to a parent class is directed to a subclass object, the subclass retains its overriding method, but if the subclass object is converted to a superclass object through type conversion, the feature is lost, the overriding method, and the loss of the subclass data becomes truncated.

⑦ marks the method as final, which means that the method cannot be overridden in a subclass. For example:

Virtual void SomeMethod () final;

2. How to reuse code with inheritance

V consider setting the method to a static method when a method is the same for all instances of the class;

V

3. Using the parent class

When writing a subclass, you need to know how the parent class interacts with the child classes. The creation order, the constructor chain, and the type conversion are all potential source of bugs .

V Create order

① if a class has a base class, the default constructor for the base class is executed.

Non-static data members of the ② class are created in the order in which they are declared.

③ executes the constructor for the class.

V destructor Call Order

① calls the destructor of the class.

② destroys the data members of the class, and wants to send the order of the creation.

③ If there is a parent class, call the parent class's destructor. (all destructors are declared as virtual????? )

V based on experience, all destructors should be declared asVirtual. For example:

V passing the constructor's arguments from the subclass to the parent class is normal, but the data member cannot be passed. (If you do, the code compiles, but remember that the data member is initialized after the parent class constructor is called, and if the data member is passed as a parameter to the parent class constructor, the data member is not initialized.) V When overriding a method in a parent class in a subclass, if you want to invoke the method of the parent class in the overridden method, be careful to add the parent class scope, otherwise an infinite loop will be executed. For example:

Sting myweatherprediction::gettemperature () const

{

Return weatherprediction::gettemperature () + "F"; Be sure to add the parent class scope here

}

4. Transition between parent class and subclass V upward transformation (i.e. subclass to parent class)

When you are transitioning upward, you can avoid truncation by using a parent class pointer or reference.

V down transformation (that is, the parent rotor Class), precautions:

The above is not practical, the following is the correct method.

V Use a downward transition only if necessary, and be sure to use thedynamic_cast.

Inheritance and polymorphism 1. Polymorphism of a class V in practice, our code rarely uses the property of class polymorphism, in fact, the polymorphism of the class is so easy to use.

The above design shows a way to make the Spreadsheetcell hierarchy polymorphic. Since Doublspreadsheetcell and Stringspreadsheetcell are inherited from the same parent class Spreadsheetcell, they are interchangeable from the perspective of other code. In fact, this means:

L Two subclasses support the same interface (the set of methods) defined by the base class.

The code that uses the Spreadsheetcell object can call any method in the interface without knowing whether the cell format is Stringspreadsheetcell or Doublspreadsheetcell.

Because of the special ability of the virtual method, the correct instance of each method in the interface is called according to the class to which the object belongs.

l Other data structures can pass through a reference to a parent class type that contains more than one type of cell.

V Note the following questions need to be considered to make reasonable use of the properties of the polymorphism of the class:

① when designing a base class, you should consider the relationship between the subclasses, and based on this information, you can extract the common attributes and place them in the parent class.

② Pure virtual method: The pure virtual method shows in the class definition that the method does not need to be defined, because the base class is not an instance (if a class contains one or more pure virtual methods, this type of object cannot be constructed), and a pure virtual method is specified with a specialized syntax: The method declaration is followed by the =0 ; in . CPP file, you do not need to write any code. For example:

③ abstract base class: The above mentioned base class, is an abstract base class, is not instantiated, but can use the abstract base class pointer or reference, because actually point to the subclass object.

Abstract classes provide a way to prevent other code from instantiating objects directly, and its subclasses can instantiate objects.

Writing-related detail carding of C + + object-oriented classes

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.