九州编程
首先,图片太小。结构体类似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 版权声明:本文为博主原创文章,转载请附上博文链接!