qfedu-cpp-level/day2/d13.cpp

45 lines
771 B
C++

// 类
#include <iostream>
#include <cstring>
using namespace std;
namespace SA
{
class Student
{
public:
int sid = 14; // 默认值设定,需要 c++11 标准以上
char name[32] = "hello";
private:
float score = 90;
public:
void showScore(); // 声明类方法
void addScore(float n) { this->score = n; }
};
// 实现类中的方法(函数)
void Student::showScore()
{
cout << "sid = " << this->sid << ", name = " << this->name << ", score = " << this->score << endl;
}
};
using namespace SA;
int main()
{
Student s1;
strcpy(s1.name, "flykhan");
s1.sid = 1001;
s1.addScore(100);
s1.showScore();
Student s2;
s2.showScore();
return 0;
}