In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In view of what is encapsulation inheritance and polymorphic understanding in C++, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Encapsulation: is to combine the abstract data and behavior (or function) to form an organic whole, that is, to organically combine the data with the source code of operating data to form a "class", in which data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming, users do not need to know the specific implementation details, but only through the external interface, specific access rights to use the members of the class. Encapsulation can hide the implementation details and make the code modular.
Inheritance: C++ supports inheritance through class derivation. The inherited type is called a base class or superclass, and the newly generated class is a derived class or subclass. The process of constructing a new class while maintaining the properties of an existing class is called inheritance. The process of generating a new class by adding its own features to an existing class is called derivation. The purpose of inheritance and derivation is to maintain the characteristics of existing classes and to construct new classes. The purpose of inheritance: to achieve code reuse. The purpose of derivation: to achieve code expansion. There are three ways of inheritance: public, protected, private.
The constructor when inheriting: (1), the constructor of the base class cannot be inherited, and the constructor of the derived class needs to declare its own constructor; (2) when declaring the constructor, only the new members in this class need to be initialized, initializing the inherited members of the base class, automatically calling the constructor of the base class; (3) the constructor of the derived class needs to pass parameters to the constructor of the base class (4) Constructor for single inheritance: derived class name: derived class name (parameter table) {initialization assignment statement for members of this class): base class name (parameter table) {initialization assignment statement for members of this class;}; (5) when a constructor in the default form or undeclared constructor is declared in the base class, the derived class constructor may not pass parameters to the base class constructor (6) if the constructor is not declared in the base class, it may not be declared in the derived class, all in the default form; (7) when the base class declares a constructor with formal parameters, the derived class should also declare the constructor with formal parameters and pass parameters to the base class constructor. (8), the call order of the constructor: a, the base class constructor is called in the order declared when they are inherited (from left to right); B, the constructor of the member object is called according to the order in which they are declared in the class; C, the contents of the constructor body of the derived class.
The destructor at the time of inheritance: (1), the destructor is not inherited, and the derived class declares itself; (2), the declaration method is the same as the destructor of the general (no inheritance relationship) class; (3) there is no need to explicitly call the destructor of the base class, the system will automatically implicitly call; (4), the order of the destructor is the opposite of the constructor.
The same name hiding rule: when the derived class and the base class have the same members: (1), if the derived class object is not forcibly named, then the derived class object is used to access the overwritten member of the base class with the same name; (2), if you want to access the overwritten member of the base class through the derived class object, you should use the base class name to qualify: base class name:: data member name.
Virtual base class: function: (1), mainly used to solve the ambiguity problem caused by multiple inheritance of the same base class; (2) to provide unique base class members for the farthest derived class without repeatedly producing multiple copies.
Inheritance, combination: combination is the use of objects of other classes as members, and inheritance is a member method in which a subclass can use the parent class. (1) An inherits B, which shows that An is a kind of B, and all the behaviors of B are meaningful to A. (2) if An is logically "part" of B, B is not allowed to derive from A. instead, B should be combined with An and other things. (3) inheritance belongs to "white box" reuse, and combination belongs to "black box" reuse.
Polymorphic can be simply summed up as "one interface, multiple methods". The program decides which function to call at run time. C++ polymorphism is achieved through virtual functions, which allow subclasses to redefine member functions, and the practice of subclasses redefining their parents is called overriding or rewriting. Overloading allows multiple functions with the same name, and these functions have different parameter lists, different number of parameters, different parameter types, or both. With regard to polymorphism, in short, you use a pointer of the parent type to point to an instance of its subclass, and then call the member function of the actual subclass through the pointer of the parent class.
The essential difference between polymorphism and non-polymorphism is whether the function address is early-bound or late-bound. If the function is called, the calling address of the function can be determined during compiler compilation, and the code can be generated, which is static, that is, the address is early bound. If the address of the function call cannot be determined during compilation, it needs to be determined at run time, which is late binding.
Encapsulation can make code modular, inheritance can extend existing code, and their purpose is code reuse. The purpose of polymorphism is for interface reuse. In other words, no matter which class of the object is passed, the function can be called to the implementation method that adapts to the respective object through the same interface.
The most common use is to declare the pointer of the base class, which points to any subclass object and calls the corresponding virtual function, which can implement different methods according to the subclass. If the virtual function is not used, that is, if the C++ polymorphism is not used, the corresponding function will always be limited to the base class function itself when calling the corresponding function with the base class pointer, and can not be called to the overridden function in the subclass. Because there is no polymorphism, the address of the function call will be certain, and the fixed address will always be called to the same function, which can not achieve the purpose of one interface, multiple methods.
A pure virtual function is a virtual function declared in the base class, which is not defined in the base class, but requires any derived class to define its own implementation method. The way to implement a pure virtual function in the base class is to add "= 0" after the function prototype. In order to facilitate the use of polymorphic features, it is often necessary to define virtual functions in the base class, and in many cases, it is unreasonable for the base class itself to generate objects. In order to solve these problems, the concept of pure virtual function is introduced, and the function is defined as pure virtual function, then the compiler requires that it must be rewritten in derived classes to achieve polymorphism. A class that also contains pure virtual functions is called an abstract class, and it cannot generate objects. Because there is no definition of pure virtual function in the class where pure virtual function is located, pure virtual function is not allowed to be called in the constructor and destructor of this class, otherwise it will lead to program running error, but other member functions can call pure virtual function.
C++ supports two kinds of polymorphism: (1), compile-time polymorphism (static polymorphism, which can determine the form of objects used at compile time): through overloaded functions; and (2) run-time polymorphism (dynamic polymorphism, the specific reference object can only be determined at run time): through virtual functions.
In C++, there are the following methods to implement polymorphism: virtual functions, abstract classes, overloads, overrides, and templates.
Function overloading (Overload): means that in the same scope (as in the same class), the function has different parameters with the same name, and the return value is ignored. Different parameters can be different numbers or different types. Effect: call the corresponding function body according to the number and type of arguments.
Function override (Override) (function rewriting): the function in the assigned class overrides the virtual function with the same name in the base class, so the scope is different. Effect: when the base class pointer or reference accesses the virtual function, the corresponding function is called according to the type of the instance.
Function Hide: for functions with the same name as the base class in a subclass, it becomes hidden if it is not overridden. Two cases: (1) different parameters with the same name; (2) the same name with the same parameter but the base class is not a virtual function.
Derived class constructor description: (1), in the derived class constructor, as long as the base class is not only using no parameters of the default constructor, to be displayed to give the base class name parameter table; (2), the base class does not define the constructor, the derived class can not be defined, using the default constructor; (3), the base class with parameters constructor, the derived class must define the constructor.
The overloaded function of a virtual function is still a virtual function. When a derived class redefines a virtual function, it must have the same function prototype, including the return type, the function name, the number of parameters, and the order of parameter types. The virtual function must be a member function of the class, not a global function, nor a static function. A friend cannot be described as a virtual function, but a virtual function can be a friend of another class. A destructor can be a virtual function, but a constructor cannot be a virtual function. Generally speaking, if there is a virtual function defined in a class, its destructor should also be described as a virtual function. This is especially true when destructors need to do something meaningful, such as freeing memory. When accessing a virtual function in a class system, you should use a pointer to the base class type or a reference to the base class type to meet the requirements of runtime polymorphism. Of course, you can also use the object name to call a function as if it were a normal member function. If a virtual function is not redefined in a derived class, the object of that class uses the virtual function code in its base class.
Abstract class: if there is at least one pure virtual function in a class, this class is called an abstract class. Abstract classes include not only pure virtual functions, but also virtual functions. A pure virtual function in an abstract class may be defined in an abstract class, or it may be inherited and redefined from its abstract base class. An important feature of an abstract class is that it must be used as a base class to derive other classes and cannot be used to create object instances directly. The reason an abstract class cannot create an object directly is that one or more of the functions are undefined, but run-time polymorphism can still be supported using pointers to abstract classes. The pure virtual function in the base class must be overloaded in the derived class, otherwise it will still be treated as an abstract class. A pure virtual function inherited from the base class is still a virtual function in the derived class.
Virtual function table: virtual function is realized through a virtual function table. Abbreviated as V-Table, in this table, it is mainly the address table of the virtual function of a class, this table solves the problem of inheritance and coverage, and ensures that it truly reflects the actual function. In this way, in an instance of a class with a virtual function, the table is allocated to the memory of the instance, so when we use the pointer of the parent class to manipulate a subclass, whether the virtual function table is important or not, it is like a map, indicating the function that should actually be called.
An example of polymorphism:
# include using namespace std; class A {public:void foo () {printf ("1\ n");} virtual void fun () {printf ("2\ n");}}; class B: public A {public:void foo () {printf ("3\ n");} void fun () {printf ("4\ n");}}; int main (void) {An a X B; A * p = & an esp-> foo (); / / 1p-> fun (); / / 2 p = & bscape p-> foo () / / 1p-> fun (); / / 4B* ptr = (B*) & a return ptre-> foo (); / / 3ptr- > fun (); / / 2 return 0;}
Another example:
# include using namespace std; int main (void) {class CA {public:virtual ~ CA () {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.