C++(OOP)纯虚函数与抽象类

纯虚函数与抽象类

  • 虚函数
  • 抽象类 抽象数据类型
    • 任何包含一个或多个纯虚函数的类都是抽象类
    • 不要创建这个类的对象,应该继承它
    • 务必覆盖从这个类继承的纯虚函数
  • 实现纯虚函数
  • C++接口
    • 只包含纯虚函数的抽象基类
#include <iostream>

using namespace std;

enum COLOR { Red, Green, Blue, Yellow, White, Black, Brow};

class Animal
{
	public:
		Animal(int);
		virtual ~Animal() {};
		virtual int GetAge() const { return itsAge; }
		virtual void SetAge(int age) { itsAge = age; }
		virtual void Sleep() const = 0;
		virtual void Eat() const = 0;
		virtual void Reproduce() const = 0;
		virtual void Move() const = 0;
		virtual void Speak() const = 0;
	private:
		int itsAge;
};

Animal::Animal(int age):itsAge(age)
{
	cout << "Animal构造函数被调用..\n";
}

class Mammal : public Animal
{
public:
	Mammal(int age):Animal(age)
	{
		cout << "Mammal构造函数被调用...\n";
	}

	virtual ~Mammal() 
	{
		cout << "<ammal析构函数被调用...\n";
	}

	virtual void Reproduce() const 
	{
		cout << "Mammal reproduction depicted...\n";
	}
};

class Fish : public Animal
{
public:
	Fish(int age) : Animal(age) 
	{
		cout << "Fish构造函数被调用...\n";
	}
	virtual ~Fish()
	{
		cout << "Fish析构函数被调用...\n";
	}
	virtual void Sleep() const { cout << "fish snoring...\n"; }
	virtual void Eat() const { cout << "fish feeding...\n"; }
	virtual void Reproduce() const { cout << "fish laying eggs...\n"; }
	virtual void Move() const { cout << "fish swimming...\n"; }
	virtual void Speak() const {}
};

class Horse : public Mammal
{
public:
	Horse(int age, COLOR color):Mammal(age), itsColor(color)
	{
		cout << "Horse构造函数被调用...\n";
	}
	virtual ~Horse()
	{
		cout << "Horse析构函数被调用...\n";
	}
	virtual void Speak() const { cout << "Whinny...\n"; }
	virtual void Sleep() const { cout << "Horse snoring...\n"; }
	virtual COLOR GetItsColor() const { return itsColor; }
	virtual void Eat() const { cout << "Horse feeding...\n"; }
	virtual void Move() const { cout << "Horse running...\n"; }

protected:
	COLOR itsColor;
};

class Dog : public Mammal 
{ 
public:
	Dog(int age, COLOR color) : Mammal(age), itsColor(color)
	{
		cout << "Dog构造函数被调用...\n";
	}
	virtual ~Dog()
	{
		cout << "Dog析构函数被调用...\n";
	}
	virtual void Speak() const { cout << "Whoof!...\n"; }
	virtual void Sleep() const { cout << "Dog snoring...\n"; }
	virtual void Eat() const { cout << "Dog eating...\n"; }
	virtual void Move() const { cout << "Dog running...\n"; }
	virtual void Reproduce() const 
	{
		cout << "Dogs reproducing...\n";
	}

protected:
	COLOR itsColor;
};

int main()
{
	Animal *pAnimal = 0;	
	int choice;
	bool fQuit = false;

	while(fQuit == false)
	{
		cout << "(1)Dog (2)Horse (3)Fish (0)Quit: ";
		cin >> choice;

		switch(choice)
		{
			case 1:
				pAnimal = new Dog(5, Brow);
				break;
			case 2:
				pAnimal = new Horse(4, Black);
				break;
			case 3:
				pAnimal = new Fish(5);
				break;
			default:
				fQuit = true;
				break;
		}
		if (fQuit == false)
		{
			pAnimal->Speak();
			pAnimal->Eat();
			pAnimal->Reproduce();
			pAnimal->Move();
			delete pAnimal;
			cout << endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值