current position:Home>47 - Conflict between father and son

47 - Conflict between father and son

2022-11-24 22:39:57uwu16

---- 整理自狄泰软件唐佐林老师课程

1. 思考

子类中是否可以定义Members of the same name in the parent class?
如果可以,如何区分?如果不可以,why?

1.1 编程实验:同名成员变量

#include <iostream>
#include <string>

using namespace std;

class Parent
{
    
public:
    int mi;
};

class Child : public Parent
{
    
public:
    int mi;
};

int main()
{
    
    Child c;
    
    c.mi = 100;    // mi Whether a subclass custom,Or class have inherited from the father?
    
    return 0;
}

在这里插入图片描述

1.2 父子间的冲突

  • 子类可以定义Members of the same name in the parent class
  • 子类中的成员将隐藏Members of the same name in the parent class(同名覆盖
  • Members of the same name in the parent class Still exists in the subclass
  • 通过 作用域分辨符( : : 访问Members of the same name in the parent class

1.2.1 访问Members of the same name in the parent class

在这里插入图片描述

1.2.2 编程实验:Member variables with the same depth analysis

#include <iostream>
#include <string>

using namespace std;

namespace A
{
    
    int g_i = 0;
}

namespace B
{
    
    int g_i = 1;
}

class Parent
{
    
public:
    int mi;
    
    Parent()
    {
    
        cout << "Parent() : " << "&mi = " << &mi << endl;
    }
};

class Child : public Parent
{
    
public:
    int mi;
    
    Child()
    {
    
        cout << "Child() : " << "&mi = " << &mi << endl;
    }
};

int main()
{
    
    Child c;
    
    c.mi = 100;    
    
    c.Parent::mi = 1000;
    
    cout << "&c.mi = " << &c.mi << endl;
    cout << "c.mi = " << c.mi << endl;
    
    cout << "&c.Parent::mi = " << &c.Parent::mi << endl;
    cout << "c.Parent::mi = " << c.Parent::mi << endl;
    
    return 0;
}

在这里插入图片描述

2. 再论重载

  • 类中的成员函数可以进行重载
    • 重载函数的本质为多个不同的函数
    • 函数名和参数列表是唯一的标识
    • 函数重载必须发生在同一个作用域中

2.1 问题

Subclasses can override the parent class defined in the function of the function of the same name?

2.2 编程实验:父子间的函数重载

#include <iostream>
#include <string>

using namespace std;

class Parent
{
    
public:
    int mi;
    
    void add(int v)
    {
    
        mi += v;
    }
    
    void add(int a, int b)
    {
    
        mi += (a + b);
    }
};

class Child : public Parent
{
    
public:
    int mi;
    
    void add(int v)
    {
    
        mi += v;
    }
    
    void add(int a, int b)
    {
    
        mi += (a + b);
    }
    
    void add(int x, int y, int z)
    {
    
        mi += (x + y + z);
    }
};

int main()
{
    
    Child c;
    
    c.mi = 100;    
    c.Parent::mi = 1000;
    
    cout << "c.mi = " << c.mi << endl;
    cout << "c.Parent::mi = " << c.Parent::mi << endl;
    
    c.add(1);
    c.add(2, 3);
    c.add(4, 5, 6);
    
    cout << "c.mi = " << c.mi << endl;
    cout << "c.Parent::mi = " << c.Parent::mi << endl;
    
    return 0;
}

在这里插入图片描述
在这里插入图片描述

2.3 实验结果

  • 子类中的函数将隐藏父类的同名函数(同名覆盖)
  • 子类无法重载父类中的成员函数(Function overloading must occur in the same scope)
  • Using the scope resolution operator to access the parent class in the function of the same name
  • 子类可以定义父类中完全相同的成员函数

3. 小结

  • 子类可以定义Members of the same name in the parent class
  • 子类中的成员将隐藏Members of the same name in the parent class
  • 子类和父类中的函数不能构成重载关系
  • 子类可以定义父类中完全相同的成员函数
  • Using the scope resolution operator to access the parent class of the members of the same name

copyright notice
author[uwu16],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/328/202211242224142205.html

Random recommended