Show Information
Coddy의 User 클래스 - OOP 프로젝트 코스 레슨 — 8개 중 4번째.
챌린지
쉬움User 클래스에 location 속성을 추가하세요. 이 속성은 private이어야 하며, 이에 접근하기 위한 두 개의 메서드를 추가하세요:
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;
}