首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++跟c结构体总结

2012-11-19 
c++和c结构体总结C中结构体的成员函数我们知道在C语言中,结构体中只能定义变量,而在c中,我们的结构体也可

c++和c结构体总结
C++中结构体的成员函数

      我们知道在C语言中,结构体中只能定义变量,而在c++中,我们的结构体也可以定义函数,而且还有成员函数。请看下面的程序:

译自www.heatpress123.net

#include
int main()
{
struct student;   //定义一个类
struct student
{
   int score;
   int id;
   student(){}
   inline student(int _score,int _id):score(_score),id(_id){}
   void Student()
   {
    printf("%d,%d\n",score,id);
   }
};
student s(1,2);
s.Student();
struct name
{
   int age;
   int i;
   name()      //可以被自动调用
   {
    age=0;
    i=2;
   }
   void n()
   {
    printf("%d,%d\n",age,i);
   }
}
name;
name.n();
return 0;
}
程序运行结果:
1,2

0,2

热点排行