CS106L Winter2025 Assignment3 Make a Class

关于CS106L的第三个作业的个人解法

遇到的问题

最后的编译过不去,不知道为什么卡在 Installing CastXML就不动了 :(

具体思路

自由发挥创造一个类,没什么好解释的,创造性很强,我没有更好的想法,就用了上课用的StanfordID类来做 class.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <sstream>

class StanfordID {
    private:
        std::string name;
        std::string sunet;
        int idNumber;

        std::string getInitialName(std::string name);
    
    public:
        StanfordID(std::string name, std::string sunet, int idNumber);
        StanfordID();

        const std::string getName();
        const std::string getSunet();
        const int getIdNumber();

        void setName(std::string name);
};

class.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "class.h"
#include <string>

StanfordID::StanfordID(std::string name, std::string sunet, int idNumber) {
    this->name = name;
    this->sunet = sunet;
    if (this->idNumber > 0) {
        this->idNumber = idNumber;
    }
}

std::string StanfordID::getInitialName(std::string name) {
    std::stringstream ss;
    ss << name;
    std::string first;
    std::string last;
    ss >> first >> last;
    std::string res;
    res += first[0];
    res += last[0];
    return res;
}

StanfordID::StanfordID() {
    this->name = "YuTaki X";
    this->sunet = "172@60";
    this->idNumber = 190;
}

const std::string StanfordID::getName() {
    return this->name;
}

const std::string StanfordID::getSunet() {
    return this->sunet;
}

const int StanfordID::getIdNumber() {
    return this->idNumber;
}

void StanfordID::setName(std::string name) {
    this->name = name;
}

sandbox.cpp

1
2
3
4
#include "class.h"
void sandbox() {
  StanfordID id("Yammy Xi", "183@80", 187);
}
不会摄影的药师不是好程序员
使用 Hugo 构建
主题 StackJimmy 设计