08C++多态与虚函数

多态与虚函数

1.多态介绍

什么是多态,简单来说,就是父类指向子类的成员,使用子类的成员,在java中,我们很方便地去使用多态,那是因为java已经为我们对多态进行了处理。在C++中,我们就需要去了解多态的本质,只有了解多态的本质,在使用时,才不会感到迷茫。

2.多态简单案例分析

#include <iostream>
using namespace std;

class Animal
{
public:
    int age;
    Animal(int age);
    void display() const;
};

Animal::Animal(int age)
{
    this->age = age;
}

void Animal::display() const
{
    cout << this->age << endl;
}

class Tiger : public Animal
{
public:
    string name;
    int age;
    Tiger(string name, int age);
    void display() const;
};

Tiger::Tiger(string name, int age) : Animal(age)
{
    this->name = name;
    this->age = age;
}

void Tiger::display() const
{
    cout << this->name << this->age << endl;
}

class Loin : public Animal
{
public:
    string name;
    Loin(string name, int age);
    void display() const;
};

Loin::Loin(string name, int age) : Animal(age)
{
    this->name = name;
}

void Loin::display() const
{
    cout << this->name << this->age << endl;
}

void show(Animal &animal)//animal对象实际指向Tiger或Loin
{
    animal.display(); //只会打印Animal的成员,并且由于调用Animal的display()函数,则使用Animal的成员
}

void show2(Animal *const animal)//等价于上面show
{
    animal->display();//只会打印Animal的成员
}

void test()
{
    Animal a(10);
    Tiger t("老虎", 3);
    Loin l("狮子", 2);
    t.age = 20;
    // show(t);
    // show(l);
    show2(&t);
    show2(&l);

    Animal * animal = &t;
    animal->display(); // 3
}

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

3
2
请按任意键继续. . .

发现:只会打印Animal的成员,而没有打印子类的成员,这不是我们想要的,也不是多态。

原因解答: void show(Animal &animal),已经为我们确定了Animal类型,根据上一章节12派生类指向基类本质分析,可以知道编译器是以类型来判定执行成员函数的,而不是实际指针指向,Animal类型只会执行Animal的成员函数。所以,尽管show(t);执行后,animal实际对象指向的是Tiger或Loin。

结论:

  • 1.指针base = b;与对象赋值不同,指针的赋值,是指针改变了方向,指向另外一个内存空间.base指针指向b的内存空间. 隐式指针 this 发生了变化
  • 2.编译器是通过类型来访问成员函数的,而不是所指的具体对象.
  • 3.成员函数,漏掉了一点,访问的成员函数属于父类,则首先调用父类的成员,反之,成员函数属于子类,则首先调用子类的成员。

解答第三点:我们知道Tiger或Lion对象,实际是创建了Animal对象,当我们修改t.age = 20;值时,这里实际修改的时Tiger对象的age,而父类Animal的age是没有修改的。由于类型是Animal,它只会Animal的成员函数,属于父类,首先调用父类的成员。

#include <iostream>
using namespace std;
//基类People
class People
{
public:
    People(char *name, int age);
    void display();

public:
    char *name;
    int age;
};
People::People(char *name, int age) : name(name), age(age) {}
void People::display()
{
    cout << this->name << "今年" << this->age << "岁了,是个无业游民。" << endl;
}
//派生类Teacher
class Teacher : public People
{
public:
    Teacher(char *name, int age, int salary);
    void display();

public:
    char *name;

private:
    int salary;
};
Teacher::Teacher(char *name, int age, int salary) : People(name, age), salary(salary)
{
    this->name = name;
}
void Teacher::display()
{
    cout << this->name << "今年" << this->age << "岁了,是一名教师,每月有" << this->salary << "元的收入。" << endl;
}

void show(People *const p)
{
    p->display();
}

int main()
{
    Teacher *t = new Teacher("赵宏佳", 45, 8200);
    t->name = "zhangsan";
    t->People::age = 20;
    show(t);
    system("pause");
    return 0;
}

3.virtual虚函数解决多态问题

    virtual void display() const;//Animal的display函数声明为虚函数virtual
#include <iostream>
using namespace std;

class Animal
{
public:
    int age;
    Animal(int age);
    virtual void display() const;
};

Animal::Animal(int age)
{
    this->age = age;
}

void Animal::display() const
{
    cout << this->age << endl;
}

class Tiger : public Animal
{
public:
    string name;
    int age;
    Tiger(string name, int age);
    virtual void display() const;//virtual可加可不加
};

Tiger::Tiger(string name, int age) : Animal(age)
{
    this->name = name;
    this->age = age;
}

void Tiger::display() const
{
    cout << this->name << this->age << endl;
}

class Loin : public Animal
{
public:
    string name;
    Loin(string name, int age);
    void display() const;
};

Loin::Loin(string name, int age) : Animal(age)
{
    this->name = name;
}

void Loin::display() const
{
    cout << this->name << this->age << endl;
}

void show(Animal &animal)
{
    animal.display(); //只会打印Animal的成员
}

void show2(Animal *const animal) //等价于上面show
{
    animal->display(); //只会打印Animal的成员
}

void test2()
{
    Tiger *t2 = new Tiger("打老虎", 10);
    t2->age = 300;
    show2(t2);
}

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

打老虎300;实际调用地是Tiger

发现:加上virtual虚函数之后,实际调用发生了改变,实际调用了Tiger的display()函数。

4.分析virtual虚函数是如何解决多态问题

virtual虚函数:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BW0zBaGA-1625408349119)(G:\markdown\C++\C++基础\多态virtual.PNG)]

如上图所示:

1.Animal会自己生成virtual表,vfptr指针指向表中某个成员函数,vptr指向成员属性。

2.Tiger也会生成virtual表,vfptr指针指向自身类中某个成员函数。

3.vfptr只会指向实际的对象,当出现Animal &animal时,animal实际指向是Tiger,vfptr指针也会指向Tiger,所以使用animal时,实际访问的是Tiger。

简单理解为,子类的vfptr覆盖了父类(实际其实没有),只是二者指针指向了同一个Tiger成员函数。

5.虚析构

看一个案例:

#include <iostream>
using namespace std;

class Base
{
public:
    Base();
    ~Base();
};

Base::Base()
{
    cout << "Base构造调用了" << endl;
}

Base::~Base()
{
    cout << "Base析构调用了" << endl;
}

class A : public Base
{
public:
    string *name;
    int age;

public:
    A();
    A(string name, int age);
    ~A();
};

A::A(string name, int age) : Base()
{
    this->name = new string(name);
    this->age = age;
}

A::~A()
{
    cout << "A析构了" << endl;
    if (this->name != NULL)
    {
        delete this->name;
        this->name = NULL;
    }
}

void test()
{
    Base *b = new A("你好", 20);
    delete b;
    b = NULL;
}

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

Base构造调用了
Base析构调用了

可以看到,只析构了Base,A的析构没有执行,这样会导致内存泄漏,所以为什么会需要虚析构原因。

    virtual ~Base();

虚析构原理:和上面所讲一致,指针指向实际的内容。

第二种方式:纯虚析构

    virtual ~Base() = 0;

注意:纯虚析构,也要注意:

Base::Base()
{
    cout << "Base构造调用了" << endl;
}

虚析构和纯虚析构区别:
如果纯虚析构,该类属于抽象类,无法实例化对象,

6.抽象类

抽象类特点:

​ 1.抽象类不能直接实例化

​ 2.抽象类只能作为基类来使用,不能定义抽象类对象,但是可以子类实现抽象类之后实例化基类

#include <iostream>
using namespace std;
class Base
{
public:
    virtual void display() = 0;
    Base();
    ~Base();
};

Base::Base()
{
    cout << "base构造了" << endl;
}

Base::~Base()
{
    cout << "base析构了" << endl;
}

class Student : public Base
{
public:
    string name;
    int age;

public:
    ~Student();
    virtual void display();
};

Student::~Student()
{
    cout << "Student析构了" << endl;
}

void Student::display()
{
    cout << this->name << this->age << endl;
}

void test()
{
    Student stu;
    stu.display();
}

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

base构造了
24
Student析构了
base析构了
请按任意键继续. . .

——————子类实现抽象类之后,抽象类才可以实例化。

7.接口

C++中,是没有接口的关键字,我们可以采用纯虚函数的方式来实现。这样做的好处是能够实现封装和多态。

#include <iostream>
using namespace std;

class IBase
{
public:
    IBase(){};
    virtual ~IBase(){};
    virtual void setName(string name) = 0;
    virtual string getName() const = 0;
};
class Base : public IBase
{
public:
    string name;
public:
    Base();
    ~Base();
    virtual void setName(string name);
    virtual string getName() const;
};

void Base::setName(string name)
{
    this->name = name;
}
string Base::getName() const
{
    return this->name;
}
Base::Base()
{
}
Base::~Base()
{
}
void test()
{
    Base b;
    b.setName("zhangsan");
    cout << b.getName() << endl;
}

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

ic:
string name;
public:
Base();
~Base();
virtual void setName(string name);
virtual string getName() const;
};

void Base::setName(string name)
{
this->name = name;
}
string Base::getName() const
{
return this->name;
}
Base::Base()
{
}
Base::~Base()
{
}
void test()
{
Base b;
b.setName(“zhangsan”);
cout << b.getName() << endl;
}

int main(int argc, char const *argv[])
{
test();
system(“pause”);
return 0;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值