cc32b_demo-32dk2j_cpp_纯虚函数与抽象类2-txwtech
//纯虚函数是用来继承用的
//纯虚函数
//抽象类-抽象数据类型
//*任何包含一个或者多个纯虚函数的类都是抽象类
//*不要/不能创建这个类的对象,应该/只能继承它
//*务必覆盖从这个类继承的纯虚函数
//实现纯虚函数-----------可写可以不写
//C++接口
//就是只包含纯虚函数的抽象基类
//一个指向基类的指针可以指向它的派生类,指向它的子子孙孙
//通常在编程语句中用 abstract 修饰的类是抽象类。在C++中,含有纯虚拟函数的类称为抽象类,它不能生成对象;在java中,含有抽象方法的类称为抽象类,同样不能生成对象。
//抽象类是不完整的,它只能用作基类。在面向对象方法中,抽象类主要用来进行类型隐藏和充当全局变量的角色。
#include <iostream>//txwtech,cc32b_demo-纯虚函数与抽象类demo2
using namespace std;
//一个指向基类的指针可以指向它的派生类,指向它的子子孙孙
//通常在编程语句中用 abstract 修饰的类是抽象类。在C++中,含有纯虚拟函数的类称为抽象类,它不能生成对象;在java中,含有抽象方法的类称为抽象类,同样不能生成对象。
//抽象类是不完整的,它只能用作基类。在面向对象方法中,抽象类主要用来进行类型隐藏和充当全局变量的角色。
enum COLOR {Red,Green,Blue,Yellow,White,Black,Brown};
class Animal
{
public:
Animal(int);
virtual ~Animal() { cout << "Animal析构函数被调用...\n"; }
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 << "Mammal析构函数被调用..."; }
virtual void Reproduce() const
{
cout << "Mammal Reproduction depiacted...\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 << "dog Whoof...\n"; }
virtual void Sleep() const { cout << "dog Snoring...\n"; }
virtual void Eat() const { cout << "dog Eatting...\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,Brown);
break;
case 2:
pAnimal = new Horse(4, Black);
break;
case 3:
pAnimal = new Fish(5);
default:
fQuit = true;
break;
}
if (fQuit == false)
{
pAnimal->Speak();
pAnimal->Eat();
pAnimal->Reproduce();
pAnimal->Move();
pAnimal->Sleep();
delete pAnimal;
cout << endl;
}
}
//getchar();
return 0;
}