Reprint Address: http://blog.csdn.net/ruyue_ruyue/article/details/8211809
three basic features of object-oriented
The three basic features of object-oriented are encapsulation, inheritance, polymorphism. Where encapsulation can hide implementation details, allowing code to be modular, inheritance can extend existing code modules (classes), and their purpose is to-code reuse. Polymorphism is to achieve another goal--interface reuse.
Encapsulation
what is encapsulation.
Encapsulation hides implementation details, makes code modular, encapsulates processes and data, and accesses data only through defined interfaces. Object-oriented computing begins with the basic concept that the real world can be depicted as a series of fully autonomous, encapsulated objects that pass through a protected
Interface to access other objects. Object-oriented programming can be understood as: to encapsulate the objective things into abstract classes, and the class can put its own data and methods only trusted class or object operations, untrusted information hidden.
inherited
what is inheritance.
Inheritance refers to the ability to use all the features of an existing class and to extend them without having to rewrite the original classes. The process of its inheritance is the process from general to special.
New classes created by inheritance are called subclasses or derived classes. The inherited class is called the base class, the parent class, or the superclass. To implement inheritance, you can do so by inheriting (inheritance) and combining (composition). In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and for multiple inheritance to be implemented by multilevel inheritance.
how inheritance is implemented.
The inheritance concept is implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance.
1. Implementation inheritance refers to the ability to use the properties and methods of the base class without additional coding;
2. Interface inheritance refers to the ability to use only the names of properties and methods, but subclasses must provide implementations;
3. Visual inheritance refers to the ability of a subform (class) to use the appearance of a base form (class) and to implement code.
polymorphic
what is polymorphism.
Polymorphism (POLYMORPHISN) is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assigning a value, the parent object can operate in a different way depending on the attributes of the child object that is currently assigned to it. To put it simply, it is a sentence that allows a pointer to a subclass type to be assigned to a pointer to a parent class type.
Example: (2012 A * * software company written questions)
Please write out the output of the following code in sequence:
Answer: Call child func
Call ~child
Call ~base
Analysis of the implementation mode of polymorphism.
To achieve polymorphism, there are two ways to overwrite, overload. Overwrite: Refers to the practice of subclasses redefining the virtual functions of the parent class. Overloading: Refers to allowing multiple functions with the same name, which have different parameter tables (perhaps the number of parameters is different, perhaps the parameter types are different, perhaps both).
Analysis:
Overload refers to the same return type and method name in the same class, but the number and type of parameters can be different
Overwrite \ Override is in a different class.
In fact, the concept of overloading does not belong to "object-oriented programming", and the implementation of overloading is that the compiler modifies the name of a function of the same name based on a different parameter table of the function, and then these functions of the same name become different functions (at least for the compiler). For example, there are two functions with the same name: function func (p:integer): integer; and function func (p:string): integer;. Then the compiler's modified function name might be this: Int_func, Str_func. The invocation of these two functions has been determined between the compilers and is static (remember: Static). That is, their addresses are bound at compile time (early bound), so overload and polymorphism are irrelevant. The real and polymorphic correlation is "overwrite." After the subclass has redefined the virtual function of the parent class, the parent class pointer is dynamically based on the different subclass pointers assigned to it (remember: dynamic.) is a subclass of this function, such a function call cannot be determined during compilation (the address of the virtual function of the calling subclass cannot be given). Therefore, such a function address is bound at run time (late Pradesh). The conclusion is that overloading is only a linguistic feature, independent of polymorphism and not object oriented. Quote the words of Bruce Eckel: "Don't be silly, if it's not a late bond, it's not polymorphic." ”
the implementation of C + + polymorphism mechanism:
This part is transferred from: http://blog.chinaunix.net/uid-7396260-id-2056657.html
1, C + + implementation of polymorphic methods
An important concept for object-oriented is an instance of an object, an instance of an object represents a specific object, so it must have a data structure that holds the object's member variables, and if the object has a virtual function method or has virtual inheritance, then there is a virtual function or a virtual table pointer, Other function pointers are not included.
The realization mechanism of virtual function in C + + is to use virtual table and virtual pointer, but what is the concrete? From more effecive C + + one article inside can know: is each class uses a virtual table, each class object with a virtual pointer. To talk about the virtual function mechanism, we must speak of inheritance, because only inherited virtual functions of the dynamic binding function, first of all, C + + inherited object instance memory allocation basics:
From more effecive C + + one article inside can know: is each class uses a virtual table, each class object with a virtual pointer. The specific usage is as follows:
Class A
{public:
virtual void f ();
virtual void g ();
Private
int a
};
Class B:public A
{
Public
void G ();
Private
int b;
};
The realization of A,B is omitted
Because A has virtual void f (), and G (), the compiler prepares a virtual table Vtablea for Class A, which reads as follows:
A::f's address
A::g's address
b because it inherits a, the compiler also prepares a virtual table Vtableb for B, which reads as follows:
A::f's address |
B::g's address |
Note: Because B::g is rewritten, the G of B's virtual table is the B::G entry address, but F is inherited from the above, so the address of F is the A::F entry address. Then somewhere there is a statement B BB; When the compiler allocates space, except for the member int B of A's int a,b, it also assigns a virtual pointer vptr, which points to B's virtual table VTABLEB,BB layout as follows:
VPTR: A virtual table pointing to B Vtableb |
int a: Member of inherited a |
int B:B Member |
When the following statement:
A *pa = &bB;
The structure of the PA is the layout of a (that is, only the first two items of the BB object can be accessed with PA, the third item int b is not accessed)
So Pa->g (), the compiler knows that G is a member function declared virtual, and that its entry address is placed in the 2nd item of the table (whether it is a vtalbea table or a vtalbeb table), then the compiler compiles the statement as a conversion: Call (pa- >VPTR) [1] (C-language array index starting from 0 ha ~).
This is the entry address of the B::G (), then the polymorphism is realized. (Note that the BB vptr points to a virtual table of B Vtableb)
In addition to note that, such as the implementation is not unique, C + + standards only require this mechanism to achieve polymorphism, as for the virtual pointer vptr in the end where the layout of an object, the standard does not require, each compiler to decide for themselves. My results are based on the g++ 4.3.4 of the disassembly analysis.
2. Two kinds of polymorphism implementation mechanism and its advantages and disadvantages
In addition to C + + this polymorphic implementation mechanism, there is another implementation mechanism, but also look-up table, but by name look-up table, is Smalltalk and other language implementation mechanism. The advantages and disadvantages of these two methods are as follows:
(1), according to the absolute position check table, this method is already indexed and table entries (such as call (Pa->vptr[1) above) in the compilation phase. So the running speed is relatively fast; the disadvantage is that when a has more virtual members (such as 1000), and b rewrites fewer members (such as 2), The remaining 998 entries in B's Vtableb are pointers to virtual member functions in a, and if the derivation system is larger, it wastes a lot of space.
For example: GUI library, take MFC library for example, MFC has many classes, is an inheritance system; and many times each class is just 1, 2 member functions need to be overridden in a derived class, and if you use C + + virtual function mechanism, each class has a virtual table, each table has a lot of duplication, will cause the space utilization is not high. So MFC's message mapping mechanism does not use virtual functions, but the second way to achieve polymorphism, that is:
(2), according to the Function name look-up table, this approach avoids problems like this, but the time efficiency performance is not high because you want to compare names and sometimes iterate through all the inheritance structures. (For the implementation of the message map of MFC, see the next article)
3. Summary:
The virtual function mechanism of C + + is better if the virtual members of the base class of the inheritance system are few and most of the parts to be overridden by the derived class, but if there are many virtual members of the base class of the inheritance system, or if the inheritance system is quite large, And there are fewer parts that need to be rewritten in a derived class, so it's more efficient to look up tables with names, such as MFC,QT
PS in fact, since the advent of the computer, time and space has become the eternal theme, because the two in 98% of the situation can not be coordinated, this long and then disappear, this is the fundamental bottleneck in computer science. Software science and the development of algorithms, it depends on whether to break the balance of time and space. Oh
More than computer science, so is the whole universe. The most basic mystery of the universe, or time and space ~
Postscript:
Have the following code:
#include <iostream>
using namespace std;
Class a{public
:
//Constructor
A (): A (2) {
}
virtual void F () {cout<< "This is
Base f ()" << Endl;
}
void G () {
cout<< "This is Base g ()" <<endl;
}
int A;
};
Class B:public A
{public
:
B (): B (3) {}
void F () {
cout<< ' This is Child f () ' <<endl ;
}
void G () {
cout<< "this are Child g ()" <<endl;
}
int b;
};
int main (int argc, char const *argv[])
{
B;
A *a = &b;
A->f ();
A->g ();
cout<< "Number A is:" <<a->a<<endl;
cout<< "Number B is:" <<b.b<<endl;
return 0;
}
The results are:
This is the child F () is
Base g () number
a is:2 number
b is:3
It can be seen in the use of polymorphism: a *a = &b. If a function in a is a virtual function, then the function can be overwritten, and if the function is not a virtual function, then the function will not be overwritten.