在C++中,struct和class完全相同,除了struct默认为公共可见性和class默认为私有可见性。
C和C++结构之间的一些重要区别:
- 结构内部的成员函数:C中的结构不能在结构内部具有成员函数,但是C++中的结构可以与数据成员一起具有成员函数。
C
// C Program to Implement Member functions inside structure
#include
struct marks {
int num;
void
Set(int temp) // Member function inside Structure to
// take input and store it in "num"
{
num = temp;
}
void display() // function used to display the values
{
printf("%d", num);
}
};
// Driver Program
int main()
{
struct marks m1;
m1.Set(9); // calling function inside Struct to
// initialize value to num
m1.display(); // calling function inside struct to
// display value of Num
}
/* Error Occured
prog.c:18:4: error: ‘struct marks’ has no member named ‘Set’
m1.Set(9); // calling function inside Struct to
initialize value to num
^
prog.c:19:4: error: ‘struct marks’ has no member named
‘display’ m1.display(); // calling function inside struct to
display value of Num
*/
C++
// C++ Program to Implement Member functions inside
// structure
#include
using namespace std;
struct marks {
int num;
void
Set(int temp) // Member function inside Structure to
// take input and store it in "num"
{
num = temp;
}
void display() // function used to display the values
{
cout << "num=" << num;
}
};
// Driver Program
int main()
{
marks m1;
m1.Set(9); // calling function inside Struct to
// initialize value to num
m1.display(); // calling function inside struct to
// display value of Num
}
// This Code is Contributed by Samyak Jain
C
// C program to demonstrate that direct
// member initialization is not possible in C
#include
struct Record {
int x = 7;
};
// Driver Program
int main()
{
struct Record s;
printf("%d", s.x);
return 0;
}
/* Output : Compiler Error
6:8: error: expected ':', ', ', ';', '}' or
'__attribute__' before '=' token
int x = 7;
^
In function 'main': */
C++
// CPP program to initialize data member in c++
#include
using namespace std;
struct Record {
int x = 7;
};
// Driver Program
int main()
{
Record s;
cout << s.x << endl;
return 0;
}
// Output
// 7
C
// C program with structure static member
struct Record {
static int x;
};
// Driver program
int main()
{
return 0;
}
/* 6:5: error: expected specifier-qualifier-list
before 'static'
static int x;
^*/
C++
// C++ program with structure static member
struct Record {
static int x;
};
// Driver program
int main()
{
return 0;
}
C
// C program to demonstrate that Constructor is not allowed
#include
struct Student {
int roll;
Student(int x)
{
roll = x;
}
};
// Driver Program
int main()
{
struct Student s(2);
printf("%d", s.x);
return 0;
}
/* Output : Compiler Error
[Error] expected specifier-qualifier-list
before 'Student'
[Error] expected declaration specifiers or
'...' before numeric constant
[Error] 's' undeclared (first use
5555555555in this function)
In function 'main': */
C++
// CPP program to initialize data member in c++
#include
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;
}
// Output
// 2
C
// C program to illustrate empty structure
#include
// empty structure
struct Record {
};
// Driver program
int main()
{
struct Record s;
printf("%d\n", sizeof(s));
return 0;
}
C++
// C++ program to illustrate empty structure
#include
using namespace std;
// empty structure
struct Record{
};
// Driver program
int main() {
struct Record s;
cout<
这将在C中产生一个错误,但在C++中不会产生任何错误。
C++
// C++ Program to Implement Member functions inside
// structure
#include
using namespace std;
struct marks {
int num;
void
Set(int temp) // Member function inside Structure to
// take input and store it in "num"
{
num = temp;
}
void display() // function used to display the values
{
cout << "num=" << num;
}
};
// Driver Program
int main()
{
marks m1;
m1.Set(9); // calling function inside Struct to
// initialize value to num
m1.display(); // calling function inside struct to
// display value of Num
}
// This Code is Contributed by Samyak Jain
输出
num=9
- 直接初始化:我们无法在C中直接初始化结构数据成员,但可以在C++中完成。
C
// C program to demonstrate that direct
// member initialization is not possible in C
#include
struct Record {
int x = 7;
};
// Driver Program
int main()
{
struct Record s;
printf("%d", s.x);
return 0;
}
/* Output : Compiler Error
6:8: error: expected ':', ', ', ';', '}' or
'__attribute__' before '=' token
int x = 7;
^
In function 'main': */
C++
// CPP program to initialize data member in c++
#include
using namespace std;
struct Record {
int x = 7;
};
// Driver Program
int main()
{
Record s;
cout << s.x << endl;
return 0;
}
// Output
// 7
输出:
7
- 使用struct关键字:在C语言中,我们需要使用struct来声明一个struct变量。在C++中,没有必要使用struct。例如,让Record有一个结构。在C语言中,我们必须对记录变量使用“结构记录”。在C++中,我们不需要使用struct,而仅使用’Record’才可以。
- 静态成员: C结构不能具有静态成员,但C++允许使用。
C
// C program with structure static member
struct Record {
static int x;
};
// Driver program
int main()
{
return 0;
}
/* 6:5: error: expected specifier-qualifier-list
before 'static'
static int x;
^*/
C++
// C++ program with structure static member
struct Record {
static int x;
};
// Driver program
int main()
{
return 0;
}
这将在C中产生一个错误,但在C++中不会产生任何错误。
- 在结构中创建构造函数:C中的结构不能在结构内部具有构造函数,但是C++中的结构可以具有构造函数创建。
C
// C program to demonstrate that Constructor is not allowed
#include
struct Student {
int roll;
Student(int x)
{
roll = x;
}
};
// Driver Program
int main()
{
struct Student s(2);
printf("%d", s.x);
return 0;
}
/* Output : Compiler Error
[Error] expected specifier-qualifier-list
before 'Student'
[Error] expected declaration specifiers or
'...' before numeric constant
[Error] 's' undeclared (first use
5555555555in this function)
In function 'main': */
C++
// CPP program to initialize data member in c++
#include
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;
}
// Output
// 2
输出:
2
- sizeof运算符:此运算符将为C中的空结构生成0 ,而为C++中的空结构生成1 。
C
// C program to illustrate empty structure
#include
// empty structure
struct Record {
};
// Driver program
int main()
{
struct Record s;
printf("%d\n", sizeof(s));
return 0;
}
C++
// C++ program to illustrate empty structure
#include
using namespace std;
// empty structure
struct Record{
};
// Driver program
int main() {
struct Record s;
cout<
在C中的输出:
0
在C++中的输出:
1
- 数据隐藏: C结构不允许数据隐藏的概念,但在C++中是允许的,因为C++是一种面向对象的语言,而C语言则不是。
- 访问修饰符: C结构没有访问修饰符,因为语言不支持这些修饰符。由于C++结构是用语言内置的,因此可以具有此概念。
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。