C++学习笔记之结构

//结构是用户定义的类型,而结构声明定义了这种类型的数据属性, 

//定义了类型以后,便可以创建这种类型的变量,

//因些创建结构包括两步, 首先,定义结构描述————它描述并标记了能够存储在结构中的各种数居类型

//然后按描述创建结构变量(结构数据对像)

 

#include<iostream>

 

 

struct inflatable//声明一个结构,名为inflatable;

{

char name[20];//一个char类的数组,名为name,包涵20个字符。

float volume;//一个float型的变量,名为volume量

double price;//一个double型的变量,名为price价格

}shoes,T_shirt;//紧随其后创建两具变量;(很方便哦~~)

 

int main1()

{

using namespace std;

 

inflatable guest=

{

"Glorious Gloria",//name 逗号分隔

1.88,//volume

29.99//price

};

 

inflatable pal=

{

"Audacious Arthur",

3.12,

32.99

};

 

cout<<"Expand your guest list with: "<<guest.name;//guest.name是guest的成员

cout<<" and "<<pal.name<<"!/n";

cout<<"you can have both of $ ";

cout<<guest.price+pal.price<<"!/n";

 

return 0;

 

 

}

//————————————结构的的赋值----------------------

int jiegoufuzi()

{

using namespace std;

 

inflatable flowers=

{

"Rose",

0.20,

10.5

};

inflatable anotherFlowers;

 

cout<<"Flowers "<<flowers.name<<" for $ "<<flowers.price<<endl;

anotherFlowers=flowers;

cout<<"another flowers "<<anotherFlowers.name<<" for $ "<<anotherFlowers.price<<endl;

 

cout<<"if your want both , your must pay for "<<flowers.price+anotherFlowers.price<<endl;

 

return 0;

 

}

//---------------------结构数组---------------------------

//这个结构里面包含一个char name[20]的数组,也可以在数组里包含结构,,下面是演示

 

int jgsz()

{

using namespace std;

 

inflatable  LotsOfFlowers[2]=//创建了一个包含20个inflatable的数组;

{

{"Rose",0.20,10.5},

{"lily",0.30,12.5}

};//分别初始化两个成员

 

cout<<"这个数组里面有两种花,/n一种是 "<<LotsOfFlowers[0].name

<<"还有一种是 "<<LotsOfFlowers[1].name<<endl;

cout<<LotsOfFlowers[0].name<<"的价格是 "<<LotsOfFlowers[0].price<<endl;

cout<<LotsOfFlowers[1].name<<"的价格是 "<<LotsOfFlowers[1].price<<endl;

 

 

return 0;

}

//--------------------结构中的字位段---------------------

//C++允许指定占有特定位数的结构成员,(这使得创建与某个硬件设备上的寄存器对应的数结据结构成方便)

//字段的类型应该为整型或枚举。然后跟冒号:  然后上数字,(它指定了使用的位数)

 

int zwd()

{

using namespace std;

 

struct zwd

{

int SN:4;

int :4;//允许使用没有名称的字位段。

bool good: 1;

bool noGood:1;

};

 

zwd tr=//创建并初始化结构字位段变量

{

14,

true,

false

};//问题:没有名称的字位段没有被初始化。以后再解决~~~

 

return 0;

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值