目录
让我们讨论一下 C 中的结构和 C++ 中的结构有什么区别?在 C++ 中,结构类似于类。
一、C 和 C++ 结构之间的差异
C结构 | C++结构 |
只允许数据成员,不能有成员函数 | 可以同时持有:成员函数和成员属性 |
不能有静态成员 | 可以有静态成员 |
结构体内不能有构造函数 | 允许创建构造函数 |
无法直接初始化数据成员 | 可以直接初始化数据成员 |
必须编写“struct”关键字来声明结构类型变量 | 声明结构类型变量不需要编写“struct”关键字 |
没有访问修饰符 | 支持访问修饰符 |
只允许指向结构的指针 | 可以同时具有对结构的指针和引用 |
Sizeof 运算符将为空结构生成 0 | Sizeof 运算符将为空结构生成 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 不是。