栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++继承与多态(个人笔记)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++继承与多态(个人笔记)

偶然发现自己对继承跟多态的操作并不是很熟悉,今天重新温习了一遍,做个笔记方便重新回顾

首先继承:

#include
using namespace std;

class Animal
{
public:
	Animal();
	~Animal();
	Animal(int age, string name);
private:
	int A_age;
	string A_name;
};

Animal::Animal()
{
	cout << "Animal无参构造函数的调用" << endl;
	A_age = 10;
	A_name = "动物";
}

Animal::~Animal()
{
	cout << "Animal析构函数的调用" << endl;
}
Animal::Animal(int age, string name)
{
	cout << "Animal有参构造函数的调用" << endl;
	this->A_age = age;
	this->A_name = name;
}

class Cat : public Animal
{
public:
	Cat();
	~Cat();

private:
	int C_age;
	string C_name;
};

Cat::Cat()
{
	cout << "Cat无参构造函数的调用" << endl;
	C_age = 1;
	C_name = "小猫";
}

Cat::~Cat()
{
	cout << "Cat析构函数的调用" << endl;
}

void test01()
{
	Cat c;
}

int main()
{
	test01();
}

运行后:

发现会派生类会调用基类的无参构造函数

继承允许多继承

钻石继承(菱形继承)的概念:

基类Father下有两个派生类Son,Daughter,再有一个Grandson继承了Son跟Daughter,就是钻石继承(菱形继承)

#include
using namespace std;

class Father
{
public:
	Father();
	~Father();

private:

};

Father::Father()
{
}

Father::~Father()
{
}

class Son : public Father
{
public:
	Son();
	~Son();

private:

};

Son::Son()
{
}

Son::~Son()
{
}

class Daughter : public Father
{
public:
	Daughter();
	~Daughter();

private:

};

Daughter::Daughter()
{
}

Daughter::~Daughter()
{
}

class Grandson :public Son,public Daughter
{
public:
	Grandson();
	~Grandson();

private:

};

Grandson::Grandson()
{
}

Grandson::~Grandson()
{
}

int main()
{

}

具体实现不展示了,只需要知道钻石继承会存在的问题:

1、数据继承下容易产生二义性

2、Grandson继承Father两份数据,其实只需要一份

解决方法:

1、两个父类拥有相同数据,那么便加以作用域去区分

2、使用虚继承

class Son : virtual public Father
class Daughter :virtual public Father

这个时候基类称为虚基类,虚继承后数据则变成了一份。

其次多态:

#include
using namespace std;

class Animal
{
public:
	Animal();
	~Animal();
virtual	void speack()
	{
		cout << " 动物在叫" << endl;
	}
private:
	int A_age;
	string A_name;
};

Animal::Animal()
{
	cout << "Animal无参构造函数的调用" << endl;
	A_age = 10;
	A_name = "动物";
}

Animal::~Animal()
{
	cout << "Animal析构函数的调用" << endl;
}


class Cat : public Animal
{
public:
	Cat();
	~Cat();
	void speack()
	{
		cout << " 小猫在叫" << endl;
	}
private:
	int C_age;
	string C_name;
};

Cat::Cat()
{
	cout << "Cat无参构造函数的调用" << endl;
	C_age = 1;
	C_name = "小猫";
}

Cat::~Cat()
{
	cout << "Cat析构函数的调用" << endl;
}

void Speack(Animal& a)
{
	a.speack();
}

void test01()
{
	Cat c;
	Speack(c);
	
}

int main()
{
	test01();
}

运行结果:

基类与派生类存在同名函数,那么将基类函数设为虚函数,子类再去重写基类的虚函数即可调用子类的同名函数。

其中若是:

#include
using namespace std;

class Animal
{
public:
	Animal();
	~Animal();
	virtual	void speack() = 0;

private:
	int A_age;
	string A_name;
};

Animal::Animal()
{
	cout << "Animal无参构造函数的调用" << endl;
	A_age = 10;
	A_name = "动物";
}

Animal::~Animal()
{
	cout << "Animal析构函数的调用" << endl;
}


class Cat : public Animal
{
public:
	Cat();
	~Cat();
	void speack()
	{
		cout << " 小猫在叫" << endl;
	}
private:
	int C_age;
	string C_name;
};

Cat::Cat()
{
	cout << "Cat无参构造函数的调用" << endl;
	C_age = 1;
	C_name = "小猫";
}

Cat::~Cat()
{
	cout << "Cat析构函数的调用" << endl;
}

void Speack(Animal& a)
{
	a.speack();
}

void test01()
{
	Cat c;
	Speack(c);
	
}

int main()
{
	test01();
}

 可以看到基类的虚函数 = 0,这里的意思是设定纯虚函数,若是基类设定了纯虚函数,那么基类就会变为抽象类,抽象类是无法实例化对象的。这时候若是派生类不去重写基类中的纯虚函数,那么派生类也会变成一个抽象类!

此时若是在test01()中尝试生成一个Animal animal

学C++的道路还是很漫长

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979889.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号