C结构和C++结构体之间的区别

目录

一、C 和 C++ 结构之间的差异

二、C 和 C++ 结构之间的相似之处

1. 结构内部的成员函数 

2. 静态成员

3. 结构中的构造函数创建

4. 直接初始化

5. 使用struct关键字

6. 访问修饰符 

7. 指针和引用

8. sizeof 运算符

9. 数据隐藏


让我们讨论一下 C 中的结构和 C++ 中的结构有什么区别?在 C++ 中,结构类似于类。

一、C 和 C++ 结构之间的差异

C结构C++结构
只允许数据成员,不能有成员函数可以同时持有:成员函数和成员属性
不能有静态成员可以有静态成员
结构体内不能有构造函数允许创建构造函数
无法直接初始化数据成员可以直接初始化数据成员
必须编写“struct”关键字来声明结构类型变量声明结构类型变量不需要编写“struct”关键字
没有访问修饰符支持访问修饰符
只允许指向结构的指针可以同时具有对结构的指针和引用
Sizeof 运算符将为空结构生成 0Sizeof 运算符将为空结构生成 1
数据隐藏是不可能的数据隐藏是可能的

二、C 和 C++ 结构之间的相似之处

  • 在 C 和 C++ 中,默认情况下结构的成员具有公共可见性。

让我们一一讨论上述一些差异和相似之处:

1. 结构内部的成员函数 

 C 中的结构不能在结构内部具有成员函数,但 C++ 中的结构可以具有成员函数和数据成员。

// C程序
// C Program to Implement Member
// functions inside structure
  
#include <stdio.h>
  
struct marks {
    int num;
  
    // Member function inside Structure to
    // take input and store it in "num"
    void Set(int temp) { num = temp; }
  
    // function used to display the values
    void display() { printf("%d", num); }
};
  
// Driver Program
int main()
{
    struct marks m1;
    // calling function inside Struct to
    // initialize value to num
    m1.Set(9);
  
    // calling function inside struct to
    // display value of Num
    m1.display();
}

C 输出

// 这将在 C 中产生错误,但在 C++ 中不会产生错误。

// C++程序
// C++ Program to Implement Member functions inside
// structure
  
#include <iostream>
using namespace std;
  
struct marks {
    int num;
  
    // Member function inside Structure to
    // take input and store it in "num"
    void Set(int temp) { num = temp; }
  
    // function used to display the values
    void display() { cout << "num=" << num; }
};
  
// Driver Program
int main()
{
    marks m1;
  
    // calling function inside Struct to
    // initialize value to num
    m1.Set(9);
  
    // calling function inside struct to
    // display value of Num
    m1.display();
}

C++ 输出

num=9 

2. 静态成员

 C 结构不能有静态成员,但在 C++ 中是允许的。 

// C program with structure static member
  
struct Record {
    static int x;
};
  
// Driver program
int main() { return 0; }

这将在 C 中产生错误,但在 C++ 中不会。 

// C++ program with structure static member
  
struct Record {
    static int x;
};
  
// Driver program
int main() { return 0; }

3. 结构中的构造函数创建

C 中的结构不能在结构中具有构造函数,但 C++ 中的结构可以具有构造函数创建。

// C程序
// C program to demonstrate that
// Constructor is not allowed
  
#include <stdio.h>
  
struct Student {
    int roll;
    Student(int x) { roll = x; }
};
  
// Driver Program
int main()
{
    struct Student s(2);
    printf("%d", s.x);
    return 0;
}

C中的输出: 

这将在 C 中产生错误。

// CPP program to initialize data member in c++
#include <iostream>
using namespace std;
  
struct Student {
    int roll;
    Student(int x) { roll = x; }
};
  
// Driver Program
int main()
{
    struct Student s(2);
    cout << s.roll;
    return 0;
}

C++ 中的输出:

2

4. 直接初始化

我们不能在C中直接初始化结构数据成员,但我们可以在C++中进行。 

// C program to demonstrate that direct
// member initialization is not possible in C
  
#include <stdio.h>
  
struct Record {
    int x = 7;
};
  
// Driver Program
int main()
{
    struct Record s;
    printf("%d", s.x);
    return 0;
}

C中的输出: 

这将在 C 中产生错误。

// CPP program to initialize data member in c++
#include <iostream>
using namespace std;
​
struct Record {
    int x = 7;
};
  
// Driver Program
int main()
{
    Record s;
    cout << s.x << endl;
    return 0;
}

C++ 中的输出: 

7
 

5. 使用struct关键字

在C语言中,我们需要使用struct来声明一个struct变量。在 C++ 中,不需要结构。例如,让 Record 有一个结构。在 C 中,我们必须对 Record 变量使用“struct Record”。在 C++ 中,我们不需要使用 struct,只使用“Record”就可以了。

6. 访问修饰符 

C 结构没有访问修饰符,因为语言不支持这些修饰符。C++ 结构可以有这个概念,因为它是内置在语言中的。  

7. 指针和引用

在 C++ 中,C++ 中可以同时存在指向结构的指针和引用,但在 C 中只允许指向结构的指针。 

8. sizeof 运算符

该运算符将为 C 中的空结构生成0,而在 C++ 中为  空结构生成1 。

// C程序
// C program to illustrate empty structure
  
#include <stdio.h>
  
// empty structure
struct Record {
};
  
// Driver Code
int main()
{
    struct Record s;
    printf("%lu\n", sizeof(s));
    return 0;
}

C中的输出: 

0

// C++ program to illustrate empty structure
#include <iostream>
using namespace std;
  
// empty structure
struct Record {
};
  
// Driver program
int main()
{
    struct Record s;
    cout << sizeof(s);
    return 0;
}
  
// This code is contributed by Shubham Sharma

注意:   sizeof 的默认类型是 long unsigned int ,这就是为什么在 printf 函数中使用“%lu”而不是“%d”的原因。

9. 数据隐藏

 C 结构不允许数据隐藏的概念,但在 C++ 中是允许的,因为它是一种面向对象的语言,而 C 不是。 

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三贝勒文子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值