c++继承(有参构造、无参构造、虚函数、虚基类)

1.继承(无参构造函数)

#include<iostream>
#include<string>
using namespace std;
#if 0
class animal {
public:
	string name;
	int age;
	string sex;
	//无参构造函数
	animal() {

	}
	//有参构造函数
	/*
	animal(string a,int b,string c) {
		name = a;
		age = b;
		sex = c;
	}*/
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "不叫" << endl;
	}
	void skill()
	{

		cout << "我是动物,我会吃肉呀" << endl;
	}

};


class cat:public animal{
public:
	void show() {
		cout << age<<"岁的"<<name << "睡着了" << endl;
		cout << name << "喵喵叫" << endl;
		
	}
	void skill()
	{

		cout << "我是加菲猫,我会飞檐走壁" << endl;
	}
};

class dog :public animal {
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "汪汪叫" << endl;
	}
	void skill()
	{

		cout << "我是哮天犬,我会接飞饼" << endl;
	}
};



int main() {
	animal a;
	a.name = "动物";
	a.age = 18;
	a.sex = "无性别";
	a.show();

	cat c;
	c.name = "猫";
	c.age = 6;
	c.sex = "女";
	c.show();

	dog d;
	d.name = "狗";
	d.age = 20;
	d.sex = "男";
	d.show();

	
	a.skill();
	d.skill();
	c.skill();
}

#endif

2.继承(有参构造函数)

构造函数不可以被继承

#include<iostream>
#include<string>
using namespace std;

class animal {
public:
	string name;
	int age;
	string sex;
	//有参构造函数
	animal(string name,int age,string sex) {
		
		this->name = name;
		this->age = age;
		this->sex = sex;
	}
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "不叫" << endl;
	}
	void skill()
	{
		cout << "我是动物,我会吃肉呀" << endl;
	}

};
class cat:public animal{
public:
	//有参构造函数
	cat(string a, int b, string c):animal(a,b,c) {
		
	}
	void show() {
		cout << age << "岁的" << name << "睡着了" << endl;
		cout << name << "喵喵叫" << endl;
	}
	void skill()
	{
		cout << "我是加菲猫,我会飞檐走壁" << endl;
	}
};

class dog :public animal{
public:
	//有参构造函数
	dog(string name, int age, string sex):animal(name,age,sex) {
	
	}
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "汪汪叫" << endl;
	}

	void skill()
	{
		cout << "我是哮天犬,我会接飞饼" << endl;
	}
};



int main() {
	animal a("动物",18, "无性别");
	a.show();

	cat c("猫",6, "女");
	c.show();

	dog d("狗",20, "男");
	d.show();
	a.skill();
	d.skill();
	c.skill();
}

3. 继承(虚函数)

3.1虚函数(无参构造)

案例一:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#if 1
//虚函数使用 继承
class Animal
{
public:
	string kind;
	double age;
public:
	/*Animal(string k , double a )
	{
		kind = k;
		age = a;
	}*/

	/*void printinfo()
	{
		cout << kind << "\t\t " << age << endl;
	}*/
	void skill()
	{
		cout << "基类技巧" << endl;
	}
	virtual void talk()
	{
		cout << "动物叫" << endl;
	}
};

class  Dog :public Animal
{
public:
	//int num;
	void skill()
	{
		cout << "我是哮天犬,我可以蹦来蹦去" << endl;
	}
	 void talk()
	{
		cout << "汪汪叫" << endl;
	}
	//Dog(string k, double b,int p) :Animal(k, b),num(p) {}
};
class Cat :public Animal
{
public:

	void skill()
	{
		cout << "我是加菲猫,我爱吃鱼" << endl;
	}
	 void talk()
	{
		cout << "喵喵叫" << endl;
	}
	void fun()
	{
		cout << "我是五彩斑斓的加菲猫" << endl;
	}
};
class Dark :public Animal
{
public:

	void skill()
	{
		cout << "我是鸭子,我爱吃虫" << endl;

	}
	 void talk()
	{
		cout << "呱呱叫" << endl;
	}
	void fun()
	{
		cout << "我想变成天鹅" << endl;
	}
};

int main()
{
	Animal animal;
	//animal.talk();//"动物叫"
	Dog dog;
	//dog.talk();//"汪汪叫"
	Cat cat;
	//cat.talk();//"喵喵叫"
	Dark dark;
	cout << "*******************" << endl;

	Animal *p;//基函数加tirtual 说明为虚函数 子类中覆盖函数不用加默认虚函数。
	p = &animal;
	p->talk();

	p = &dog;
	p->talk();

	p = &cat;
	p->talk();

	Animal &p1 = dark;
	p1.talk();
	cout << "*******************" << endl;
	//child baby;
	//baby.Cat::age = 9;
	//baby.Cat::skill();
	//baby.Dog::skill();//方法1
	//baby.skill();
	system("pause");
}

案例二:

#include<iostream>
#include<string>
using namespace std;

class animal {
public:
	string name;
	int age;
	string sex;
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "不叫" << endl;
	}
	virtual void skill()
	{
		cout << "我是动物,我会吃肉呀" << endl;
	}

};


class cat:public animal{
public:
	void show() {
		cout << age<<"岁的"<<name << "睡着了" << endl;
		cout << name << "喵喵叫" << endl;
		
	}
	virtual void skill()
	{
		cout << "我是加菲猫,我会飞檐走壁" << endl;
	}
};

class dog :public animal {
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "汪汪叫" << endl;
	}
	virtual void skill()
	{

		cout << "我是哮天犬,我会接飞饼" << endl;
	}
};



int main() {
	animal a;
	a.name = "动物";
	a.age = 18;
	a.sex = "无性别";
	a.show();

	cat c;
	c.name = "猫";
	c.age = 6;
	c.sex = "女";
	c.show();

	dog d;
	d.name = "狗";
	d.age = 20;
	d.sex = "男";
	d.show();

	animal *p;//基函数加tirtual 说明为虚函数 子类中覆盖函数不用加默认虚函数。
	p = &a;
	p->skill();

	p = &c;
	p->skill();

	p = &d;
	p->skill();

}

3.2虚函数(有参构造)

虚函数解决多态性的。

#include<iostream>
#include<string>
using namespace std;

//虚函数
class animal {
public:
	string name;
	int age;
	string sex;
	//有参构造函数
	animal(string name, int age, string sex) {

		this->name = name;
		this->age = age;
		this->sex = sex;
	}
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "不叫" << endl;
	}
	virtual void skill()
	{
		cout << "我是动物,我会吃肉呀" << endl;
	}

};


class cat :public animal {
public:
	//有参构造函数
	cat(string a, int b, string c) :animal(a, b, c) {
		this->name = a;
		this->age = b;
		this->sex = c;
	}
	void show() {
		cout << age << "岁的" << name << "睡着了" << endl;
		cout << name << "喵喵叫" << endl;
	}
	void skill()
	{
		cout << "我是加菲猫,我会飞檐走壁" << endl;
	}
};

class dog :public animal {
public:
	//有参构造函数
	dog(string name, int age, string sex) :animal(name, age, sex) {
		this->name = name;
		this->age = age;
		this->sex = sex;
	}
public:
	void show() {
		cout << age << "岁的" << sex << name << "睡着了" << endl;
		cout << name << "汪汪叫" << endl;
	}

	void skill()
	{
		cout << "我是哮天犬,我会接飞饼" << endl;
	}
};



int main() {
	animal a("动物", 18, "无性别");
	cat c("猫", 6, "女");
	dog d("狗", 20, "男");

	animal *p;//基函数加tirtual 说明为虚函数 子类中覆盖函数不用加默认虚函数。
	p = & a;
	p->skill();

	p = &c;
	p ->skill();

	p = &d;
	p->skill();
}

4. 虚基类

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#if 1
/*
C++中的虚函数的作用主要是实现了多态的机制,关于多态,简而言之就是用父类型别的指针指向
其子类的实例,然后通过父类的指针调用
实际子类的成员函数。这种技术可以让父类的指针有“多种形态”,这是一种泛型技术。
所谓泛型技术,说白了就是试图使用不变的代码来实现可变的算法。*/
class Animal
{
public:
	string kind;
	int age;
	Animal(string a="0",int b=0)
	{
		kind = a;
		age = b;
	}
	virtual void talk()
	{
		cout << "动物叫" << endl;
	}

};
class Dog :virtual public Animal//虚基类
{
public:
	char sex;
	Dog(string a="0", int b=0, char c='0') :Animal(a, b), sex(c) {}
	/*{
		sex = c;
	}*/
	 void talk()
	{
		cout << "汪汪叫" << endl;
	}

};
class Cat :virtual public Animal//虚基类
{public:
	string color;
	Cat(string a="0", int b=0, string d="0") :Animal(a, b)
	{
		color = d;
	}
	void talk()
	{
		cout << "喵喵叫" << endl;
	}
};
class Child :public Dog, public Cat
{
public:
	string name;
	Child(string a = "0", int b = 0, char c = '0', string d = "0", string e="0"):Animal(a,b),Dog(a,b,c),Cat(a,b,d)
	{
		name = e;
	}
	void talk()
	{
		cout << "我不仅会往往叫还会喵喵叫" << endl;
	}
	void show()
	{
		cout << "kind:" << kind << endl;
		cout << "age:" << age << endl;
		cout << "sex:" << sex << endl;
		cout << "name:" << name << endl;
		cout << "color:" << color << endl;
	}
};
//虚函数使用
int main()
{
	cout << "*********虚函数*********" << endl;
	Animal*p;
	Animal a;
	p = &a;
	p->talk();

	Dog d;
	p = &d;
	p->talk();

	Cat c;
	p = &c;
	p->talk();
	
	cout << "**********虚基类*********" << endl;
	Child child("妖怪",8, 'm', "pink", "神兽");
	child.talk();
	child.show();
	system("pause");
}

总结:

构造函数:创建对象时,默认自动调用构造函数。构造函数名和类名相同。
拷贝构造函数:用于建立新的对象时,将已存在的对象数据拷贝给新数据。
析构函数:释放对象内存空间。
虚函数:主要解决多态问题。虚函数不是静态函数,也不是友元函数;只有类的成员函数才能为虚函数,虚函数的声明只能出现在类的定义中,因为虚函数仅适用于有继承关系的类对象,普通函数不能说明为虚函数。虚函数只是在成员函数前加上关键字virtual。
虚基类:主要解决多重继承导致的二义性。主要是在继承时对相同成员变量重复继承,导致有歧义,所以在继承时,在该类加上virtual关键字,变为虚基类。

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小飞龙程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值