c++中的struct

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

C++中struct和class的区别

我也贴一段小代码吧

 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct test_struct{
 5     int id;
 6     int age;
 7 
 8     void fun(){
 9         cout<<"id = "<<id<<"age = "<<age<<endl;//这里定义了成员函数
10     }
11 };
12 
13 int main(){
14     struct test_struct var1 = {1, 24};
15     printf("%d %d\n", var1.id, var1.age);
16 
17     return 0;
18 }

posted on 2014-10-13 15:34  luckygxf  阅读(862)  评论(0编辑  收藏  举报

导航