【题目】定义一个Dog类,包含age,weight等属性,以及对这些属性的操作方法。实现并测试这个类。
【代码】
#include <iostream>
using namespace std;
class Dog{
private:
int age,weight;
public:
void input();
void output();
};
void Dog::input(){
cout << "Input the age and weight:";
cin >> age >> weight;
}
void Dog::output(){
cout << "Dog age:" << age << "year" << endl;
cout << "Dog weight:" << weight << "kg"<< endl;
}
int main(){
Dog D;
D.input();
D.output();
return 0;
}
【结果】