IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++实操 - struct和class的区别 -> 正文阅读

[C++知识库]C++实操 - struct和class的区别

在C++编程中,使用struct和class都可以定义类。

在C++11标准中,11章开头写到:

使用class定义的类的成员,默认是private的,比如你写个构造函数,如果不加public访问修饰符,则构造函数为私有,就没法用这个构造函数创建对象。

使用struct或union定义的类的成员,默认是public的。

class X {

int a; // X::a is private by default

};

struct S {

int a; // S::a is public by default

};

11.2中写到,使用class或struct来定义子类时,如果对基类不加访问修饰符,则默认的访问修饰符是不同的。?

struct默认是public,class默认是private。


class B { /_ ... _/ };

class D1 : private B { /_ ... _/ };

class D2 : public B { /_ ... _/ };

class D3 : B { /_ ... _/ }; // B private by default

struct D4 : public B { /_ ... _/ };

struct D5 : private B { /_ ... _/ };

struct D6 : B { /_ ... _/ }; // B public by default

class D7 : protected B { /_ ... _/ };

struct D8 : protected B { /_ ... _/ };

在stackoverflow中查到相关解说:

The differences between a class and a struct in C++ is:

* struct members and base classes/structs are public by default.

* class members and base classes/struts are private by default.

Both classes and structs can have a mixture of public, protected and private members, can use inheritance and can have member functions.

I would recommend you:

* use struct for plain-old-data structures without any class-like features;

* use class when you make use of features such as private or protected members, non-default constructors and operators, etc.

让我们来写点代码体验一下。

test.cpp:

#include <stdio.h>

struct A
{
int n;
void printValue(){printf("n is %d.\n", n);}

};


int main()

{
??A a;
??a.n = 1;
??a.printValue();

??return 0;
}


$ g++ -o test test.cpp
$ ./test
n is 1.

使用struct关键字定义一个类,类中包含一个成员变量和成员函数,都是public的,可以直接访问。

将上例中test.cpp的代码中的class改成struct,编译,提示有错误:

$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13:5: error: ‘int A::n’ is private within this context
???13 |???a.n = 1;
??????|?????^
test.cpp:6:5: note: declared private here
????6 | int n;
??????|?????^
test.cpp:14:16: error: ‘void A::printValue()’ is private within this context
???14 |???a.printValue();
??????|????????????????^
test.cpp:7:6: note: declared private here
????7 | void printValue(){printf("n is %d.\n", n);}
??????|??????^~~~~~~~~~

使用class关键字则成员默认变成了私有,不能直接访问。添加public访问说明,则可以正常运行。

test.cpp

#include <stdio.h>

class A
{
public:
int n;
void printValue(){printf("n is %d.\n", n);}
};


int main()
{
??A a;
??a.n = 1;
??a.printValue();

??return 0;
}


$ g++ -o test test.cpp
$ ./test
n is 1.

改为struct关键字,添加private访问修饰符,也不能直接访问:

test.cpp

#include <stdio.h>

struct A
{
private:

int n;

public:

void printValue(){printf("n is %d.\n", n);}
};

int main()
{
??A a;
??a.n = 1;
??a.printValue();

??return 0;
}



$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:14:5: error: ‘int A::n’ is private within this context
???14 |???a.n = 1;
??????|?????^

test.cpp:6:5: note: declared private here
????6 | int n;
??????|?????^

然后试一下子类访问基类的权限:

test.cpp

#include <stdio.h>

struct A
{

int n;

void printValue(){printf("n is %d.\n", n);}
};

struct B : A
{

};

class C : A
{

};


int main()
{

??B b;
??C c;

??b.n = 1;
??b.printValue();

? c.n = 1;
??c.printValue();

??return 0;

}



$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:25:5: error: ‘int A::n’ is inaccessible within this context
???25 |???c.n = 1;
??????|?????^

test.cpp:5:5: note: declared here
????5 | int n;
??????|?????^

test.cpp:26:16: error: ‘void A::printValue()’ is inaccessible within this context
???26 |???c.printValue();
??????|????????????????^

test.cpp:6:6: note: declared here
????6 | void printValue(){printf("n is %d.\n", n);}
??????|??????^~~~~~~~~~

test.cpp:26:16: error: ‘A’ is not an accessible base of ‘C’
???26 |???c.printValue();
??????|????????????????^

我们看到使用struct定义的子类,可以访问基类成员,而使用class定义的子类不可以。

再试一下Union的使用:

union.cpp

#include <stdio.h>

union U
{

struct A
{

??int n;

??void printValue(){printf("n is %d.\n", n);}

} a;

void printUnion(){printf("Struct A.n value is %d.\n", a.n);}

private:

??int b;

};

int main()
{
??U u;
??u.a.n = 1;
??u.printUnion();

??return 0;

}


$ g++ -o union union.cpp ^C
$ ./union
Struct A.n value is 1.

使用union的话,也是默认为public访问权限,如果加上了private,就没法直接访问了。

gcc版本:

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.??There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE

参考:

oop - When should you use a class vs a struct in C++? - Stack Overflowicon-default.png?t=L892https://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-30 11:45:05  更:2021-09-30 11:47:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 21:51:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码