This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

白话C++编程系列之十七 结构体

mosjin(25)
Published in
#cpp
Words
1719
Reading
8 min
Listen
Play
9y

联盟-矮人
上篇《白话C++编程系列之十六 调试篇》, 我们一起学习了C++中是如何进行调试(抓虫子的).
gdb: 抓住日子的一张图在调侃的文字

For a fish, the archer fish is known to shoot down bugs from low hanging plants by spitting water at them.
对于鱼类来说,已知射手鱼通过向其吐水来击落低矮植物的虫子。

现在来回顾一下吧!_
调试(debug)

调试 就是借助断点或者日志等方式来 de-bug, 消灭掉程序中的 bug.

这样程序运行更健康!
具体的概念,方法和如何践行, 请您再次再翻开《白话C++编程系列之十六 调试篇》看看,加深印象.

今天我们一起来学习, C++中面向对象的一种将 数据 和 操作数据的方法 打包 在一起的数据结构.

这里说的数据结构, 就类似一个收纳盒或者一台冰箱, 它们的里面可装东西, 水果蔬菜, 而且收纳盒和冰箱本身起着容纳,保鲜的作用.

收纳盒中的东西或者冰箱中的水果或肉类, 就可以想像成C++中的数据, 收纳和保鲜则是施加到这些数据上的操作方法.

通常来说, 数据一般为名词, 操作为动词.

之前我在我的白话C++编程系列中也说过一个我最推崇的编程原则之一:
一处定义原则.(请参见知识星球一处定义原则)

其实, 面向对象就是一处定义原则的深刻体现.
面向对象(OO: Orential object)核心是将数据和对数据的操作打包在一个 结构体(struct) 或 类(class) 中.

面向对象是软件行业中少有的革命性进步和思想, 是软件工程发展的飞跃和福音.
每当您在越来越大的项目中进行软件开发时, 您的体会会越来越深!

每当您对比面向过程的那种一锅粥开发方式时, 您会知道面向对象有多么的优雅, 完美和让您心动!

1. 结构体是啥?


结构体 一个包含数据和对其数据进行操作的数据结构.

数据
就像上面举的例子, 水果,蔬菜,肉都是数据.

操作
收纳, 保存, 保鲜 等等, 都是施加到 数据 上的操作.

数据结构
冰箱, 收纳盒 就是一个结构. 里面放(包)着数据( 水果, 牛奶, 蔬菜等...)

C++中的结构体是指:
struct 这个关键字

比如 定义一个 007 的结构体:

struct str007
{
    //空的结构体 
}

定义一个复杂一点儿 007结构体: 只有数据

struct stru007
{
    std::string _strName;       //名字
    std::string _strStuff;      //写作
    std::string _strWeb;            //网站
    std::string _strWOA;            //微信公众号

};

再来一个更丰富多彩的007.
数据和操作都有了._


struct stru007
{
    std::string _strName;       //名字
    std::string _strStuff;      //写作
    std::string _strWeb;            //网站
    std::string _strWOA;            //微信公众号

    // 构造函数
    stru007()
    {
        name( "007" );
        stuff( "写作" );
        web( "http://buchuju.net" );
        woa( "不出局" );
    }

    //析构函数 
    ~stru007()
    {}

public:
    std::string name();
    void name( std::string const & strName );

    std::string stuff();
    void stuff( std::string const &strStuff );

    std::string web();
    void web( std::string const & strWeb );

    std::string woa();
    void woa( std::string const & strWoa );

    void print();
    void print( std::string const & strTip, std::string const & strValue );
};

2. 结构体的例子(举例)?


在结构体是啥中,我们已经给它的定义和例子,现在来个更完整的例子...

当然现在struct007这个结构体还在不断成长壮大, 因此期待您的丰富的声明和定义!
让struct007这个结构体更精彩,更完整,更壮大!...

//这次给出声明和具体实现

struct stru007
{
    std::string _strName;       //名字
    std::string _strStuff;      //写作
    std::string _strWeb;            //网站
    std::string _strWOA;            //微信公众号

    // 构造函数
    stru007()
    {
        name( "007" );
        stuff( "写作" );
        web( "http://buchuju.net" );
        woa( "不出局" );
    }

    //析构函数 
    ~stru007()
    {}

public:
    std::string name()
    {
        return _strName;
    }

    void name( std::string const & strName )
    {
        _strName = strName;
    }

    std::string stuff()
    {
        return _strStuff;
    }

    void stuff( std::string const &strStuff )
    {
        _strStuff = strStuff;
    }

    std::string web()
    {
        return _strWeb;
    }

    void web( std::string const & strWeb )
    {
        _strWeb = strWeb;
    }

    std::string woa()
    {
        return _strWOA;
    }

    void woa( std::string const & strWoa )
    {
        _strWOA = strWoa;
    }

    void print()
    {
        std::cout << std::endl;
        print( "name", name() );
        print( "stuff", stuff() );
        print( "web", web() );
        print( "woa", woa() );
    }

    void print( std::string const & strTip, std::string const & strValue )
    {
        std::cout << strTip << ":" << strValue << std::endl;
    }
};

007结构体程序运行图:
007struct运行结果图

3. 践行struct


动手实践吧!
现在打开您安装的vs2013( or open http://cpp.sh)敲入文章中上面的代码, 或您想声明,定义的结构体.

按F5或者点击网站上的run按钮, 欣赏下您的劳动成果吧. 哈哈!
恭喜您, 学会和践行了C++中最重要的 面向对象方法和其杰出代表: struct

完整的代码如下:

// D:\dev\jinLab\p17_struct\p17_struct\p17_struct.cpp
//  调试篇:

/*
@Author: Albert 小宁
@Web: http://jinLab.com
@WeChatOfficalAccount: 小宁静致远
@Date: 2018-03-12
@KS: https://t.xiaomiquan.com/IeieiEm
*/


#include 
#include 

struct stru007
{
    std::string _strName;       //名字
    std::string _strStuff;      //写作
    std::string _strWeb;            //网站
    std::string _strWOA;            //微信公众号

    // 构造函数
    stru007()
    {
        name( "007" );
        stuff( "写作" );
        web( "http://buchuju.net" );
        woa( "不出局" );
    }

    //析构函数 
    ~stru007()
    {}

public:

    std::string name()
    {
        return _strName;
    }

    void name( std::string const & strName )
    {
        _strName = strName;
    }

    std::string stuff()
    {
        return _strStuff;
    }

    void stuff( std::string const &strStuff )
    {
        _strStuff = strStuff;
    }

    std::string web()
    {
        return _strWeb;
    }

    void web( std::string const & strWeb )
    {
        _strWeb = strWeb;
    }

    std::string woa()
    {
        return _strWOA;
    }

    void woa( std::string const & strWoa )
    {
        _strWOA = strWoa;
    }

    void print()
    {
        std::cout << std::endl;
        print( "name", name() );
        print( "stuff", stuff() );
        print( "web", web() );
        print( "woa", woa() );
    }

    void print( std::string const & strTip, std::string const & strValue )
    {
        std::cout << strTip << ":" << strValue << std::endl;
    }

};

int  main( int argc, char * argv[ ] )
{
    std::cout << argc << " argv: " << argv;

    for ( int i = 0; i < argc; ++i )
        std::cout << "i: " <<  i << " argv:" << argv[ i ];

    //声明007结构体对象
    stru007 s007;

    //调用007结构体'打印'方法
    s007.print();

    // 修改007结构体内部的数据
    s007.name( "零零七" );
    s007.stuff( "七天一篇写七年!" );
    s007.web( "http://www.buChuJu.net" );
    s007.woa( "向左而生" );
    s007.print();
    

    return 0;
}

代码图1
代码图2
代码图3

怎么样? 很简单吧?
Define your OWN struct!

塞纳里奥角鹰兽宝宝



小结:


结构体是啥?

结构体 一个包含数据和对其数据进行操作的数据结构.

结构体(例子)
践行结构体

Any ideas?请随时在文章留言区留言或者直接联系我 _




白话C++编程系列之:下期预告


白话C++编程系列之十八: 类
* 1. 类是啥?
* 2. 类咋用?
* 3. 践行类.


延伸 相关阅读


gdb官方网站: gdb official website: https://www.gnu.org/software/gdb/

相信好运,自然好运!

Learn archer fish!





《白话C++编程系列》


《白话C编程系列之: C入门与实践大纲》

《白话C++编程系列之一: 本质篇》

《白话C++编程系列之二: 变量篇》

《白话C++编程系列之三: 类型篇》

《白话C++编程系列之四: 语句篇》

《白话C编程系列之五: C是啥?》

《白话C++编程系列之六: 表达式(语句之爱恨)》

《白话C++编程系列之七: 函数篇》

《白话C++编程系列之八: 文件篇》

《白话C++编程系列之九: 多个文件篇》

《白话C++编程系列之加餐一: IDE工具篇》

《白话C++编程系列之十: 入口函数篇》

《白话C++编程系列之十一 规范篇》

《白话C++编程系列之十二 操作符篇》

《白话C++编程系列之十三 关键字-保留字篇》

《白话C++编程系列之十三 关键字-保留字篇》

《白话C++编程系列之十四 文件操作篇》

《白话C++编程系列之十五 输入输出篇》

《白话C++编程系列之十六 调试篇》








Wanna learn English in USA? Click this link:
Talk English in USA: https://t.xiaomiquan.com/EuBqfMb

区块链投资/板砖/挖矿: https://t.xiaomiquan.com/iQV37MB

Robinson Crusoe|鲁滨逊漂流记-Chapter 6

孩子们不能错过的经典动画片“小猪佩奇”

请大家多多动手,让编程也成为您的一种爱好和生产力。
请关注我的微信公众号 小宁静致远 或扫码关注我, 欢迎、感激传播! _
期待和您多交流、共成长。谢谢!
扫描关注小宁静致远
精灵实验室? http://jinLab.com
留言区就是您的天下,欢迎吐槽、拍砖!

Why NOT let programming a skill of you?
Please follow my Wechat Official Account 小宁静致远 or scan the 2D code above to follow me & thanks for sharing me to your friends! _
Hope we can communicate each other & improve ourselves together. Thanks!
JinLab? http://jinLab.com
Any ideas? Please add comments to the article!

学习C? Lear C? 请加入我的知识星球: Join my Knowledge Space:
白话C编程:C入门与实践 https://t.xiaomiquan.com/IeieiEm
oral C++ programming and practice! https://t.xiaomiquan.com/IeieiEm
加入白话C++ programming
不让好机会溜走,飞向白话C++编程知识星球!不让好机会溜走白话C++编程知识星球

鼓励我的创作? Encourage me?
您的鼓励我的动力! Appreciate your praise!

白话C++编程系列之十七 结构体 | Ecency