C++的多态案例

计算器案例

class rule
{
public:
	virtual double getnum() = 0;
};
class plus_rule :public rule
{
public:
	plus_rule(double a, double b)
	{
		this->a = a;
		this->b = b;
	}
	virtual double getnum()
	{
		return a + b;
	}
private:
	double a, b;
};
class min_rule :public rule
{
public:
	min_rule(double a,double b)
	{
		this->a = a;
		this->b = b;
	}
	virtual double getnum()
	{
		return a - b;
	}
private:
	double a, b;
};
class operatore
{
public:
	operatore()
	{
		r = new rule * [2];
		msize = 0;
	}
	void addcaozuo(rule*rule)
	{
		r[msize] = rule;
		msize++;
	}
	void show()
	{
		for (int i = 0; i < 2; i++)
		{
			cout << r[i]->getnum() << endl;
		}
	}
	~operatore()
	{
		for (int i = 0; i < 2; i++)
		{
			delete r[i];
			r[i] = NULL;
		}
		delete[]r;
		r = NULL;
	}
private:
	int msize;
	rule** r;
};
void test()
{
	operatore* r = new operatore;
	r->addcaozuo(new plus_rule(3, 5));
	r->addcaozuo(new min_rule(4, 6));
	r->show();
	delete r;
	r = NULL;
}

动物园案例()

#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
//1:创建动物园基类
class animal
{
public:
	virtual void speak() = 0;
};
//2:创建动物
class Dog :public animal
{
public:
	Dog(string name)
	{
		this->name = name;
	}
	virtual void speak()
	{
		cout << "小狗" << name << "旺旺" << endl;
	}
private:
	string name;
};
class Dark :public animal
{
public:
	Dark(string name,string type)
	{
		this->name = name;
		this->type = type;
	}
public:
	virtual void speak()
	{
		cout << type << "品牌的" << "小鸭子" << name << "嘎嘎嘎"<name = name;
		this->age = age;
	}
	virtual void speak()
	{
		cout << age << "年龄的" << "老虎" << name <<"轰轰" << endl;
	}
private:
	string name;
	int age;
};
class cat :public animal
{
public:
	cat(string name)
	{
		this->name = name;
	}
	virtual void speak()
	{
		cout << "小猫" << name <<"喵喵"<< endl;
	}
private:
	string name;
};
//3:创建动物园
class Zoo
{
public:
	Zoo()
    {
	mcapacity = 1024;
	msize = 0;
	//申请空间,存储animal*的空间,用指针数组
	this->p = new animal * [mcapacity];
	}
	//增加动物
	int Addanimal(animal* animal)
	{
		if (msize == mcapacity)
		{
			return -1;
		}
		//把指针存储到指针数组
		this->p[msize] = animal;
		msize++;
	}
	void startspeak()
	{
		for (int i = 0; i < msize; i++)
		{
			p[i]->speak();
		}
	}
	//析构函数
	~Zoo()
	{
		//先释放指针数组中指针指向的堆区空间
		for (int i = 0; i < msize; i++)
		{
			if (p[i] != NULL)
			{
				delete p[i];
				p[i] = NULL;
			}
		}
		//释放指针数组
		delete[]p;
		p = NULL;
	}
private:
	animal** p;
	int mcapacity;//容量
	int msize;
};
void test()
{
	//创建动物园
	Zoo* zoo = new Zoo;
	//添加动物
	zoo->Addanimal(new Dog("丹单"));
	zoo->Addanimal(new Dark("小黄","大黄鸭"));
	zoo->Addanimal(new tiger("啊啊", 12));
	zoo->Addanimal(new cat("慧慧"));
	zoo->startspeak();
	delete zoo;
}
int main()
{
	test();
	system("pause");
	return EXIT_SUCCESS;
}

班级人数案例

#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
class people
{
public:
	virtual void show() = 0;
};
class maker :public people
{
public:
	maker(string name, int age,string sex,string skill)
	{
		this->name = name;
		this->age = age;
		this->sex = sex;
		this->skill = skill;
	}
	virtual void show()
	{
		cout << age << "岁的" << sex << name << skill << endl;
	}
private:
	string name;
	int age;
	string sex;
	string skill;
};
class myclass
{
public:
	myclass()
	{
		mcapacity = 52;
		msize = 0;
			this->p = new people * [mcapacity];
	}
	void addpeople(people*pe)
	{
		if (mcapacity == msize)
		{
			return;
		}
		p[msize] = pe;
		msize++;
	}
	void show()
	{
		for (int i = 0; i < msize; i++)
		{
			p[i]->show();
		}
	}
	~myclass()
	{
		for (int i = 0; i < msize; i++)
		{
			if (p[i] != NULL)
			{
				delete p[i];
				p[i] = NULL;
			}
		}
		delete[]p;
		p = NULL;
	}
private:
	people** p;
	int mcapacity;
	int msize;
};
void test()
{
	myclass* clss = new myclass;
	clss->addpeople(new maker("hui",20, "女生", "会喵喵叫"));
	clss->show();
}
int main()
{
	test();
	system("pause");
	return EXIT_SUCCESS;
}

你可能感兴趣的:(c++,算法,开发语言)