Problem Description定义哺乳动物类Mammal,再由此派生出狗类Dog,定义一个Dog类的对象
//你的程序将被嵌在这里
int main()
{
Dog d;
d.setAge(4);
d.setWeight(12);
d.setColor("Black");
cout<<d.getAge()<<endl;
cout<<d.getWeight()<<endl;
cout<<d.getColor()<<endl;
d.speak();return 0;
}
Sample OutputConstructor in Mammal. Constructor in Dog. 4 12 Black Dog sound wang,wang,wang! Destructor in Dog. Destructor in Mammal.Hint1、类Mammal有数据成员Age(年龄,int类型)、Weight(体重,double类型),和对应的set函数以及get函数。
2、Dog定义了新的数据成员Color(颜色,string类型)。
3、Mammal类和Dog类都有成员函数speak。
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
private:
int m_Age;
double m_Weight;
public:
Mammal()
{
cout << "Constructor in Mammal." << endl;
}
~Mammal()
{
cout << "Destructor in Mammal." << endl;
}
void setAge(int age)
{
m_Age = age;
}
int getAge()
{
return m_Age;
}
void setWeight(double weight)
{
m_Weight = weight;
}
double getWeight()
{
return m_Weight;
}
void speak()
{
}
};
class Dog : public Mammal
{
private:
string m_Color;
public:
Dog()
{
cout << "Constructor in Dog." << endl;
}
~Dog()
{
cout << "Destructor in Dog." << endl;
}
void setColor(string Color)
{
m_Color = Color;
}
string getColor()
{
return m_Color;
}
void speak()
{
cout << "Dog sound wang,wang,wang!" << endl;
}
};