c++之结构体struct和类class的区别

前端之家收集整理的这篇文章主要介绍了c++之结构体struct和类class的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

权限的不同:class默认权限为private,struct默认权限为public。

#include<iostream>
using namespace std;

class Student {
    string name;
    int age;
    double score;
};
struct Teacher {
    void show() {
        cout << "姓名:" << name << endl;
        cout << 年龄:" << age << endl;
    }
};

 main() {
    Student s1;
    /* 此时这样访问会报错
    s1.name = "tom";
    s1.age = 12;
    s1.score = 99.0;
    */
    Teacher t1;
    t1.name = tom";
    t1.age = 45;
    t1.show();
    system(pause);
    return 0;
}

猜你在找的C&C++相关文章