当前位置:首页 » 《关注互联网》 » 正文

❤️学妹竟然问struct和class的区别❤️,我一篇文讲的明明白白!_Linux猿

13 人参与  2021年07月25日 07:03  分类 : 《关注互联网》  评论

点击全文阅读



🎈 作者:Linux猿

🎈 简介:CSDN博客专家🏆,C/C++、面试、刷题、算法尽管咨询我,关注我,有问题私聊!

🎈 关注专栏:C/C++面试通关集锦(优质好文持续更新中……)🚀


绝大多数的人对 struct 和 class 都是很熟悉的,那它们之间有什么区别呢?我想很多人并没有深入的了解过这个,这篇文章就来分析一下!

首先,注意本文讨论的是 C++ 中 struct 和 class 的区别,因为 C 中 struct 和 class 的区别已经很明显了!

先说下 C++ 中 struct 相比于 C 中增加了哪些功能。

(1)struct 中可以使用 public、private、protected等属性,和 C++ 一样。

例如:

#include <iostream>
using namespace std;

struct Node {
    public:
        int x;
    private:
        int y;
    protected:
        int z;

};

int main() {
    Node obj;
    obj.x = 10;
    cout<<"obj.x = "<<obj.x<<endl;
}

编译后输出结果为:

linuxy@linuxy:~/structClass$ g++ main.cpp -o main
linuxy@linuxy:~/structClass$ ./main 
obj.x = 10
linuxy@linuxy:~/structClass$

从例子中可以看到 struct 在 C++ 中是可以添加 public、private、protected的。在纯 c 语言环境下是会出错的。

(2)struct 中可以添加方法

例如:

#include <iostream>
using namespace std;

struct Node {
    public:
        int add(int x, int y) {
            return x + y;
        }
};

int main() {
    Node obj;
    cout<<"add 10 + 20 = "<<obj.add(10, 20)<<endl;
}

编译后输出结果为:

linuxy@linuxy:~/structClass$ g++ main.cpp -o main
linuxy@linuxy:~/structClass$ ./main 
10 + 20 = 30
linuxy@linuxy:~/structClass$

(3)struct 可以继承等属性

例如:

#include <iostream>
using namespace std;

struct Parent {
    int left;
    int right;
};

struct Node : Parent{
    public:
        int x;
};

int main() {
    Node obj;
    obj.left = 10;
    cout<<"obj.left = "<<obj.left<<endl;
}

编译后输出结果为:

linuxy@linuxy:~/structClass$ g++ main.cpp -o main
linuxy@linuxy:~/structClass$ ./main 
obj.left = 10
linuxy@linuxy:~/structClass$

从上面可以看到,在C++中,struct 已经和 class 基本相同了。那为什么还要保留 struct 呢,因为 C++ 是要兼容 C的,毕竟是 c plus plus 。

那下面来具体看看 struct 和 class 有哪些不同:

(0)定义上不同

struct 是各种数据类型的组合,是一种复合数据类型,class 是一个对象的方法和属性的集合,更注重数据的安全性。

(1)默认的访问属性不同

struct 默认的访问属性是 public,class 默认的访问属性是 private

例子:

#include <iostream>
using namespace std;

struct Node {
    int x;
};

class Student {
    int y;
};

int main() {
   Node obj1;
   Student obj2;
   cout<<"obj1 x = "<<obj1.x<<endl;
   cout<<"obj2 y = "<<obj2.y<<endl;
}

编译后会出错,如下所示:

linuxy@linuxy:~/structClass$ g++ main.cpp -o main
main.cpp: In function ‘int main()’:
main.cpp:16:28: error: ‘int Student::y’ is private within this context
   16 |    cout<<"obj2 y = "<<obj2.y<<endl;
      |                            ^
main.cpp:9:9: note: declared private here
    9 |     int y;
      |         ^
linuxy@linuxy:~/structClass$

可以看到,obj1.x 的使用并没有出错,而 obj2.y 出错,显示该变量是私有的。

(2)默认的继承方式不同

struct 默认的继承方式是 public,class 默认的继承方式是 private。

#include <iostream>
using namespace std;

struct Parent1 {
    int w;
};

class Parent2 {
    int z;
};

struct Node : Parent1 {
    int x;
};

class Student : Parent2 {
    int y;
};

int main() {
   Node obj1;
   Student obj2;
   cout<<"obj1 x = "<<obj1.w<<endl;
   cout<<"obj2 y = "<<obj2.z<<endl;
}

编译后出错,如下所示:

linuxy@linuxy:~/structClass$ g++ main.cpp -o main
main.cpp: In function ‘int main()’:
main.cpp:24:28: error: ‘int Parent2::z’ is private within this context
   24 |    cout<<"obj2 y = "<<obj2.z<<endl;
      |                            ^
main.cpp:9:9: note: declared private here
    9 |     int z;
      |         ^
linuxy@linuxy:~/structClass$

可以看到,obj2.z 是私有的。

总结

在 C++ 中通常更推荐使用 class,数据安全性更高,struct 一般是作为不同类型的合集。


🎈 欢迎小伙伴们点赞👍、收藏⭐、留言💬



点击全文阅读


本文链接:http://zhangshiyu.com/post/24048.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1