-->
当前位置:首页 > 题库 > 正文内容

Luz4年前 (2021-03-08)题库2187
6-3 类的声明和成员函数的实现 (10 分)

声明了一个Dog类,包含了age,weight等属性,以及对这些属性进行操作的方法。请实现该类的成员函数。

Dog类声明如下:

class Dog {public:    void setAge(int a);    int getAge();    void setWeight(int w);    int getWeight();private:    int age, weight;
};

请实现Dog类的成员函数。

裁判测试程序样例:

#include <iostream>using namespace std;class Dog {public:    void setAge(int a);    int getAge();    void setWeight(int w);    int getWeight();private:    int age, weight;
};int main(){
    Dog d;    int a, w;    cin >> a >> w;
    d.setAge(a);
    d.setWeight(w);    cout << d.getAge() << endl;    cout << d.getWeight() << endl;    return 0;
}/* 你的代码将被嵌在这里 */

输入样例:

1 3

输出样例:

1
3
作者
李廷元
单位
民用航空飞行学院
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
int Dog::getAge(){
    return age;
}
void Dog::setAge(int a){
    age=a;
}
void Dog::setWeight(int w){
    weight=w;
}
int Dog::getWeight(){
    return weight;
}