3. Polymorphism and function overloading in C++

1. Polymorphism in C++

insert image description here

  • Advantages: Polymorphism is one of the important features of object-oriented programming, which enables an interface to implement different functions. Polymorphism can be divided into static polymorphism and dynamic polymorphism.

Static polymorphism : Static polymorphism is achieved through function overloading, yesat compile timeDetermines the function to call based on the number and types of parameters. C++ function overloading is a kind of static polymorphism.

Dynamic polymorphism : Dynamic polymorphism is implemented through virtual functions and inheritance, isat runtimeThe function to call is determined according to the actual object type.

2. Function overloading in C++

  • 1. Use the same function name to define different functions. function overloading
  • 2. Overloaded functions are distinguished according to the number and type of parameters
  • 3、cannot be distinguished based on the return value

The principle of C++ function overloading

  • Function overloading is a mechanism that allows multiple functions to share a name but perform different functions depending on the number and types of parameters.
  • 1. The C++ compiler will "automatically detect" the functions written by the user when compiling, and the "parameter list" will alias the function name according to the difference in the parameter list to generate multiple functions with different names.
  • 2. According to the "parameter" passed in by the user, the type is automatically deduced to determine the function to be executed

Exercise: Write an add function, which can realize the addition of two integers and the addition of two floating-point types

#include <iostream>
using namespace std;

void add(int a, int b)
{
    
    
    cout << "int" << a + b << endl;
}

void add(double a, double b)
{
    
    
    cout << "float" << a + b << endl;
}

int main()
{
    
    
    add(10, 20);//根据传入的参数不同,自动推导调用的函数 pf_int_int(int a, int b);

    add(1.1, 2.2);//根据传入的参数不同,自动推导调用的函数 pf_double_double(double a, double b);
}
//都是c++ 编译器自动去完成的!!不需要用户干预!!

3. Precautions for function overloading

  • 1. The function name must be the same when the function is overloaded
  • 2. The basis of function overloading <parameter number> <parameter type> is different
  • 3、The return type cannot be used as a basis for overloading(Function overloading requires the same function name, but it is distinguished according to the number and type of parameters. The return value type does not affect function overloading.)
  • 4. When calling a function, it is necessary to prevent overloading the functionAmbiguityparameters

insert image description here

Fourth, the default parameters of the overloaded function

1. Default parameters: When defining a function in C++, you can use default parameters to assign formal parameters

#include <iostream>
using namespace std;

// 定义一个带有默认参数的函数
void func(int a = 100)
{
    
    
	cout << a << endl;
}

int main()
{
    
    
	func(); // 调用函数时不传递参数,使用默认参数
}

Function of default parameters: Simplify function calls! !

2. The order of filling in the default parameters

insert image description here
Hint: When calling the function, omitted arguments are taken withright to left orderFill in the default values.

fp(10, 20, 30) 10 is passed to a, 20 is passed to b, 30 is passed to c

5. Example of using default parameters to simplify function calls

#include <iostream>
using namespace std;

extern "C"
{
    
    
    #include <stdio.h>

}

//映射一个设备 , 利用默认参数封装mmap 是他的调用更加简单
void* my_mmap(int fd,size_t length,
              intprot=PROT_READ|PROT_WRITE,intflags=MAP_SHARED,
              void *addr=NULL,off_toffset=0)
{
    
    
    return mmap(addr,length,prot,flags,fd,offset);
}

int main()
{
    
    
    //打开一个文件
    int fd = open("1.txt", O_RDWR);
    if(fd < 0)
    {
    
    
        cerr << "打开文件失败" << endl;
        return 0;
    }
    //映射文件
    void *p = my_mmap(fd, 1024);

    //只读权限
    void *p1 = my_mmap(fd, 1024, PROT_READ);

    //修改权限
    void *p2 = my_mmap(fd, 1024, PROT_READ, MAP_SHARED_VALIDATE);
}

Guess you like

Origin blog.csdn.net/qq_53402930/article/details/132394821