#include<iostream>
using namespace std;
class Dog
{
public:
Dog(int age=0,int weight=5);
~Dog();
int getAge(){ return _age; }
int getWeight(){ return _weight; }
void setAge(int age){ _age = age; }
void setWeight(int weight){ _weight = weight; }
private:
int _age, _weight;
};
Dog::Dog(int age, int weight)
{
_age = age;
_weight = weight;
}
Dog::~Dog() {}//析构函数,不做任何工作
int main()
{
Dog Tom, Jack(2, 10);
cout << "when Tom born,it is " << Tom.getAge() << " years old and he is " << Tom.getWeight() << " pounds weight." << endl;
cout << "Jack is a dog who is " << Jack.getAge() << " years old and he is " << Jack.getWeight() << " pounds weight." << endl;
Jack.setAge(7);
Jack.setWeight(20);
cout << "Now Jack is " << Jack.getAge() << " years old and " << Jack.getWeight() << " pounds weight." << endl;
return(0);
}
C++ Dog类
最新推荐文章于 2023-09-21 16:41:10 发布