加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:05:52 所属栏目:百科 来源:网络整理
导读:权限的不同:class默认权限为private,struct默认权限为public。 #includeiostream using namespace std; class Student { string name; int age; double score;}; struct Teacher { void show() { cout " 姓名: " name endl; cout 年龄: " age endl; }};

权限的不同: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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读