12-C++面向对象(父类指针、子类指针、多态、虚函数、虚表)

父类指针、子类指针

  •         父类指针可以指向子类对象,是安全的,开发中经常用到(继承方式必须是public)
  •         子类指针指向父类对象是不安全的
#include<iostream>
using namespace std;
struct Person
{
	int m_age;
};
struct Student:Person
{
	int m_score;
};
int main() {
	//父类指针指向子类对象
	Person* p = new Student();
	p->m_age = 10;
	return 0;
}

p指针只能操作m_age变量 

 多态

        默认情况下,编译器只会根据指针类型调用对应的函数,不存在多态

   多态是面向对象非常重要的一个特性:

  •         同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果
  •         在运行时,可以识别出真正的对象类型,调用对应子类中的函数

   多态的要素:

  •         子类重写父类的成员函数(override)
  •         父类指针指向子类对象
  •         利用父类指针调用重写的成员函数

虚函数

  •         C++中的多态通过虚函数(virtual function)来实现
  •         虚函数:被virtual修饰的成员函数
  •         只要在父类中声明为虚函数,子类中重写的函数也自动变成虚函数(也就是说子类中可以省略virtual关键字)
#include<iostream>
using namespace std;
//struct Person
//{
//	int m_age;
//};
//struct Student:Person
//{
//	int m_score;
//};
struct Animal
{
	virtual void run() {
		cout << "Animal::run()" << endl;
	}
	void speak() {
		cout << "Animal::speak()" << endl;
	}
};
struct Dog:Animal
{
	void run() {
		cout << "Dog::run()" << endl;
	}
	void speak() {
		cout << "Dog::speak()" << endl;
	}
};
struct Cat :Animal
{
	void run() {
		cout << "Cat::run()" << endl;
	}
	void speak() {
		cout << "Cat::speak()" << endl;
	}
};
struct Pig :Animal
{
	void run() {
		cout << "Pig::run()" << endl;
	}
	void speak() {
		cout << "Pig::speak()" << endl;
	}
};
void liu(Animal *p) {
	p->speak();
	p->run();
}
int main() {
	//父类指针指向子类对象
	/*Person* p = new Student();
	p->m_age = 10;*/
	liu(new Dog());
	liu(new Cat());
	liu(new Pig());
	return 0;
}

虚表

        虚函数的实现原理是虚表,这个虚表里面存储着最终需要调用的虚函数地址,这个虚表也叫虚函数表

        所有的Cat对象(不管在全局区还是、栈、堆)共用同一份虚表

#include<iostream>
using namespace std;
struct Animal
{
	int m_age;
	virtual void speak() {
		cout << "Animal::speak()" << endl;
	}
	virtual void run() {
		cout << "Animal::run()" << endl;
	}
};
struct Cat:Animal
{
	int m_life;
	void speak() {
		cout << "Cat::speak()" << endl;
	}
	void run() {
		cout << "Cat::run()" << endl;
	}
};

int main() {
	cout << sizeof(Cat) << endl;
	return 0;
}

调用父类的成员函数实现     

struct Animal
{
	virtual void speak() {
		cout << "Animal::speak()" << endl;
	}
};
struct Cat:Animal
{
	void speak() {
        Animal::speak();
		cout << "Cat::speak()" << endl;
	}
	
};

猜你喜欢

转载自blog.csdn.net/qq_56728342/article/details/129717172