diff --git a/README.md b/README.md index 60a8827..67357dd 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ - [x] [位域那些事](./bit) - [x] [extern那些事](./extern) - [x] [struct那些事](./struct) +- [x] [struct与class那些事](./struct_class) 代码运行: 全部在linux下用vim编写,使用gcc/g++调试!全部可正常运行! diff --git a/struct/ext b/struct/ext index d450b9f..45104f8 100755 Binary files a/struct/ext and b/struct/ext differ diff --git a/struct/ext_struct_func.cpp b/struct/ext_struct_func.cpp index 3c6af69..7ab1eb2 100644 --- a/struct/ext_struct_func.cpp +++ b/struct/ext_struct_func.cpp @@ -1,6 +1,6 @@ #include #include - +using namespace std; struct Base { int v1; // private: //error! @@ -10,10 +10,14 @@ struct Base { virtual void print(){ printf("%s\n","Base"); }; + Base(){cout<<"Base construct"<print(); + delete b; return 0; } diff --git a/struct_class/README.md b/struct_class/README.md index c8d304d..119e039 100644 --- a/struct_class/README.md +++ b/struct_class/README.md @@ -1,222 +1,19 @@ -# 一文搞懂C和C++中struct +# struct与class区别 -## 1.C中struct +## 关于作者: -- 在C中struct只单纯的用作数据的复合类型,也就是说,在结构体声明中只能将数据成员放在里面,而不能将函数放在里面。 -- 在C结构体声明中不能使用C++访问修饰符,如:public、protected、private 而在C++中可以使用。 -- 在C中定义结构体变量,如果使用了下面定义必须加struct。 -- C的结构体不能继承(没有这一概念)。 -- 若结构体的名字与函数名相同,可以正常运行且正常的调用!例如:可以定义与 struct Base 不冲突的 void Base() {}。 +个人公众号: -完整案例: +![](../img/wechat.jpg) -```c -#include +关于C与C++中struct内容:见[struct那些事](../struct) -struct Base { // public - int v1; -// public: //error - int v2; - //private: - int v3; - //void print(){ // c中不能在结构体中嵌入函数 - // printf("%s\n","hello world"); - //}; //error! -}; +总的来说,struct 更适合看成是一个数据结构的实现体,class 更适合看成是一个对象的实现体。 -void Base(){ - printf("%s\n","I am Base func"); -} -//struct Base base1; //ok -//Base base2; //error -int main() { - struct Base base; - base.v1=1; - //base.print(); - printf("%d\n",base.v1); - Base(); - return 0; -} -``` +区别: -最后输出: +最本质的一个区别就是默认的访问控制 -``` -1 -I am Base func -``` +默认的继承访问权限。struct 是 public 的,class 是 private 的。 -完整代码见:[struct_func.c](./struct_func.c) - -## 2.C++中struct - -与C对比如下: - -- C++结构体中不仅可以定义数据,还可以定义函数。 -- C++结构体中可以使用访问修饰符,如:public、protected、private 。 -- C++结构体使用可以直接使用不带struct。 -- C++继承 -- 若结构体的名字与函数名相同,可以正常运行且正常的调用!但是定义结构体变量时候只用用带struct的! - -例如: - -> 情形1:不适用typedef定义结构体别名 - -未添加同名函数前: - -```c++ -struct Student { - -}; -Student(){} -Struct Student s; //ok -Srudent s; //ok -``` - -添加同名函数后: - -```c++ -struct Student { - -}; -Student(){} -Struct Student s; //ok -Srudent s; //error -``` - -> 情形二:使用typedef定义结构体别名 - -```c++ -typedef struct Base1 { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}B; -//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名 -``` - -> 前三种案例 - -```c++ -#include -#include - -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}; - -int main() { - struct Base base1; //ok - Base base2; //ok - Base base; - base.v1=1; - base.v3=2; - base.print(); - printf("%d\n",base.v1); - printf("%d\n",base.v3); - return 0; -} -``` - -完整代码见:[struct_func.cpp](struct_func.cpp) - -> 继承案例 - -```c++ -#include -#include -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - virtual void print(){ - printf("%s\n","Base"); - }; -}; -struct Derived:Base { - - public: - int v2; - void print(){ - printf("%s\n","Derived"); - }; -}; -int main() { - Base *b=new Derived(); - b->print(); - return 0; -} -``` - -完整代码见:[ext_struct_func.cpp](./ext_struct_func.cpp) - -> 同名函数 - -```c++ -#include -#include - -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}; - -typedef struct Base1 { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}B; -void Base(){ - printf("%s\n","I am Base func"); -} -//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名 -int main() { - struct Base base; //ok - //Base base1; // error! - base.v1=1; - base.v3=2; - base.print(); - printf("%d\n",base.v1); - printf("%d\n",base.v3); - Base(); - return 0; -} -``` -完整代码见:[struct_func_func.cpp](./struct_func_func.cpp) - -## 3.总结 - -### C和C++中的Struct区别 - -| C | C++ | -| ------------------------------------------------------ | ------------------------------------------------------------ | -| 不能将函数放在结构体声明 | 能将函数放在结构体声明 | -| 在C结构体声明中不能使用C++访问修饰符。 | public、protected、private 在C++中可以使用。 | -| 在C中定义结构体变量,如果使用了下面定义必须加struct。 | 可以不加struct | -| 结构体不能继承(没有这一概念)。 | 可以继承 | -| 若结构体的名字与函数名相同,可以正常运行且正常的调用! | 若结构体的名字与函数名相同,使用结构体,只能使用带struct定义! | \ No newline at end of file +struct 作为数据结构的实现体,它默认的数据访问控制是 public 的,而 class 作为对象的实现体,它默认的成员变量访问控制是 private 的。 diff --git a/struct_class/ext b/struct_class/ext deleted file mode 100755 index d450b9f..0000000 Binary files a/struct_class/ext and /dev/null differ diff --git a/struct_class/ext_struct_func.cpp b/struct_class/ext_struct_func.cpp deleted file mode 100644 index 3c6af69..0000000 --- a/struct_class/ext_struct_func.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - virtual void print(){ - printf("%s\n","Base"); - }; -}; - -struct Derived:Base { - - public: - int v2; - void print(){ - printf("%s\n","Derived"); - }; -}; - -int main() { - Base *b=new Derived(); - b->print(); - return 0; -} diff --git a/struct_class/sf b/struct_class/sf deleted file mode 100755 index 321061d..0000000 Binary files a/struct_class/sf and /dev/null differ diff --git a/struct_class/stff b/struct_class/stff deleted file mode 100755 index de43727..0000000 Binary files a/struct_class/stff and /dev/null differ diff --git a/struct_class/struct_func b/struct_class/struct_func deleted file mode 100755 index 8541eba..0000000 Binary files a/struct_class/struct_func and /dev/null differ diff --git a/struct_class/struct_func.c b/struct_class/struct_func.c deleted file mode 100644 index 6bea643..0000000 --- a/struct_class/struct_func.c +++ /dev/null @@ -1,27 +0,0 @@ -#include - -struct Base { // public - int v1; -// public: //error - int v2; - //private: - int v3; - //void print(){ // c中不能在结构体中嵌入函数 - // printf("%s\n","hello world"); - //}; //error! -}; - -void Base(){ - printf("%s\n","I am Base func"); -} -//struct Base base1; //ok -//Base base2; //error - -int main() { - struct Base base; - base.v1=1; - //base.print(); - printf("%d\n",base.v1); - Base(); - return 0; -} diff --git a/struct_class/struct_func.cpp b/struct_class/struct_func.cpp deleted file mode 100644 index 09bef26..0000000 --- a/struct_class/struct_func.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}; - -int main() { - struct Base base1; //ok - Base base2; //ok - Base base; - base.v1=1; - base.v3=2; - base.print(); - printf("%d\n",base.v1); - printf("%d\n",base.v3); - return 0; -} diff --git a/struct_class/struct_func_func.cpp b/struct_class/struct_func_func.cpp deleted file mode 100644 index bce3749..0000000 --- a/struct_class/struct_func_func.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include - -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}; - -typedef struct Base1 { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}B; -void Base(){ - printf("%s\n","I am Base func"); -} -//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名 -int main() { - struct Base base; //ok - //Base base1; // error! - base.v1=1; - base.v3=2; - base.print(); - printf("%d\n",base.v1); - printf("%d\n",base.v3); - Base(); - return 0; -} diff --git a/struct_class/stu b/struct_class/stu deleted file mode 100755 index 6dabbe1..0000000 Binary files a/struct_class/stu and /dev/null differ