The "C + + Institute" (10) overloaded with rewrite/polymorphic in-depth/multiple inheritance interfaces

Source: Internet
Author: User
Tags prototype definition

1. Override PK Overload understanding

1.1 function Overloading
Must be in the same class
A subclass cannot overload a function of the parent class, and a function with the same name will be overwritten
Overloading is the function call that is determined by the type and number of arguments during compilation
1.2 Function rewriting
Must occur between the parent class and the child class
and the parent class must have exactly the same prototype as the function in the child class.
Can generate polymorphism after using the virtual declaration (if you don't use virtual, it's called redefinition)
Polymorphism is the function call that is determined by the type of the specific object during the run
#include <cstdlib> #include <iostream>using namespace std;//overload is placed in only one class, and the class Parent01{public is determined during compilation: Parent01 () {cout << "parent01:printf (): Do "<< Endl;} Public:void aaaaa () {;} void Func () {cout << "parent01:void func ()" << Endl;} void func (int i) {cout << "parent:void func (int i)" << Endl;} virtual void func (int i, int j) {cout << "parent:void func (int i, int j)" << endl;}};/ /rewrite between parent and child classes, functions three elements (function name, function parameter, function return type) exactly the same//rewrite is divided into two//if there is a virtual keyword in the parent class, the relationship between parent and child is called virtual function rewrite, in which case polymorphism (dynamic chain delay binding)// If there is no virtual keyword in the parent class, this relationship between parent and child is redefined (static chaining) class Child01:public parent01{public://here 2 parameters, and the parent class Func function is what the relationship "polymorphic" void func (int I, Int j) {cout << "child:void func (int i, int j)" << "" << i + J << Endl;} I want to overload the Func function of the parent class in the subclass = = = "C.parent01::func ();//Here are 3 parameters, and what is the relationship between the parent class Func function" Just the function name is the same "void func (int i, int j, int k) {cout &lt ;< "child:void func (int i, int j, int k)" << "" << i + j + k << Endl;}; void Run01 (Parent01* p) {P-&GT;func (1, 2);} int main () {Parent01 p;p.func ();p. Func (1);p. Func (1, 2); CHILD01 C;//c.func (); Note 1:error C2661: "Child01::func": no overloaded function accepts 0 parameter C. Parent01::func (); c.aaaaa ();//c.func (1, 2); Run01 (&p); Run01 (&c); System ("pause"); return 0;} Question 1:child object inherits the Func of the parent class object, can I ask this sentence to run? Why//c.func (); 1 The Func inside the subclass cannot overload the Func//2 in the parent class when the parent class and subclass have the same function name, variable name, occurrence name overwrite//3//c.parent::func ();///2 What is the relationship between the two func of the subclass and three Func functions in the parent class?

2.would you like to talk about your understanding of polymorphism?

Multi-State implementation effect
Polymorphism: The same invocation statement has many different forms of expression;
Three conditions for polymorphic implementations
There are inheritance, virtual overrides, and a parent pointer (reference) to the child class object.
Multi-State C + + implementation
The virtual keyword tells the compiler that this function needs to support polymorphism; Do not judge how to call based on the pointer type, but to determine how to invoke the actual object type that the pointer is pointing to.
The theoretical basis of polymorphism
Dynamic-linked PK Static-linked series. The invocation of an overriding function is judged based on the actual object type.
The significance of polymorphism
The basis of the design pattern.
Theoretical basis for the realization of polymorphism
function pointers do function arguments

3.the implementation of the polymorphic principle?

When a virtual function is declared in a class, the compiler generates a virtual function table in the class
A virtual function table is a data structure that stores a pointer to a class member function
Virtual function tables are automatically generated and maintained by the compiler.
The virtual member function is placed in the virtual function table by the compiler
When a virtual function exists, each object has a pointer to the virtual function table (vptr pointer)
Illustration 1:
Calling an overriding function through the virtual function table pointer vptr is done when the program runs, so you need to address it to determine which function is really supposed to be called. The normal member function is a function that determines the call at compile time. In efficiency, the efficiency of virtual function is much lower.
Illustration 2:
For efficiency reasons, it is not necessary to declare all member functions as virtual functions

4. Can a virtual function be called in a constructor to achieve polymorphism? Why

The constructor calls the Polymorphic function and cannot implement polymorphism.
The vptr pointer is initialized by the compiler when the object is created.
Only when the object's construction is completely finished will the VPTR's point be finalized.
Parent-Class object Vptr point to parent virtual function table
The vptr of the subclass object points to the subclass virtual function table

1. When sprints points to the constructor of the parent class, the C + + compiler initializes the vptr pointer to the subclass, letting the vptr pointer point to the virtual function table of the parent class. If a virtual function is called inside the constructor of the parent class, the function of the parent class is called. There is no polymorphism at this time. 2. When the constructor of the parent class executes, and then executes the constructor of the subclass, this time, let the vptr pointer really point to the virtual function table of the subclass. Polymorphism is generated at this time. 5. Multi-Inheritance interface

No interface concept in C + +
Interfaces can be implemented using pure virtual functions in C + +
Only function prototypes are defined in the interface class, and no data is defined.

#include "iostream" using namespace the concept of no interface in C + + can be implemented using pure virtual functions in the interface class only the function prototype definition, no data definition. */class interface1{public:virtual void print () = 0;virtual int Add (int i, int j) = 0;}; Class interface2{public:virtual int Add (int i, int j) = 0;virtual int minus (int i, int j) = 0;}; Class parent{public:int I;}; Class Child:public Parent, public Interface1, public interface2{public:void print () {cout << ' child::p rint ' << ; Endl;} int add (int i, int j) {return i + j;} int minus (int i, int j) {return i-j;}}; int main (int argc, char *argv[]) {child c;c.print (); cout << C.add (3, 5) << endl;cout << C.minus (4, 6) < ;< endl;interface1* i1 = &c;interface2* i2 = &c;cout << i1->add (7, 8) << endl;cout << i2-& Gt;add (7, 8) << Endl;system ("pause");

The "C + + Institute" (10) overloaded with rewrite/polymorphic in-depth/multiple inheritance interfaces

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: 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.