编写C++程序:声明一个动物类animal,再由此派生出狗类Dog和猫类Cat。
编写一个animal基类,继承该类,定义派生类dog,cat。基类包含动物名称字段name和获取该字段的函数getname,及虚函数getweight,派生类覆盖该函数,...
编写一个animal基类,继承该类,定义派生类dog,cat。基类包含动物名称字段name和获取该字段的函数getname,及虚函数getweight,派生类覆盖该函数,输出dog和cat自身的重量。在主函数main中,定义dog和cat对象,输出该对象的信息。
用C++程序编写。拜托哪位大神帮帮忙。 展开
用C++程序编写。拜托哪位大神帮帮忙。 展开
2个回答
展开全部
#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;
}
2018-04-14
引用yu321chang的回答:
#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;}
#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;}
展开全部
int main()吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询