C++定义一个Dog类

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

#include <iostream>
using namespace std;

class Dog{
    
    
	private:
		int age;
		int weight;
	public:
		void setterAge(int age) {
    
    
			this->age = age;
		}
		void setterWeight(int weight) {
    
    
			this->weight = weight;
		}

		void getter() {
    
    
            cout << "Age = " << this->age << "   Weight = " << this->weight << endl;
		}

};

int main() {
    
    
	Dog dog;	//实例化一个对象,叫做dog
	dog.setterAge(18);	//调用方法,给私有属性年龄赋值
	dog.setterWeight(85);	//同上

	dog.getter();	//输出

}

猜你喜欢

转载自blog.csdn.net/qq_53703628/article/details/115371278