cpp: create class

PigInfo.h

#ifndef PIGINFO_H
#define PIGINFO_H

#include <iostream>
#include<string.h>
#include<math.h>

using namespace std;

/*
实体类
https://learn.microsoft.com/zh-cn/cpp/standard-library/cpp-standard-library-header-files?view=msvc-170
https://learn.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=msvc-170
*/
class PigInfo
{
private:
	string PigName;
	int PigWeight;

public:
	void SetPigName(string pigName)
	{
		PigName = pigName;
	}

	void SetPigWeight(int pigWeight)
	{
		PigWeight = pigWeight;
	}

	string getPigName()
	{
		return PigName;
	}

	int getPigWeight()
	{
		return PigWeight;
	}
	//构造函数
	//PigInfo(string pigname, int pigweight);

	void ShowInfo();

};


/*
构造函数
PigInfo::PigInfo(string PigName, int PigWeight)
{
	//构造函数函数体内进行赋值操作
	this->PigName = PigName;
	this->PigWeight = PigWeight;
}*/
/*
*
*/
void PigInfo::ShowInfo()
{
	cout << "小猪名称:" << this->getPigName() << ",重量:" << this->getPigWeight() << endl;
	//cout << "" << this->getPigWeight() << endl;
}


#endif

  

// cppdemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<string.h>
#include<math.h>
#include<list>
#include "PigInfo.h"


using namespace std;

class Rectangle {
	int width, height;
public:
	void set_values(int, int);
	int area() { return width * height; }
};

void Rectangle::set_values(int x, int y) {
	width = x;
	height = y;
}




int main()
{
    //std::cout << "Hello World!涂聚文\n";

	/*Rectangle rect;
	rect.set_values(3, 4);
	cout << "area: " << rect.area();
    */
	list<PigInfo> pigInfoList;

	PigInfo piginfo;
	string name;
	int weight;

	//piginfo.SetPigName("geovindiu");
	//piginfo.SetPigWeight(25);
	for (int i = 1; i < 4; i++)
	{
		std::cout << "请输入第"<<i<<"只小猪的名称" << endl;
		cin >> name;
		std::cout << "请输入第"<<i<<"只小猪的重量" << endl;
		cin >> weight;
		piginfo.SetPigName(name);
		piginfo.SetPigWeight(weight);
		//piginfo.ShowInfo();
		pigInfoList.push_back(piginfo);

	}
	cout << "名称:" << "\t" << "重量:" << "\t" << endl;
	for (auto info : pigInfoList)
	{
		cout <<"\t\t"<< info.getPigName() << "\t"<<info.getPigWeight() << endl;
	}

	system("pause");
	return 0;
	

}

  

 

posted @ 2023-04-04 20:15  ®Geovin Du Dream Park™  阅读(17)  评论(0编辑  收藏  举报