c++和c结构体总结

来源:互联网 发布:php截取字符串前几位 编辑:程序博客网 时间:2024/05/20 20:20
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

原创粉丝点击