21xrx.com
2024-09-20 01:13:16 Friday
登录
文章检索 我的文章 写文章
C++的动态多态:增删改查
2023-06-23 05:18:45 深夜i     --     --
C++ 动态多态 增删改查

C++是一种基于对象的编程语言,其面向对象的特性使得它支持动态多态。动态多态是一种允许同一函数在不同情况下表现出不同行为的特性。在C++中,动态多态主要通过函数重载和虚函数实现。本文将介绍如何利用C++的动态多态实现增删改查操作。

增操作

在C++中,增操作可以通过函数重载实现。例如,我们可以定义一个add函数,接受两个整数作为参数,将它们相加并返回结果;也可以定义一个add函数,接受两个字符串作为参数,将它们拼接在一起并返回结果。这样,我们就可以通过调用不同的add函数来实现不同类型的增操作。

void add(int a, int b) {

  int sum = a + b;

  cout << "The sum of " << a << " and " << b << " is " << sum << endl;

}

void add(string a, string b) {

  string result = a + b;

  cout << "The result of adding " << a << " and " << b << " is " << result << endl;

}

删除操作

在C++中,删除操作可以通过虚函数实现。我们可以定义一个基类,其中包含一个虚函数,然后再定义多个子类继承自这个基类,并重写这个虚函数。当我们需要删除一个对象时,只需要调用这个虚函数,就会调用对应的子类中的函数来实现删除操作。

class Person {

public:

  virtual void deletePerson()

    cout << "This is the Person's delete function." << endl;

};

class Student : public Person {

public:

  void deletePerson()

    cout << "This is the Student's delete function." << endl;

};

class Teacher : public Person {

public:

  void deletePerson()

    cout << "This is the Teacher's delete function." << endl;

};

int main() {

  Person* p1 = new Student();

  Person* p2 = new Teacher();

  p1->deletePerson(); //调用Student中的deletePerson函数

  p2->deletePerson(); //调用Teacher中的deletePerson函数

  delete p1;

  delete p2;

  return 0;

}

改操作

在C++中,改操作可以通过函数重载和虚函数同时实现。我们可以定义一个基类,其中包含一个虚函数和几个重载函数,然后再定义多个子类继承自这个基类,并重写这个虚函数和重载函数。当需要进行改操作时,我们可以选择调用虚函数,也可以选择调用重载函数。

class Shape {

public:

  virtual void display()

    cout << "This is the Shape's display function." << endl;

  void display(int x)

    cout << "This is the Shape's display function with parameter of " << x << "." << endl;

};

class Circle : public Shape {

public:

  void display()

    cout << "This is the Circle's display function." << endl;

  void display(int x)

    cout << "This is the Circle's display function with parameter of " << x << "." << endl;

};

class Square : public Shape {

public:

  void display()

    cout << "This is the Square's display function." << endl;

  void display(int x)

    cout << "This is the Square's display function with parameter of " << x << "." << endl;

};

int main() {

  Shape* s1 = new Circle();

  Shape* s2 = new Square();

  s1->display(); //调用Circle中的display函数

  s1->display(10); //调用Shape中的display函数

  s2->display(); //调用Square中的display函数

  s2->display(20); //调用Shape中的display函数

  delete s1;

  delete s2;

  return 0;

}

查操作

在C++中,查操作可以通过虚函数实现。与删除操作类似,我们可以定义一个基类,其中包含一个虚函数,然后再定义多个子类继承自这个基类,并重写这个虚函数。当需要查找对象时,只需要调用这个虚函数,就会调用对应的子类中的函数来实现查找操作。

class Animal {

public:

  virtual void findAnimal()

    cout << "This is the Animal's find function." << endl;

};

class Dog : public Animal {

public:

  void findAnimal()

    cout << "This is the Dog's find function." << endl;

};

class Cat : public Animal {

public:

  void findAnimal()

    cout << "This is the Cat's find function." << endl;

};

int main() {

  Animal* a1 = new Dog();

  Animal* a2 = new Cat();

  a1->findAnimal(); //调用Dog中的findAnimal函数

  a2->findAnimal(); //调用Cat中的findAnimal函数

  delete a1;

  delete a2;

  return 0;

}

综上所述,C++的动态多态能够帮助我们实现增删改查等对象操作。通过函数重载和虚函数的灵活应用,我们可以编写出更加高效、可维护的程序。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复