c++中的struct

时间:2023-03-09 23:06:29
c++中的struct

c++中的struct不在是c中的struct,不仅仅是一个多个数据类型的结构体了。c++中的struct可以具有成员函数(c语言中是不可以的),c++ struct还可以继承class等等。同时c++中的struct还兼容c的struct。下面这篇文章写得很详细

C++中struct和class的区别

我也贴一段小代码吧

 #include <iostream>
using namespace std; struct test_struct{
int id;
int age; void fun(){
cout<<"id = "<<id<<"age = "<<age<<endl;//这里定义了成员函数
}
}; int main(){
struct test_struct var1 = {, };
printf("%d %d\n", var1.id, var1.age); return ;
}

c++中的struct