关于Struct 与 Class的区别(包括class 定义对象时候不能赋初值的方法何原因)
C++中struct和class的区别?
答:
(1)关于继承和访问权限,struct默认继承和访问权限均为public,class均为private;
(2)关于模版,在模版中,类型参数前面可以使用class或typename,不能使用struct。
答:
(1)关于继承和访问权限,struct默认继承和访问权限均为public,class均为private;
(2)关于模版,在模版中,类型参数前面可以使用class或typename,不能使用struct。
( 3 ) Struct 里面没有默认无参构造函数, 而Class里面有默认的无参构造函数
关于Struct 与 Class的区别(详细):链接:
http://blog.youkuaiyun.com/nocky/article/details/6195556
// class.cpp: 定义控制台应用程序的入口点。(可以运行)
#include "stdafx.h"
struct STUDENT {
int a;
int b;
char c;
};
int main()
{
//定义Struct结构体的对象方法
struct STUDENT a, b; //(C风格)
STUDENT c, d; //(C++风格)
//对象的赋初值方法,
STUDENT Student;
Student.a = 1;
Student.b = 2;
Student.c = 'a';
return 0;
}
//-----------------------------------------------------分割线-----------------------------------------------------------
注意:如果数据成员是私有的,或者类中有private 或 protected 的成员,就不能够使用下面这种方法初始化(如图):见谭浩强C++:p69-70
struct Time
{
int hour;
int minute;
int sec;
};
Time t1 = { 14, 56, 30 };
但是亲测在Win10专业版 VS2017版本里面测试编译不通过,
修正:原因已经找到了:没有在Class 或者 Student 里面没有写 public的(有多个参数的)构造函数~~~
//-----------------------------------------------------分割线-----------------------------------------------------------