C++ primer plus系列4——结构

    技术2024-12-21  12

    结构 是C++ oop堡垒(class)的基石,了解 结构 有助于我们学习oop 1- C和C++struct 的使用小差别

    struct inflatable hat;//key word struct is required in C inflatable vicent;//key word struce is not required in C++

    2-C++中使用struct

    #include<iostream> using namespace std; struct inflatable { char name[20]; float volume; double price; };//其中 每一条列表项都是一条声明 int main() { inflatable guest = { "glorious", 1.88, 29.9 }; inflatable pal = { "Audacious", 3.12, 32.99 }; cout << guest.name << " and " << pal.name <<" is good friend"<< endl; return 0; } //输出结果 // glorious and Audacious is good friend

    3-结构初始化/赋值

    struct inflatable { char name[20]; float volume; double price; };//其中 每一条列表项都是一条声明 //初始化 inflatable com={"compressor",25.0,16.3};//可以省略等号 //赋值 inflatable com1; com1=com;//赋值

    5-结构数组初始化

    inflatable guest[2] = { { "glorious", 1.88, 29.9 }, { "poor", 1.89, 20.9 } };

    6-结构中位字段(不理解)

    Processed: 0.010, SQL: 9