1. 程式人生 > >結構體:struct 和 typedef struct應該以及結構體的建構函式 區別

結構體:struct 和 typedef struct應該以及結構體的建構函式 區別

主要記錄 struct 和 typedef struct的筆記

# include <iostream>
using namespace std;
struct Student
{
    int age;
}stu1;
int main()
{
    stu1.age = 25;
    cout << stu1.age << endl;
    system("pause");
}
# include <iostream>
using namespace std;
typedef struct Student
{
    int
age; }stu2; int main() { stu2 L; L.age = 25; cout << L.age << endl; system("pause"); }

使用時可以直接訪問stu1.a
但是stu2則必須先 stu2 s2;

結構體和類的區別

參考博文
結構體 建構函式的實現

# include <iostream>
using namespace std;
typedef struct Student
{
    Student()
    {
        age = 0;
        high = 0
; }; Student(int age1, double high1) { age = age1; high = high1; } int age; double high; }stu1; int main() { stu1 L; cout << L.high << endl; system("pause"); }