【c++】继承

简介: 【c++】继承

继承的引入:

我们定义person这个类,他包括人的名字,人的性别,人的家庭住址我们如果要定义其他类比如说学生,老师这些类,也会用到person类中的这些变量,为了提高代码的复用性,我们引入了继承.它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。以前我们接触的复用都是函数复用,继承是类设计层次的复用

继承的定义格式

下面我们看到Person是父类,也称作基类。Student是子类,也称作派生类

继承的代码理解

#include<iostream>
using namespace std;
class Person
{
public:
  void Print()
  {
    cout << "name:" << _name << endl;
    cout << "age:" << _age << endl;
  }
protected:
  string _name = "peter"; 
private:
  int _age = 18; 
};
class Student : public Person
{
public:
  void func()
  {
    cout << _name << endl;
    Print();
  }
protected:
  int _stuid; 
};
class Teacher : public Person
{
protected:
  int _jobid; 
};
int main()
{
  Student t;
  
  t.Print();
  t.func();
  return 0;
}


总结1:基类private成员在派生类中无论以什么方式继承都是不可见的。这里的不可见是指基类的私有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管在类里面还是类外面都不能去访问它。

代码演示:

#include<iostream>
using namespace std;
class Person
{
public:
  void Print()
  {
    cout << "name:" << _name << endl;
    cout << "age:" << _age << endl;
  }
protected:
  string _name = "peter"; 
private:
  int _age = 18; 
};
class Student : public Person
{
public:
  void func()
  {
    cout << _name << endl;
    cout << _age << endl;//类内访问基类的private
    Print();
  }
protected:
  int _stuid; 
};
class Teacher : public Person
{
protected:
  int _jobid; 
};
int main()
{
  Student t;
  cout << t._age << endl;//类外访问基类的private
  //t.Print();
  //t.func();
  return 0;
}

但是我们可以通过间接的方式来访问基类的_age,就是基类可以通过在public中定义一个函数来访问自己私有的_age,然后继承方式修改成public,然后再派生类中调用这个函数就可以了

代码如下:

#include<iostream>
using namespace std;
class Person
{
public:
  void Print()
  {
    
    cout << "age:" << _age << endl;
  }
protected:
  string _name = "peter"; 
private:
  int _age = 18; 
};
class Student : public Person
{
public:
  void func()
  {
    
    Print();
  }
protected:
  int _stuid; 
};
int main()
{
  Student t;
  
  t.func();
  return 0;
}


总结2: 基类private成员在派生类中是不能被访问,如果基类成员不想在类外直接被访问,但需要在

派生类中能访问,就定义为protected。可以看出保护成员限定符是因继承才出现的。

上面的代码,由于基类的Print函数是public的,所以在类外面也可以直接使用到这个函数,而Print函数里可以访问到基类的_age,所以上面的代码在类外面也能访问到_age;

#include<iostream>
using namespace std;
class Person
{
public:
  void Print()
  {
    //cout << "name:" << _name << endl;
    cout << "age:" << _age << endl;
  }
protected:
  string _name = "peter"; 
private:
  int _age = 18; 
};
class Student : public Person
{
public:
  void func()
  {
    
    Print();
  }
protected:
  int _stuid; 
};
class Teacher : public Person
{
protected:
  int _jobid; 
};
int main()
{
   Person().Print();//使用匿名对象来调用Print(方便)
  return 0;
}

根据总结2,代码解释:

#include<iostream>
using namespace std;
class Person
{
public:
  void Print()
  {
    
    cout << "age:" << _age << endl;
  }
protected:
  string _name = "peter"; 
    int _age = 18; 
};
class Student : public Person
{
public:
  void func()
  {
    
    cout << _age << endl;
    
  }
protected:
  int _stuid; 
};
int main()
{
  Student t;
  t.func();//类内访问_age;
  cout << Person()._age << endl;//类外访问_age;
  return 0;
}

注释掉main中的t.func()

类外不允许访问基类的protected.

注释掉main函数中的cout << Person()._age << endl;

基类_age访问权限设置为protected,类外访问不了,派生类内可以访问


总结3:继承基类成员访问方式的变化

基类的私有成员在子类都是不可见。基类的其他成员在子类的访问方式 == Min(成员在基类的访问限定符,继承方式),public > protected >private。


总结4:使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public,不过

最好显示的写出继承方式。(class默认访问权限也是private,struct默认访问权限是public)

总结5:在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡

使用protetced/private继承,因为protetced/private继承下来的成员都只能在派生类的类里

面使用,实际中扩展维护性不强


基类和派生类对象赋值转换

我们之前看到过的类型转换,是类似类型可以转化,比如说int,char,unsigned int long等


此外派生类对象 可以赋值给 基类的对象 / 基类的指针 / 基类的引用。这里有个形象的说法叫切片或者切割。寓意把派生类中父类那部分切来赋值过去。

注意派生类赋值给基类不会产生中间隐式变量,中间隐式变量具有常性

继承中的作用域

  1. 在继承体系中基类和派生类都有独立的作用域。
  2. 子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义。(在子类成员函数中,可以使用 基类::基类成员 显示访问)
  3. 需要注意的是如果是成员函数的隐藏,只需要函数名相同就构成隐藏。
  4. 注意在实际中在继承体系里面最好不要定义同名的成员。
class Person
{
protected:
  string _name = "小李子"; // 姓名
  int _num = 111;
};
class Student : public Person
{
public:
  void Print()
  {
    cout << " 姓名:" << _name << endl;
    
    cout << " 学号:" << _num << endl;
  }
protected:
  int _num = 999; // 学号
};
void main()
{
  Student s1;
  s1.Print();
};

在派生类中出现了和基类中相同的变量,由于先在派生类找_num,如果找不到的话,就在基类中找。

如何可以直接访问到基类中的_num,我们之前学过,只要加上他的作用域即可


看一个选择题

class A
{
public:
  void fun()
  {
    cout << "func()" << endl;
  }
};
class B : public A
{
public:
  void fun(int i)
  {
    A::fun();
    cout << "func(int i)->" << i << endl;
  }
};
void main()
{
  B b;
  b.fun(10);
}

上面代码出现的结果是?

A.编译报错 B.形成函数重载 C.构成隐藏

选C

// B中的fun和A中的fun不是构成重载,因为不是在同一作用域

// B中的fun和A中的fun构成隐藏,成员函数满足函数名相同就构成隐藏。


派生类的默认成员函数

1.派生类的构造函数:

在派生类的构造中需要处理的变量有自己的,以及基类的

自己的变量也分为两种,一种是自己的内置类型,不做处理,而自己的自定义类型,去调用该自定义类型的构造函数,基类的话,我们也可以把他当做一个自定义类型,需要调用他的构造函数.

class Person
{
public:
  Person(const char* name)
    : _name(name)
  {
    cout << "Person()" << endl;
  }
protected:
  string _name="sansi"; // 姓名
};
class Student : public Person
{
public:
  
  Student(int num, const char* str, const char* name)
      :Person(name)
      ,_num(num)
      , _str(str)
  { 
    cout << "Student()" << endl;
  }
protected:
  
  int _num; 
  string _str;
};
int main()
{
  
  Student st(123, "xxxxx", "zhangsan");
}

2.派生类的拷贝构造:

同样分为自己的和基类的,如果是自己的,分为内置类型,和自定义类型,内置类型采用值拷贝,自定义类型会去调用他自己的拷贝构造函数来完成初始化,而是基类的变量的话,我们还是把他看做自定义类型,去调用他自己的拷贝构造函数来完成初始化.

class Person
{
public:
  Person(const char* name)
    : _name(name)
  {
    cout << "Person()" << endl;
  }
  Person(const Person& p)
    : _name(p._name)
  {
    cout << "Person(const Person& p)" << endl;
  }
protected:
  string _name; // 姓名
};
class Student : public Person
{
public:
  
  Student(int num, const char* str, const char* name)
      :Person(name)
      ,_num(num)
      , _str(str)
  { 
    cout << "Student()" << endl;
  }
  Student(const Student& s)
    :Person(s)
    , _num(s._num)
    , _str(s._str)
  {}
protected:
  
  int _num; 
  string _str;
};
int main()
{
  
  Student st(123, "xxxxx", "zhangsan");
  Student sk(st);
}

3.派生类的赋值函数

class Person
{
public:
  Person(const char* name)
    : _name(name)
  {
    cout << "Person()" << endl;
  }
  Person(const Person& p)
    : _name(p._name)
  {
    cout << "Person(const Person& p)" << endl;
  }
  Person& operator=(const Person& p)
  {
    cout << "Person operator=(const Person& p)" << endl;
    if (this != &p)
      _name = p._name;
    return *this;
  }
protected:
  string _name; // 姓名
};
class Student : public Person
{
public:
  
  Student(int num, const char* str, const char* name)
      :Person(name)
      ,_num(num)
      , _str(str)
  { 
    cout << "Student()" << endl;
  }
  Student(const Student& s)
    :Person(s)
    , _num(s._num)
    , _str(s._str)
  {}
  Student& operator=(const Student& s)
  {
    if (this != &s)
    {
      Person::operator=(s);
      _num = s._num;
      _str = s._str;
    }
    return *this;
  }
protected:
  
  int _num; 
  string _str;
};
int main()
{
  
  Student st(123, "xxxxx", "zhangsan");
  
}

4.派生类的析构函数

class Person
{
public:
  Person(const char* name)
    : _name(name)
  {
    cout << "Person()" << endl;
  }
  Person(const Person& p)
    : _name(p._name)
  {
    cout << "Person(const Person& p)" << endl;
  }
  Person& operator=(const Person& p)
  {
    cout << "Person operator=(const Person& p)" << endl;
    if (this != &p)
      _name = p._name;
    return *this;
  }
  ~Person()
  {
    cout << "~Person()" << endl;
  }
protected:
  string _name; // 姓名
};
class Student : public Person
{
public:
  
  Student(int num, const char* str, const char* name)
      :Person(name)
      ,_num(num)
      , _str(str)
  { 
    cout << "Student()" << endl;
  }
  Student(const Student& s)
    :Person(s)
    , _num(s._num)
    , _str(s._str)
  {}
  Student& operator=(const Student& s)
  {
    if (this != &s)
    {
      Person::operator=(s);
      _num = s._num;
      _str = s._str;
    }
    return *this;
  }
  ~Student()
  {
    
    
    cout << "~Student()" << endl;
    
  }
protected:
  
  int _num; 
  string _str;
};
int main()
{
  
  Student st(123, "xxxxx", "zhangsan");
  
}

总结:

  1. 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认
    的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用。
  2. 派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。
  3. 派生类的operator=必须要调用基类的operator=完成基类的复制。
  4. 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序。
  5. 派生类对象初始化先调用基类构造再调派生类构造。
  6. 派生类对象析构清理先调用派生类析构再调基类的析构。
  7. 因为后续一些场景析构函数需要构成重写,重写的条件之一是函数名相同,那么编译器会对析构函数名进行特殊处理,处理成destrutor(),所以父类析构函数不加
    virtual的情况下,子类析构函数和父类析构函数构成隐藏关系
目录
相关文章
|
29天前
|
C++
C++中的封装、继承与多态:深入理解与应用
C++中的封装、继承与多态:深入理解与应用
30 1
|
16天前
|
编译器 数据安全/隐私保护 C++
c++primer plus 6 读书笔记 第十三章 类继承
c++primer plus 6 读书笔记 第十三章 类继承
|
4天前
|
C++
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
7 0
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
|
4天前
|
C++
【C++】学习笔记——继承_2
【C++】学习笔记——继承_2
11 1
|
5天前
|
编译器 C++
C++中的继承
C++中的继承
13 1
|
9天前
|
C++
C++一分钟之-继承与多态概念
【6月更文挑战第21天】**C++的继承与多态概述:** - 继承允许类从基类复用代码,增强代码结构和重用性。 - 多态通过虚函数实现,使不同类对象能以同一类型处理。 - 关键点包括访问权限、构造/析构、菱形问题、虚函数与动态绑定。 - 示例代码展示如何创建派生类和调用虚函数。 - 注意构造函数初始化、空指针检查和避免切片问题。 - 应用这些概念能提升程序设计和维护效率。
20 2
|
10天前
|
C++
C++:继承性
C++:继承性
13 2
|
10天前
|
编译器 C++
C++:继承性_程序
C++:继承性_程序
11 1
|
15天前
|
存储 编译器 程序员
【C++高阶】C++继承学习手册:全面解析继承的各个方面
【C++高阶】C++继承学习手册:全面解析继承的各个方面
16 1
|
4天前
|
安全 Java 程序员
【C++航海王:追寻罗杰的编程之路】继承你学会了么?
【C++航海王:追寻罗杰的编程之路】继承你学会了么?
6 0