c语言结构体形函数,C语言结构体类似函数吗?

396d5e45dd522a345aadd59557345137.png

九州编程

首先,图片太小。结构体类似C++里的类,但结构体与类有一个区别,就是类默认的成员是private,结构体默认的成员是public。结构体可以定义public、private、protected,可以定义static成员。可以定义构造和析构函数。也可以继承,也可以定义虚函数123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112#include "stdafx.h"typedef struct st_father{    int m_iValueOne;    st_father(){        printf("st_father 构造\r\n");        m_iValueOne = 10;    }    virtual ~st_father(){        printf("st_father 析构\r\n");    }    void st_father_public()    {        printf("st_father_public \r\n");    }    virtual void st_virtual()    {        printf("st_father st_virtual\r\n");    }    void TEST()    {        printf("st_father TEST\r\n");    }private:    int m_iValuePrivate;      static int m_iValueStatic;      void st_father_private()    {        printf("st_father_private \r\n");    }protected:    void st_father_protected()    {        printf("st_father_protected\r\n");    }  }ST_FATHER;  typedef struct st_son: public st_father{    st_son()    {        printf("st_son 构造\r\n");    }      ~st_son()    {        printf("st_son析构\r\n");    }      virtual void st_virtual()    {        printf("st_son st_virtual\r\n");    }      void TEST()    {        printf("st_son TEST\r\n");    }}ST_SON;    int main(int argc, char* argv[]){    ST_FATHER st_father;    ST_SON st_son;      ST_FATHER* p;      printf("st_father.m_iValueOne = %d\r\n", st_father.m_iValueOne);      //private成员不能访问,结构体默认是public,类默认是private    //printf("st.m_iValuePrivate = %d\r\n", st.m_iValuePrivate);      //结构体中也可以定义虚函数,也可以定义static变量    printf("sizeof(ST_FATHER) = %d\r\n", sizeof(ST_FATHER));      //外部也是只能调用public方法    st_father.st_father_public();    //st_father.st_father_protected();    //st_father.st_father_private();    st_father.st_virtual();    st_son.st_virtual();    p = &st_father;    p->st_virtual();    p = &st_son;    p->st_virtual();      return 0;}--------------------- 作者:guoyiyan1987 来源:CSDN 原文:https://blog.csdn.net/guoyiyan1987/article/details/80433432 版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值