4-8

定义一个Dog 类,包含了age, weight等属性,以及对这些属性操作的方法。实现并测试这个类。

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 class Dog{
 6 private:
 7     int age;
 8     int weight;
 9 public:
10     Dog(int age, int weight){
11         this->age = age;
12         this->weight = weight;
13     }
14     int getAge(){
15         return this->age;
16     }
17     int getWeight(){
18         return this->weight;
19     }
20     void setAge(int age){
21         this->age = age;
22     }
23     void setWeight(int weight){
24         this->weight = weight;
25     }
26     
27 };
28     
29 int main(){
30     Dog dog(5,20);
31     cout<<"dog's age is "<<dog.getAge()<<endl<<"dog's weight is "<<dog.getWeight()<<endl;
32     dog.setAge(10);
33     dog.setWeight(30);
34     cout<<"dog's age is "<<dog.getAge()<<endl<<"dog's weight is "<<dog.getWeight()<<endl;
35     return 0;
36 }

 

posted @ 2023-03-12 17:19  nlkdfgnvfdkl  阅读(92)  评论(0)    收藏  举报