您的位置:首页 > 编程语言 > C语言/C++

C++ 结构体struct的使用

2016-03-17 21:45 141 查看



C++基础:C++的结构体struct

#include <iostream>
#include <string>

using namespace std;

/*在c++中struct和类的区别在于struct不能有方法,所有成员是public的*/
struct Student/*可以指定类型名也可以不指定*/
{
//成员都是public的
int ID;
string Name;
} stu1; //可以在声明struct的时候声明一个struct实例

int main(){
//movie变量在Movie结构声明处定义了
stu1.ID = 100;
stu1.Name = "Kri";

cout<<"stu1.ID = "<<stu1.ID<<endl;
cout<<"stu1.Name = "<<stu1.Name<<endl;

//声明一个变量m,无须为赋初值就可以使用它了
Student stu2;
stu2.ID = 101001;
stu2.Name = "GDragon";
cout<<"stu2.ID="<<stu2.ID<<endl;
cout<<"stu2.Name="<<stu2.Name<<endl;

//声明结构的指针
Student * mp;
mp = &m;
//在指针中调用成员时要用->符号,mp->ID等价于(*mp).ID
cout<<"mp->ID = "<<mp->ID<<endl;
cout<<"mp->Name = "<<mp->Name<<endl;

}

是不是好简单啊?!!,





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: