cpp: create class

PigInfo.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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 @   ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示