C++ defines a Dog class

Define a dog class, including age, weight attributes, and the operation methods for these attributes. Implement and test this class.

#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();	//输出

}

Guess you like

Origin blog.csdn.net/qq_53703628/article/details/115371278
Dog