zoukankan      html  css  js  c++  java
  • 【C++】三大概念要分清--重载,隐藏(重定义,覆盖(重写)

    c++三大概念要分清--重载,隐藏(重定义),覆盖(重写)}

    重载

    •  概念:在同一个作用域内;函数名相同,参数列表不同(参数个数不同,或者参数类型不同,或者参数个数和参数类型都不同),返回值类型可相同也可不同;这种情况叫做c++的重载!

      注意:c语言没有函数重载的机制

    •  举例代码: 

     1 #include<iostream>
     2 using namespace std;
     3 
     4 int Add(int a,int b)
     5 {
     6     return a+b;
     7 }
     8 
     9 float Add(float a,float b)
    10 {
    11     return a+b;
    12 }
    13 
    14 int main()
    15 {
    16     cout<<Add(4,5)<<endl;       // 调用 int Add(int a,int b)
    17     cout<<Add(2.5f,3.7f)<<endl; // 调用 float Add(float a,float b)
    18     return 0;
    19 }

    此时,两个函数Add();在同一作用域,函数名相同都是Add,参数类型不同;就构成了c++中的函数重载;

    • c++函数重载达到的效果:调用函数名相同的函数,会根据实参的类型和实参顺序以及实参个数选择相应的函数; 

    • c++函数重载是一种静态多态(又叫做静态联编,静态绑定,静态决议)

    覆盖(又叫重写)

    • 覆盖(重写)的前提条件:父类函数为虚函数;

    • 覆盖(重写)的概念:当在子类中定义了一个与父类完全相同的虚函数时,则称子类的这个函数重写(也称覆盖)了父类的这个虚函数。

    • 什么是在子类中定义了一个与父类完全相同的虚函数:

    有两种情况:

    1. 就是说子类中的虚函数和父类中的虚函数,函数名,参数个数,参数类型,返回值类型都相同;这种情况下子类的这个虚函数重写的父类中的虚函数,构成了重写;

    2. 协变—是说子类中的虚函数和父类中的虚函数,函数名,参数个数,参数类型都相同,只是返回值类型不同;父类的虚函数返回父类的指针或者引用,子类虚函数返回子类的指针或者引用;这种情况下子类的这个虚函数也重写了父类中的虚函数,也构成了重写;——我们把这种特殊的情况叫做协变

    【注意】:在基类中定义了一个与派生类虚函数完全相同的函数,那么这个派生类的函数就是重写了基类的虚函数,此时这个派生类的函数就是虚函数,如果不显示的加上virtual修饰,编译器也会默认为虚函数;

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Person
     5 {
     6 public:
     7     virtual void BuyTickets()
     8     {
     9         cout << " 买票-全票" << endl;
    10     }
    11 protected:
    12     string  _name; // 姓名
    13 };
    14 
    15 class Student : public Person
    16 {
    17 public:
    18     void BuyTickets()
    19     {
    20         cout << " 买票-半价" << endl;
    21     }
    22 protected:
    23     int _num; //学号
    24 };
    25 
    26 void Fun(Person* p)
    27 {
    28     p->BuyTickets();
    29 }
    30 
    31 void Fun(Person &p)
    32 {
    33     p.BuyTickets();
    34 }
    35 
    36 void Test()
    37 {
    38     Person p;
    39     Student s;
    40     Fun(p);
    41     Fun(s);
    42 
    43     Fun(&p);
    44     Fun(&s);
    45 }
    46 
    47 int main()
    48 {
    49     Test();
    50     system("pause");
    51     return 0;
    52 }

    输出结果:

    举例2:

     1 #include<iostream>
     2 using namespace std;
     3 class Person
     4 {
     5 public:
     6     virtual  Person& BuyTickets()//基类虚函数
     7     {
     8         cout << "成人买票-全票" << endl;
     9         return *this;
    10     }
    11 public:
    12     string  _name; // 姓名
    13 };
    14 
    15 class Student : public Person
    16 {
    17 public:
    18     virtual Student& BuyTickets()//派生类虚函数
    19     {
    20         cout << "学生买票-半票" << endl;
    21         return *this;
    22     }
    23 public:
    24     int _num; //学号
    25 };
    26 
    27 void Fun(Person* p)
    28 {
    29     p->BuyTickets();
    30 }
    31 
    32 void Fun(Person &p)
    33 {
    34     p.BuyTickets();
    35 }
    36 void Test()
    37 {
    38     Person p;
    39     Student s;
    40     Fun(p);
    41     Fun(s);
    42 
    43     Fun(&p);
    44     Fun(&s);
    45 }
    46 int main()
    47 {
    48     Test();
    49     system("pause");
    50     return 0;
    51 }

    输出结果:

     

    参考资料

    1. c++三大概念要分清--重载,隐藏(重定义),覆盖(重写)

     

  • 相关阅读:
    马哥博客作业第十六周
    马哥博客作业第十五周
    马哥博客作业第十四周
    马哥博客作业第十三周
    回调函数
    spring cloud
    jt业务实现
    dubbo框架介绍
    SOA思想
    linux设置防火墙规则-指定ip的访问权限
  • 原文地址:https://www.cnblogs.com/sunbines/p/9130227.html
Copyright © 2011-2022 走看看