#include <string>
using namespace std
class animal
{
public:
animal(string n){ name = n }
string GetName(){ return name }
virtual float GetWeight()= 0
private :
string name
}
class dog :public animal
{
public:
dog(float w, string n) :animal(n){ weight = w }
float GetWeight() { return weight }
private:
float weight
}
class cat : public animal
{
public:
cat(float w, string n) :animal(n){ weight = w }
float GetWeight() { return weight }
private:
float weight
}
void main()
{
dog d(2,"dog1")
cat c(1, "cat1")
cout << d.GetName() << ":" << d.GetWeight() << "KG" << endl
cout << c.GetName() << ":" << c.GetWeight() << "KG" << endl
}
class Dog{static int Dogs=0
public:
Dog() {++Dogs}
~Dog() {--Dogs}
static int GetDogs() {return Dogs}
}
其实这离能记录类的数量还差的远, 只要有个类重载它就会出一系列错误了。 具体看 Effective C++后面有一段讨论这个事
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)