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; }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)