
在C++中,结构和类基本相同。但也有一些细微的差别。这些差异如下所示。
默认情况下,类成员是私有的,但结构体的成员是公共的。让我们看看这两个代码,看看有什么不同。
网奇.NET网络商城系统下载系统优势: 1、 使用全新ASP.Net+c#和三层结构开发. 2、 可生成各类静态页面(html,htm,shtm,shtml和.aspx) 3、 管理后台风格模板自由选择,界面精美 4、 风格模板每月更新多套,还可按需定制 5、 独具的缓存技术加快网页浏览速度 6、 智能销售统计,图表分析 7、 集成国内各大统计系统 8、 多国语言支持,内置简体繁体和英语 9、 UTF-8编码,可使用于全球
示例
#includeusing namespace std; class my_class { int x = 10; }; int main() { my_class my_ob; cout << my_ob.x; }
输出
This program will not be compiled. It will generate compile time error for the private data member.
示例
#includeusing namespace std; struct my_struct { int x = 10; }; int main() { my_struct my_ob; cout << my_ob.x; }
输出
10
When we derive a structure from a class or structure, the default access specifier of that base class is public, but when we deriving a class the default access specifier is private.
示例
#includeusing namespace std; class my_base_class { public: int x = 10; }; class my_derived_class : my_base_class { }; int main() { my_derived_class d; cout << d.x; }
输出
This program will not be compiled. It will generate compile time error that the variable x of the base class is inaccessible
示例
#includeusing namespace std; class my_base_class { public: int x = 10; }; struct my_derived_struct : my_base_class { }; int main() { my_derived_struct d; cout << d.x; }
输出
10










