编写C++程序:声明一个动物类animal,再由此派生出狗类Dog和猫类Cat。

编写C++程序:声明一个动物类animal,再由此派生出狗类Dog和猫类Cat。,第1张

#include <iostream>

#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++后面有一段讨论这个事


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11618215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存