Show Information
Coddyの「Userクラス - OOPプロジェクト」コースのレッスン 4/8。
チャレンジ
簡単User クラスに location プロパティを追加してください。このプロパティは private にし、アクセスするための2つのメソッドを追加します:
getLocation- location を返します。setLocation- 関数パラメータから新しい location を設定します。
locationのデフォルト値は"unset"にする必要があります。
自分で試してみよう
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"
int main() {
User u;
User_init(&u, "", "");
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\r\n")] = '\0';
char* cmd = strtok(line, " \t");
if (!cmd) continue;
if (strcmp(cmd, "new") == 0) {
char* un = strtok(NULL, " \t");
char* em = strtok(NULL, " \t");
User_init(&u, un ? un : "", em ? em : "");
}
if (strcmp(cmd, "username") == 0) { printf("%s\n", u.username); }
if (strcmp(cmd, "email") == 0) { printf("%s\n", u.email); }
if (strcmp(cmd, "setLocation") == 0) { char* v = strtok(NULL, " \t"); if (v) User_setLocation(&u, v); }
if (strcmp(cmd, "getLocation") == 0) { printf("%s\n", User_getLocation(&u)); }
}
return 0;
}