声明一个Dog类,包含年龄和体重两个属性以及设置和显示其属性的操作方法,实现这个类,并在main函数中定义Dog类的一个对象,调用成员函数来测试这个类
#include <iostream>
using namespace std;
class Dog
{
private:
int age;
int weight;
public:
void setAge(int a){age = a;}
void setWeight(int w){weight = w;}
int getAge(){return age;}
int getWeight(){return weight;}
void disp()
{
cout << "年龄=" << age << ",体重=" << weight << "公斤"<< endl;
}
};
int main()
{
Dog d;
d.setAge(2);
d.setWeight(10);
d.disp();
return 0;
}