C ++ inheritance, derived, polymorphism

Inheritance is to create a new class based on an existing class on.

Existing class is called the base class , also known as the father ; the establishment of a new class called a derived class , also known as subclasses .

Inheritance allows us to define a class based on another class, without rewriting a portion of the data members and member functions to achieve code reuse functionality and improve the efficiency of the effect.

A base class and derived classes

Class can be derived from a plurality of classes inherit the data and functions from a plurality of base classes. We use a derived class list to specify the base class.

class derived-class: access-specifier base-class

Where access modifier access-specifierShi public, protectedor privateone of the, if not specified default private.

An example of handling

class Shape {
public:
    void setWidth(int w) {
        width = w;
    }
    void setHeight(int h) {
        height = h;
    }
protected:
    int width;
    int height;
};
class Rectangle: public Shape {
public:
    int getArea() {
        return (width * height);
    }
};
int main(void) {
    Rectangle Rect;
    Rect.setWidth(5);
    Rect.setHeight(7);
    cout << "Total area: " << Rect.getArea() << endl;
}

Derived class inherits the properties of the base class inherits the behavior of the base class, you can add additional behavior, you can redefine these behaviors.

If the data members defined in the base class data members of the derived classes and add the same name , the base class data members are covered. Note that even if the same function name, different parameters, can not call the base class function.

At the time of the visit, the type of handle to determine which class members are accessed. Using the object name or reference access member with the same name, determined according to the type of references, pointers to access base class members use the same name, determined according to the type of pointer.

Access control

Derived class can access all non-private members of the base class. A derived class inherits all the methods other than the base class constructor, destructor, overloaded operators, friend function is removed.

Different types of inheritance rules are as follows

  • Public inheritance (public):
    • Base class public member of the derived class public members
    • Base class to protect a member of the derived class protection member
    • Base class private members of the derived class can not be accessed directly, but you can call the base class public and to protect access to members.
  • Protected inheritance (protected):
    • Base class public and protection of the members of the derived class will be to protect members.
  • Private inheritance (private):
    • Base class public and protection of the members of the derived class will become private members.

Friend of the base class can access private members of the base class, as well as members of the derived class inherits from the base class. Derived class friend function can only access the private members of the derived class, but can not access the private members of the base class.

Grade copy constructor for copying or only a derived class to copy the base class, the base class can not be used to copy the derived class.

Virtual and polymorphism

Virtual functions and pure virtual function

Virtual functions defined in order to allow the base class pointer to call this function subclass.

For example, the following procedure is output B

class A {
public:
    virtual void foo() {
        cout<<"A"<<endl;
    }
};
class B:public A {
public:
    void foo() {
        cout<<"B"<<endl;
    }
};
int main() {
    A *a = new B();
    a->foo();
}

Here, the call is not the function of which is determined at compile time, but was determined at run time. In other words, at compile time, not knowing the called function or a function which is derived class base class.

It is defined as a pure virtual function, the function representing not implemented, just to implement an interface. It is not defined in the base class, but requires that any derived class must define your own implementation. Defined way

virtual void func()=0;

Polymorphism

In object-oriented languages, a number of different implementations of the interface is the polymorphism.

Polymorphic class allows the assignment of a pointer to a sub-class type parent pointer type. After this assignment, the parent object can operate in different ways depending on the characteristics of the current assigned to its child objects.

From the application point of view, before the base class by adding virtualthe keyword declared virtual, override this function in a derived class, calling the corresponding function will be based on the actual type of the object at runtime. If the object type is a derived class, the derived class is invoked; if the object type is the base class, the base class is called.

All virtual objects class which will automatically add a hidden pointer to a table in the vtable belong to the class, there is a virtual address of all functions.

Class virtual function with

Each class has a virtual table, and the virtual table can be inherited.

If the subclass does not override the virtual function, then the address sub-class virtual table will still be of the function, but this address points to a base class virtual function to achieve.

If rewriting the corresponding virtual function, then the virtual address in the table will change virtual functions implemented to itself.

If the derived class has its own virtual function, then the virtual table will be added.

Class called polymorphism, i.e. as a function defines the base class virtual function, overridden in a derived class, calling the corresponding function will be based on the actual type of the object at runtime.

Note that area classification polymorphism polymorphism and function. Polymorphism refers to a function of a plurality of function is defined as a function of different parameters, when calling this function, it will call different functions of the same name.

to sum up

Derived class inherits from the base class, the base class can be described as a collection of objects in a set of more detailed, specific objects. Derived class inherits the properties of the base class inherits the behavior of the base class, you can add additional behavior, you can redefine these behaviors.

Virtual function is to allow the pointer to the base class function call subclass. Which function call is not to be determined at compile time, but was determined at run time.

It is defined as a pure virtual function, the function representing not implemented, just to implement an interface. It is not defined in the base class, but requires that any derived class must define your own implementation.

Polymorphic class allows the assignment of a pointer to a sub-class type parent pointer type. After this assignment, the parent object can operate in different ways depending on the characteristics of the current assigned to its child objects.

Guess you like

Origin www.cnblogs.com/mollnn/p/12612713.html