8.5 C++ 继承与多态

简介: C/C++语言是一种通用的编程语言,具有高效、灵活和可移植等特点。C语言主要用于系统编程,如操作系统、编译器、数据库等;C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统、图形用户界面、嵌入式系统等。C/C++语言具有很高的效率和控制能力,但也需要开发人员自行管理内存等底层资源,对于初学者来说可能会有一定的难度。

C/C++语言是一种通用的编程语言,具有高效、灵活和可移植等特点。C语言主要用于系统编程,如操作系统、编译器、数据库等;C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统、图形用户界面、嵌入式系统等。C/C++语言具有很高的效率和控制能力,但也需要开发人员自行管理内存等底层资源,对于初学者来说可能会有一定的难度。

继承的基本语法: 继承的目的就是用于提高代码的复用性,减少重复代码的开发,提高工作效率,先来定义简单的继承关系.

#include <iostream>

using namespace std;

class BasePage
{
   
private:
  int m_x;

protected:
  int m_y;

public:
  void header() {
    cout << "header" << endl; }
  void footer() {
    cout << "footer" << endl; }
};

// 新建一个News类,以公有属性继承BasePage类中的内容
class News :public BasePage
{
   
public:
  void connect() {
    cout << "connect" << endl; }
};

int main(int argc, char *argv[])
{
   
  News news;
  news.header();
  news.footer();

  system("pause");
  return 0;
}

继承中的构造/析构函数: 继承后的构造顺序base start -> news start析构顺序news end -> base end.

#include <iostream>

using namespace std;

class BasePage
{
   
public:
  BasePage(){
    cout << "base page -> start" << endl; }
  ~BasePage(){
    cout << "base page -> end" << endl; }
};

class News :public BasePage
{
   
public:
  News(){
    cout << "news page -> start" << endl; }
  ~News(){
    cout << "news page -> end" << endl; }
};

void call()
{
     // 此处如果初始化写在main中,将无法看到析构函数的执行
  News news;
}

int main(int argc, char *argv[])
{
   
  call();
  system("pause");
  return 0;
}

继承下的同名函数调用: 在继承多个类的时候,难免会出现同名函数的冲突问题,在调用是可以指明其作用域.

#include <iostream>

using namespace std;

class BasePage
{
   
public:
  void func(int x, int y)
  {
   
    cout << "BasePage -> " << x + y << endl;
  }
};

class News :public BasePage
{
   
public:
  void func(int x, int y)
  {
   
    cout << "News -> " << x + y << endl;
  }
};

int main(int argc, char *argv[])
{
   
  News news;

  // 如果想调用父类的func()则需要
  news.BasePage::func(10,20);

  // 调用子类的则是
  news.func(100, 200);

  system("pause");
  return 0;
}

继承下的静态成员属性: 静态成员属性也可以直接通过作用域方式直接调用.

#include <iostream>

using namespace std;

class BasePage
{
   
public:
  static int m_x;

public:
  static void func(int x, int y) {
   cout << "BasePage -> " << x + y << endl;}
};

class News :public BasePage
{
   
public:
  static int m_x;

public:
  static void func(int x, int y) {
   cout << "News -> " << x + y << endl;}
};

int BasePage::m_x = 100;
int News::m_x = 200;

int main(int argc, char *argv[])
{
   
  News news;

  cout << News::m_x << endl;
  cout << News::BasePage::m_x << endl;

  News::func(10,20);
  News::BasePage::func(100,200);

  system("pause");
  return 0;
}

多继承的调用: 定义Super派生类,分别继承Base1Base2这两个基类.

#include <iostream>

using namespace std;

class Base1
{
   
public:
  int m_x;
public:
  Base1(){
    m_x = 10; }
  void show() {
    cout << m_x << endl; }
};

class Base2
{
   
public:
  int m_x;
public:
  Base2(){
    m_x = 20; }
  void show() {
    cout << m_x << endl; }
};

class Super :public Base1, public Base2
{
   
public:
  int m_y;
public:
  Super(){
    m_y = 30; }
  void display(){
    cout << m_y << endl; }
};

int main(int argc, char *argv[])
{
   
  Super super;

  cout << super.Base1::m_x << endl;
  cout << super.Base2::m_x << endl;
  cout << super.m_y << endl;

  super.Base1::show();
  super.display();

  system("pause");
  return 0;
}

菱形继承(虚继承): 虚继承可能会出现多个基类中存在相同的成员数据,从而造成内存开销过大,虚基类即可解决开销问题.

#include <iostream>

using namespace std;

class Animal
{
   
public:
  int m_Age;
};

// 虚基类 Sheep
class Sheep :virtual public Animal {
    };

// 虚基类 Tuo
class Tuo :virtual public Animal {
    };

// 定义派生类
class SheepTuo :public Sheep, public Tuo {
    };

int main(int argc, char *argv[])
{
   
  SheepTuo ptr;

  ptr.Sheep::m_Age = 10;
  ptr.Tuo::m_Age = 20;

  cout << ptr.Sheep::m_Age << endl;
  cout << ptr.Tuo::m_Age << endl;
  cout << ptr.m_Age << endl;

  system("pause");
  return 0;
}

虚函数实现动态多态: 动态多态的作用是,通过传递不同的类指针,实现调用不同的方法,同一个结构返回不同的状态.

#include<iostream>

using namespace std;

class Animal
{
   
public:
  virtual void speak() {
    cout << "Animal speak" << endl; }
  virtual void eat() {
    cout << "Animal eat" << endl; }
};

class Cat :public Animal
{
   
public:
  void speak() {
    cout << "Cat speak" << endl; }
  virtual void eat() {
    cout << "cat eat" << endl; }
};

class Dog :public Animal
{
   
public:
  void speak() {
    cout << "Dog speak" << endl; }
  virtual void eat() {
    cout << "dog eat" << endl; }
};

void doSpeak(Animal & animal)
{
   
  animal.speak();
}

int main(int argc, char *argv[])
{
   
  Cat cat_ptr;
  doSpeak(cat_ptr);    // 调用同一个函数,返回不同的结果

  Dog dog_ptr;
  doSpeak(dog_ptr);

  system("pause");
  return 0;
}

纯虚函数实现多态: 如果父类中存在纯虚函数,子类继承父类后就必须要实现纯虚函数,父类中存在纯虚函数,该类无法实例化对象,只能被继承后实现,这种类也被叫做抽象类.

#include <iostream>

using namespace std;

class AbstractCalculator
{
   
public:
  int x;
  int y;

public:
  // virtual int getResult() { return 0; };    // 定义虚函数
  virtual int getResult() = 0;                 // 定义纯虚函数
  void Set_Val(int x, int y){
    this->x = x; this->y = y; };
};

class AddCalculator :public AbstractCalculator
{
   
public:
  virtual int getResult()   // 扩展getResult方法
  {
   
    return x + y;
  }
};

class SubCalculator :public AbstractCalculator
{
   
public:
  virtual int getResult()
  {
   
    return x - y;
  }
};

int main(int argc, char *argv[])
{
   
  AbstractCalculator *ptr = new AddCalculator;   // 创建一个加法
  ptr->Set_Val(10, 20);
  cout << "x+y = " << ptr->getResult() << endl;
  delete ptr;

  ptr = new SubCalculator;   // 创建一个减法
  ptr->Set_Val(10, 20);
  cout << "x-y = " << ptr->getResult() << endl;
  delete ptr;

  system("pause");
  return 0;
}

实现虚析构函数: 在基类的析构函数上加上virtual将其修饰为虚析构函数,其解决的问题是父类指针释放子类对象时,无法释放掉子类对象中的数据,或者说无法调用到子类对象的析构函数,我们可以利用虚析构来解决这个问题.

#include <iostream>

using namespace std;

class Animal
{
   
public:
  // 此处加上virtual声明为虚析构函数
  virtual ~Animal() {
    cout << "Animal back" << endl; }
};

class Cat :public Animal
{
   
public:
  char *m_name;

public:
  Cat(const char *name)
  {
   
    this->m_name = new char[strlen(name) + 1];
    strcpy(this->m_name, name);
  }
  ~Cat()
  {
   
    cout << "Cat back" << endl;
    if (this->m_name != NULL)
    {
   
      delete[] this->m_name;
      this->m_name = NULL;
    }
  }
};

int main(int argc, char *argv[])
{
   
  Animal *ptr = new Cat("Tomcat");
  delete ptr;
  system("pause");
  return 0;
}

实现纯虚析构函数: 如果函数中出现了纯虚析构函数,那么这个类也算抽象类,不可实例化对象,纯虚析构函数必须要存在实现,并且实现代码必须在类外,因为类内被定义为了纯虚析构函数,所以只能在类外部完成了.

#include <iostream>

using namespace std;

class Animal
{
   
public:
  // 此处加上 virtual 声明为纯虚析构函数
  virtual ~Animal() = 0 ;
};

Animal::~Animal()
{
   
  // 纯虚析构函数实现
  cout << "Animal back" << endl;
}

class Cat :public Animal
{
   
public:
  char *m_name;

public:
  Cat(const char *name)
  {
   
    this->m_name = new char[strlen(name) + 1];
    strcpy(this->m_name, name);
  }
  ~Cat()
  {
   
    cout << "Cat back" << endl;
    if (this->m_name != NULL)
    {
   
      delete[] this->m_name;
      this->m_name = NULL;
    }
  }
};

int main(int argc, char *argv[])
{
   
  Animal *ptr = new Cat("Tomcat");
  delete ptr;
  system("pause");
  return 0;
}

本文作者: 王瑞
本文链接: https://www.lyshark.com/post/5652995a.html
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

目录
相关文章
|
4天前
|
C++
【C++】从零开始认识多态(二)
面向对象技术(oop)的核心思想就是封装,继承和多态。通过之前的学习,我们了解了什么是封装,什么是继承。 封装就是对将一些属性装载到一个类对象中,不受外界的影响,比如:洗衣机就是对洗衣服功能,甩干功能,漂洗功能等的封装,其功能不会受到外界的微波炉影响。 继承就是可以将类对象进行继承,派生类会继承基类的功能与属性,类似父与子的关系。比如水果和苹果,苹果就有水果的特性。
24 1
|
4天前
|
C++
【C++】从零开始认识多态(一)
面向对象技术(oop)的核心思想就是封装,继承和多态。通过之前的学习,我们了解了什么是封装,什么是继承。 封装就是对将一些属性装载到一个类对象中,不受外界的影响,比如:洗衣机就是对洗衣服功能,甩干功能,漂洗功能等的封装,其功能不会受到外界的微波炉影响。 继承就是可以将类对象进行继承,派生类会继承基类的功能与属性,类似父与子的关系。比如水果和苹果,苹果就有水果的特性。
26 4
|
4天前
|
安全 前端开发 Java
【C++】从零开始认识继承二)
在我们日常的编程中,继承的应用场景有很多。它可以帮助我们节省大量的时间和精力,避免重复造轮子的尴尬。同时,它也让我们的代码更加模块化,易于维护和扩展。可以说,继承技术是C++的灵魂。
14 1
|
4天前
|
安全 程序员 编译器
【C++】从零开始认识继承(一)
在我们日常的编程中,继承的应用场景有很多。它可以帮助我们节省大量的时间和精力,避免重复造轮子的尴尬。同时,它也让我们的代码更加模块化,易于维护和扩展。可以说,继承技术是C++的灵魂。
24 3
【C++】从零开始认识继承(一)
|
4天前
|
C++ 编译器 存储
|
4天前
|
存储 C++
C++中的多态
C++中的多态
8 0
|
4天前
|
存储 编译器 C++
C++中的继承
C++中的继承
11 0
|
4天前
|
设计模式 算法 编译器
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
13 0
|
4天前
|
安全 程序员 编译器
【C++】继承(定义、菱形继承、虚拟继承)
【C++】继承(定义、菱形继承、虚拟继承)
15 1
|
4天前
|
存储 编译器 C++
[C++基础]-多态
[C++基础]-多态