C ++ abstract class, abstract class

Source: Internet
Author: User

C ++ abstract class, abstract class

1. Pure virtual function definition
A pure virtual function is a virtual function declared in the base class. It is not defined in the base class, but any derived class must define its own implementation method. To implement pure virtual functions in the base class, add "= 0" after the function prototype"

Ii. Reasons for introduction:
1. To facilitate the use of polymorphism, we often need to define virtual functions in the base class.
2. In many cases, it is unreasonable for the base class to generate objects. For example, an animal can be derived from sub-classes such as tigers and peacocks as a base class, but the object generated by the animal itself is obviously unreasonable.
To solve the above problem, the concept of pure virtual Function is introduced, and the Function is defined as a pure virtual Function (method: virtual ReturnType Function () = 0 ;), the compiler requires that the class must be overloaded to implement polymorphism. Classes that contain pure virtual functions are called abstract classes and cannot generate objects. In this way, the above two problems are well solved.

 

Classes with pure virtual functions are called abstract classes. An abstract class is a special class established for the purpose of abstraction and design. It is at the upper layer of the hierarchy of inheritance.Abstract classes cannot define objects. In practice, to emphasize that a class is an abstract class, you can describe the constructor of this class as a protected access control permission.

The main function of an abstract class is to organize the relevant organizations in an inheritance hierarchy to provide them with a public root. The related subclass is derived from this root.

Abstract classes depict the General Semantics of the Operation interfaces of a group of child classes. These semantics are also passed to child classes. In general, an abstract class only describes the operation interfaces of this group of sub-classes, and the complete implementation is left to the sub-classes.

An abstract class can only be used as a base class. The implementation of its pure virtual function is provided by the derived class. If the derived class does not redefine the pure virtual function, and the derived class only inherits the pure virtual function of the base class, the derived class is still an abstract class. If the implementation of the basic class pure virtual function is provided in the derived class, the derived class is no longer an abstract class. It is a concrete class that can create objects.

Abstract class requirements

(1) abstract classes can only be used as the base classes of other classes, and abstract class objects cannot be created.

(2) abstract classes cannot be used as parameter types, function return types, or explicitly converted types.

(3)You can define a pointer and reference to an abstract class. This pointer can point to its derived class to implement polymorphism.

 

# Include <iostream> using namespace std; const double PI = 3.14159; class Shapes // abstract class {protected: int x, y; public: void setvalue (int d, int w = 0) {x = d; y = w;} virtual void disp () = 0; // pure virtual function}; class Square: public Shapes {public: void disp () {cout <"rectangular area:" <x * y <endl ;}}; class Circle: public Shapes {public: void disp () {cout <"circular area:" <PI * x <endl ;}}; int main () {Shapes * ptr [2]; // define the array of object pointers, Square s1; Circle c1; ptr [0] = & s1; ptr [0]-> setvalue (10, 5 ); ptr [0]-> disp (); ptr [1] = & c1; ptr [1]-> setvalue (10); ptr [1]-> disp (); return 0 ;}

 

Iii. Concept of similarity:
1. Polymorphism
The same object receives different messages or different objects receive the same message to produce different implementation actions. C ++ supports two types of polymorphism: compile-time polymorphism and Runtime polymorphism.
A. polymorphism during compilation: implemented through function overloading and operator overloading.
B Runtime polymorphism: implemented through inheritance and virtual functions.
2. Virtual Functions
A virtual function is a member function declared as virtual in the base class and redefined in the derived class. This function can be dynamically overloaded.
The declaration of pure virtual functions has a special syntax format:Virtual return value type member function name (parameter table) = 0;

 

Note that pure virtual functions should only be declared and have no specific definitions. Even if the definition of pure virtual functions is given, they will be ignored by the compiler.

 

3,Abstract class

 

Classes that contain pure virtual functions are called abstract classes. Abstract classes contain non-defined pure virtual functions, so the objects of abstract classes cannot be defined.
In C ++, we can set classes that can only be used for inheritance but cannot directly create objects as Abstract classes ).

 

Abstract classes exist mainly because they have uncertainties. The member functions that do exist in those classes but cannot be determined in the parent class are called pure virtual functions. A pure virtual function is a special virtual function. It has only declarations and has no specific definitions. At least one pure virtual function exists in the abstract class; the class with pure virtual function must be an abstract class. The existence of pure virtual functions is a sufficient condition for being an abstract class.

 

// Base class: class A {public: A (); void f1 (); virtual void f2 (); virtual void f3 () = 0; virtual ~ A () ;}; // subclass: class B: public A {public: B (); void f1 (); void f2 (); void f3 (); virtual ~ B () ;}; // main function: int main (int argc, char * argv []) {A * m_j = new B (); m_j-> f1 (); m_j-> f2 (); m_j-> f3 (); delete m_j; return 0;}/* f1 () is a normal overload. when m_j-> f1 () is called, f1 () in Class A is called, which is determined when we write the code. because f1 () is not A virtual function, it will not be dynamically bound, that is, it is defined by Class A, so that the function of this class is called. f2 () is a virtual function. when m_j-> f2 () is called, the corresponding function is called in the object saved in m_j. this is because the new B object. f3 () is the same as f2 (), but does not need to write function implementation in the base class. */

 

The differences between virtual functions and pure virtual functions are as follows.

(1) If a virtual function is declared in the class, this function is implemented. Even if it is a null implementation, it is used to enable this function to be overwritten in its subclass, in this way, the compiler can use later binding to achieve polymorphism. A pure virtual function is just an interface and a declaration of a function. It must be left in the subclass for implementation.
(2) virtual functions can not be overloaded in the subclass. However, pure virtual functions must be implemented in the subclass, just like Java interfaces. Adding many functions to virtual is a good habit. Although some performance is sacrificed, object-oriented polymorphism is increased, it is hard to predict that the function in the parent class does not modify its implementation in the subclass.
(4) classes with pure virtual functions are called virtual base classes. These base classes cannot directly generate objects. They can only be used after they are inherited and overwritten. Such classes are also called abstract classes. Abstract classes are different from the virtual base classes that are commonly said. abstract classes are defined in C #. abstract classes are defined in C ++, but abstract classes are not defined in C ++. After an abstract class is inherited, The subclass can continue to be an abstract class or a common class, while the virtual base class is a class containing pure virtual functions. If it is inherited, then the subclass must implement all pure virtual functions in the virtual base class, and its subclass cannot be an abstract class.

 

Iii. Differences between abstract classes and interfaces:
1. class is the abstraction of objects. abstract classes can be understood as classes as objects. abstract classes are called abstract classes. the interface is just a behavior specification or provision. Microsoft's custom interface always carries the able field behind it to prove that it represents a class "I can do it... ". Abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions.
2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
3. A class can implement several interfaces at a time, but only one parent class can be extended.
4. interfaces can be used to support callback, but inheritance does not.
5. the abstract class cannot be sealed.
6. The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, you can also declare them as virtual.
7. Similar to a non-abstract class, an abstract class must provide its own implementation for all the members of the interface listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods.
8. abstract classes implement a principle in oop that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented.
9. A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution.
10. Avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined (based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example, in asp.net, the Page class has Server Request and other attributes, but in fact they are all objects of a certain class. This object of the Page class is used to call the methods and attributes of other classes. This is a very basic design principle.
11. If an abstract class implements an interface, you can map the methods in the interface to the abstract class as an abstract method without having to implement it. Instead, you can implement the methods in the subclass of the abstract class.

Related Article

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.