c++和c结构体总结

2012-12-17 12:59:26 · 作者: · 浏览: 295

    C++(www.cppentry.com)中结构体的成员函数

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

    #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