小标
2018-12-26
来源 :
阅读 812
评论 0
摘要:本文主要向大家介绍了 C/C++知识点之struct在C和C++中的使用总结,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了 C/C++知识点之struct在C和C++中的使用总结,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
主要理解一下两点:1.在C和C++中struct的常规使用。
2.在C++中struct和class基本一致,除了在访问控制权限方面,即:
通过struct关键字实现的类,属性,函数默认的访问权限为public; 通过class关键字实现的类,属性,函数默认的访问权限为private。
下面举例说明:
#include
using namespace std;
struct point{
int x;
int y;
int fun(point &p) //在C++中,完全可以在struct中使用函数
{
p.x = 100;
p.y = 200;
return 0;
}
point(int x, int y): x(x) , y(y){ } //等同于C++中的class
};
struct teacher{
int age;
char *name;
};
int main(void){
point p(0,0);
p.x = 1;
p.y = 2;
point p1= p;
cout<<"p1.x= p1.y="<<p1.y<<endl;<br/><br/> point *p3 = &p1;<br/> p3->x = 10;<br/> p3->y = 20;<br/> cout<<" p3-="">x="<
cout<<"p3->y="<
p3->fun(p1);
cout<<"p3->x="<
cout<<"p3->y="<
point p4(1000,2000);
cout<<"p4.x= p4.y="<<p4.y<<endl;<br/><br/> cout<<" before="" define="" struct="" teacher="" after="" t1.age="<<t1.age<<endl;<br/> cout<<" t1.name="<<t1.name<<endl;<br/><br/> struct teacher *t2 = &t1;<br/> t2->age = 35;<br/> t2->name = " t2-="">age="<
cout<<"t2->name="<
system("pause");
return 0;
}
输出结果:
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 9.5px Helvetica; color: #008f00 }
span.Apple-tab-span { white-space: pre }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号