浅谈C++中的类Class(真的复杂啊) 您所在的位置:网站首页 class不是定义类的唯一关键字吗 浅谈C++中的类Class(真的复杂啊)

浅谈C++中的类Class(真的复杂啊)

2024-07-05 06:06| 来源: 网络整理| 查看: 265

每日疑问:为什么大学要用class而歧视struct呢? 而且这么困难的内容为什么要放到第三章??? 有哪位大佬可以给我讲明白吗?

简单入门

C++萌新: 什么是类(Class)? 怎么定义类? 类有没有什么高级操作呢?

面对这样的素质三联,C_T表示:不可描述,先举个栗子吧~

上栗子在这里插入图片描述

#include #include using namespace std; class Data{ public: void setname(string x) { name=x; } string getname() const { return name; } void printhello() const { cout public: void setname(string x) { name=x; } string getname() const { return name; } void printhello() const { cout name=x; } string getname() const { return name; } void printhello() const { cout public: explicit Data(string x) : name(x) {} string getname() const { return name; } void printhello() const { cout public: explicit Data(string x) : name(x) {} string getname() const { return name; } void printhello() const { cout public: explicit Data(std :: string x) : name(x) {} std :: string getname() const { return name; } void printhello() const { std :: cout public: explicit Data(std :: string x); //这里失去了 : a(x) 的部分 std :: string getname() const; void printhello() const; private: std :: string name; };

在createData.h中我们把所有大括号以及大括号内的内容隐去即可

//createData.cpp #include #include #include "createData.h" using namespace std; Data :: Data(string x) : name(x) {} //构造函数,失去了explicit,出现了Data:: string Data :: getname() const { return name; } void Data :: printhello() const { cout public: explicit GradeBook(std :: string); void setCourseName(std :: string); std :: string getCourseName() const; void displayMessage() const; private: std :: string courseName; }; //GradeBook.cpp #include #include #include "GradeBook.h" using namespace std; GradeBook :: GradeBook (string name) { setCourseName(name); } void GradeBook :: setCourseName(string name) { if (name.size()25) { courseName=name.substr(0,25); cerr cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有