C + + daily question: what is object-oriented? Three characteristics of object oriented

1, What is process oriented?

Process oriented programming is a process centered programming idea. Its principle is to decompose the problem into detailed steps, and then realize each step through functions and call them in turn.
Process oriented, we are concerned about the steps to solve a problem. For example, car start and car stop are two different events. For process oriented, we are concerned about the event itself. Therefore, we will use two functions to complete the above two actions, and then call them in turn.

2, What is object oriented?

Object oriented programming is an object-centered programming idea, which is to analyze problems, decompose objects one by one, and then combine them through calls between different objects to solve problems. The purpose of establishing objects is not to complete a step, but to describe the behavior of something in the process of solving the whole problem.
As in the above example, the car starts and the car stops. For object-oriented, we are concerned about objects such as cars. The two events are only the behavior of such objects.

3, What are the three basic characteristics of object-oriented?

1. Encapsulation

Encapsulation is to hide some information of a class inside the class and not allow direct access from the outside. Instead, it provides some methods to access and operate the hidden information.
The advantage of encapsulation is to enhance data security and hide the implementation details of the class. Users do not need to understand the specific implementation details, but only need to access through a specific interface, which is also convenient for the implementation and modification of the class itself.

2. Inherit

Inheritance is a relationship between classes, that is, a subclass inherits the characteristics and behavior of the parent class, so that the subclass has the same properties and behavior as the parent class.
The advantage of inheritance is that the subclass inherits the properties and methods of the parent class, so as to realize the reuse of code.

3. Polymorphism

Polymorphism means that the same method of a class object has different forms in different situations.
Polymorphism enables objects with different internal structures to share the same external interface.

4. Differences between C + + overload rewriting and polymorphism

4.1 difference between overloading and rewriting

overload: in the same class, the function names are the same and the parameter lists are different. The compiler will modify the function names with the same name according to the different parameter lists of these functions, so as to generate some preprocessing functions with different names, which does not reflect polymorphism.

Override: also called override. Subclasses redefine virtual functions with the same name and parameters in the parent class, which mainly occurs in the inheritance relationship. The rewritten function must be virtual, and the access modifiers of the rewritten function can be different. Although virtual is private, the rewritten function in subclasses can be changed to public and protected, which reflects polymorphism.

redefining: also called hiding. Subclasses redefine non virtual functions with the same name in the parent class. The parameter list can be the same or different. It will overwrite the methods of its parent class and does not reflect polymorphism.

1) a if the function of the derived class has the same name as the function of the base class, but the parameters are different, at this time, the function of the base class is hidden whether there is virtual or not.
2) b if the function of the derived class has the same name as the function of the base class and the parameters are the same, but the base class function does not have the visual keyword, the function of the base class is hidden. (if there is virtual, it will be rewritten)

#include <iostream>  
using namespace std;  
class Base  
{  
private:  
    virtual void display() { cout<<"Base display()"<<endl; }  
    void show(){ cout<<"Base show()"<<endl; }  
public:  
    void exec(){ display(); show(); }  
    void fun(string s) { cout<<"Base fun(string)"<<endl; }  
    void fun(int a) { cout<<"Base fun(int)"<<endl; }//overload: two fun functions are overloaded inside the Base class  
    virtual int function(){}  
};  
class ClassA:public Base  
{  
public:  
    void display() { cout<<"ClassA display()"<<endl; }//override: display in the base class is a virtual function, and the parameter list is always, so it is overridden here  
    void fun(int a,int b) { cout<<"ClassA fun(int,int)"<<endl; }//Redefining: the fun function is not a virtual function in the Base class, so it is redefined here  
    void show() { cout<<"ClassA show()"<<endl; }//redefining: the reason is the same as above  
    int function(int a){}//Overload: note that this is an overload rather than an override, because the parameter list is different. At compile time, ClassA actually has an int function() {} inherited from Base secretly added by the compiler itself;  
};  
int main(){  
    ClassA a;  
    Base *base=&a;  
    base->exec();//display() is ClassA's because it is overridden. show() is Base's own  
    a.exec();//The results are the same as above  
    a.show();//show() is redefined by ClassA  
    base->fun(1);//fun() is base's own because it is called directly from the object base  
    a.fun(1, 1);//fun() is redefined by ClassA  
    return 0;  
}  
4.2 polymorphism

"One interface, multiple methods", the program only determines the function to be called at run time, and the polymorphism of C + + is realized through virtual functions. The most common usage is to declare the pointer of the base class, use the pointer to point to any subclass object, call the corresponding virtual function, and implement different methods according to the subclasses pointed to. If the virtual function is not used, that is, C + + polymorphism is not used, when calling the corresponding function using the base class pointer, it will always be limited to the base class function itself and cannot call the rewritten function in the subclass. Because there is no polymorphism, the address of function call will be certain, and the fixed address will always call the same function, which can not achieve the purpose of one interface and multiple methods.

a compile time polymorphism: implemented through overloaded functions.
b runtime polymorphism: realized by virtual functions.

Keywords: OOP

Added by receiver on Mon, 01 Nov 2021 03:02:46 +0200