c++定义一个dog类,包含age,weight等属性,以及对这些属性操作的方法。实现并测试这个类
3个回答
展开全部
#include <iostream>
using namespace std;
class dog
{
int age;
float weight;
string name;
public:
dog(string name, int age, float weight)
{
this->name = name;
this->age = age;
this->weight = weight;
}
int getAge()
{
return age;
}
float getWeight()
{
return weight;
}
string getName()
{
return name;
}
void changeName(string name)
{
this->name = name;
}
void setAge(int age)
{
this->age = age;
}
void setWeight(float w)
{
weight = w;
}
void bark()
{
cout<<"Woof woof!"<<endl;
}
void showInfo()
{
cout<<"Dog Info."<<endl;
cout<<"name\tage\tweight"<<endl;
cout<<getName()<<'\t'<<getAge()<<'\t'<<getWeight()<<endl;
}
};
int main()
{
dog dog1("Tom", 2, 5.3);
dog1.bark();
dog1.showInfo();
dog1.changeName("Mary");
dog1.setAge(3);
cout<<"After change the infomation of the dog"<<endl;
dog1.showInfo();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-31
展开全部
具体实例要给我看看,我才好回答。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询