C++-抽象类和纯虚函数

#include <iostream>

using namespace std;

//抽象类,包含一个或多个抽象函数
//不能实例化对象,只可以继承
//继承之后的虚函数还虚函数,而且想要实例化对象必须把所有抽象函数覆盖,否则子类还是抽象类
//所继承的父类只要任何一个成员函数是虚的,子类的析构函数也应当是虚的
class Shape
{
public:
	Shape() {}
	virtual ~Shape(){}
	virtual double GetArea() = 0;
	virtual double GetPerim() = 0;
	virtual void Draw() = 0;
};
//纯虚函数一般不写,纯虚函数的实现,要对后代有用
void Shape::Draw()
{
	cout << "Abstract drawing 方法";
}
class Circle :public Shape
{
public:
	Circle(int radius):itsRadius(radius){}
	virtual ~Circle(){}
	double GetArea() { return 3.14 * itsRadius * itsRadius;}
	double GetPerim() { return 2 * 3.14 * itsRadius; }
	void Draw();
private:
	int itsRadius;
};
void Circle::Draw()
{
	cout << "Circle drawing routine here!\n";
	Shape::Draw();//纯虚函数的调用
}
class Retangle :public Shape
{
public:
	Retangle(int len, int width) : itsWidth(width),itsLength(len){}
	virtual ~Retangle() {}
	double GetArea() { return itsLength * itsWidth; }
	double GetPerim() { return 2 * itsWidth + 2 * itsLength; }
	virtual int GetLenth() { return itsLength; }
	virtual int GetWidth() { return itsWidth; }
	void Draw();
private:
	int itsWidth;
	int itsLength;
};
void Retangle::Draw()
{
	for (int i = 0; i < itsLength; i++)
	{
		for (int j = 0; j < itsLength; j++)
			cout << "x";
		cout << "\n";
	}
	Shape::Draw();//纯虚函数的调用
}
class Square :public Retangle
{
public:
	Square(int len);
	Square(int len, int width);
	virtual ~Square() {}
	double GetPerim() { return 4 * GetLenth(); }
};
Square::Square(int len):Retangle(len,len){}
Square::Square(int len, int width) : Retangle(len, width)
{
	if (GetLenth() != GetWidth())
		cout << "Error, not a square...a Rectangle??\n";
}
int main()
{
	//Circle a(8);
	//a.Draw();

	//Retangle b(10, 5);
	//b.Draw();

	//Square c(8);
	//c.Draw();

	int choice;
	bool fQuit = false;
	Shape* sp;

	while (fQuit == false)
	{
		cout << "(1)Circle (2)Retangle (3)Square (0)Quit ";
		cin >> choice;
		switch (choice)
		{
		case 1:
			sp = new Circle(5);	//指针的方法创建Circle对象
			break;
		case 2:
			sp = new Retangle(5, 10);
			break;
		case 3:
			sp = new Square(5);
			break;
		case 0:
			fQuit = true;
			break;
		default:
			break;
		}
		if (fQuit == false)
		{
			sp->Draw();
			delete sp;
			cout << endl;
		}
	}
	return 0;
}

一个例子:

#include <iostream>

using namespace std;

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

class Animal
{
public:
	Animal(int);
	virtual ~Animal() = 0;
	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 Speek() const = 0;

private:
	int itsAge;
};
Animal::Animal(int age) :itsAge(age)
{
	cout << "Animal的构造函数被调用...\n";
}
Animal::~Animal()
{
	cout << "Animal的析构函数被调用...\n";
}
class Mammal :public Animal
{
public:
	Mammal(int age) :Animal(age)
	{
		cout << "Mammal构造函数被调用...\n";
	}
	virtual ~Mammal() { cout << "Mammal析构函数被调用...\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 Speek() 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 Speek() 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 Speek() const { cout << "Whoof!...\n"; }
	virtual void Sleep() const { cout << "Dog snoring...\n"; }
	virtual COLOR GetItsColor() const { return itsColor; }
	virtual void Eat() const { cout << "Dog eating...\n"; }
	virtual void Move() const { cout << "Dog running...\n"; }
	virtual void Reproduce() const
	{
		cout << "Dog repeoducing...\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, Brown);
			break;
		case 2:
			pAnimal = new Horse(4, Black);
			break;
		case 3:
			pAnimal = new Fish(5);
			break;
		default:
			fQuit = true;
			break;
		}
		if (fQuit == false)
		{
			pAnimal->Speek();
			pAnimal->Eat();
			pAnimal->Reproduce();
			pAnimal->Move();
			pAnimal->Sleep();
			delete pAnimal;	//创建完的对象不用要删除
			cout << endl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值