Designing a Dog class with a constructor

Topic content:

Design a Dog class, including properties such as name, age, sex, and weight, and initialize the data members in the constructor with parameters.

Public member functions are: GetName(), GetAge(), GetSex() and GetWeight() to get name, age, gender and weight. Write the member function speak() to display the barking of the dog. Write the main function, input the dog's name, age, gender and weight; declare the Dog object and use the input data to initialize the object through the constructor, and obtain the dog's properties through member functions and display them.


Input format:

dog information


Output format:

Dog information, plus barking


Input sample:

Tom 4 m 2.4


Sample output:

Tom

4

m

2.4

Arf!Arf!


Time limit: 500ms Memory limit: 32000kb
#include<iostream>
#include<cstring>
using namespace std;
class Dog
{
	char name[10];
	int age;
	char sex;
	float weight;
public:
	Dog(char *name_,int age_,char sex_,float weight_)
	{
		strcpy(name,name_);
		age=age_;
		sex=sex_;
		weight=weight_;
	}
	char *GetName() {return name;}
	int GetAge() {return age;}
	char GetSex() {return sex;}
	float GetWeight() {return weight;}
	void speak()
	{
		cout<<"Arf!Arf!"<<endl;
	}
};
intmain()
{
	char name[10];
	int age;
	char sex;
	float weight;
	
	cin>>name>>age>>sex>>weight;
	Dog dog(name,age,sex,weight);
	cout<<dog.GetName()<<endl;
	cout<<dog.GetAge()<<endl;
	cout<<dog.GetSex()<<endl;
	cout<<dog.GetWeight()<<endl;
	dog.speak();
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325369094&siteId=291194637