c++定义一个dog类,包含age,weight等属性以及对这些属性操作的方法。实现并测试这个类?
2个回答
展开全部
#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;}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <cstdlib>
#include<iostream>
using namespace std;
class dog
{
private:
int m_age;
int m_weight;
public:
dog():m_age(0),m_weight(0){}
void setAge(int i){m_age=i;}
void setWeight(int i){m_weight=i;}
int age(){return m_age;}
int weight(){return m_weight;}
};
int main()
{
dog d;
cout<<d.age()<<" "<<d.weight()<<endl;
return 0;
}
#include<iostream>
using namespace std;
class dog
{
private:
int m_age;
int m_weight;
public:
dog():m_age(0),m_weight(0){}
void setAge(int i){m_age=i;}
void setWeight(int i){m_weight=i;}
int age(){return m_age;}
int weight(){return m_weight;}
};
int main()
{
dog d;
cout<<d.age()<<" "<<d.weight()<<endl;
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询