Your class should have a constructor that initializes the three data members.
조회수 522회
원서로 된 책에 있는 문제인데 무슨 의미인지 정확히 이해가 가지 않아서 질문드립니다 요약하자면 class 예제인데 탈것 클래스에 5개의 멤버변수를 주고 set,get함수를 각 변수에 만들어주고 메소드를 통해서 각 객체의 멤버변수를 출력하라는 문제입니다
그런데 문제중 저 구절이 의미하는게 제가 생각하기에는 constructor에 초기화시키는 변수를 딱 3개만 넣으라는 말같은데 그럼 나머지 두개는 어떻게하라는건지 말이 되지 않는 것 같아 제가 문제를 못알아먹는것같아 질문드립니다 ㅠㅠ
2 답변
-
Your class should have a constructor that initializes the three data members.
Constructor는 해당 Instance를 생성할 때에 실행되는 코드입니다.
나머지 두개의 변수는 어떻게 하라는건가? 그냥 생략하라는건가? 라는 것이 의문인 것 같아 그것에 초점을 맞추어서 대답합니다.
아마도 3개의 파라미터를 받는 Constructor를 만들라는 말 같습니다.
#include <iostream> #include <ctime> #include <string> using namespace std; class user{ private: int id; time_t create_date; string userID; string password; string email; public: user(string userID, string password, string email){ this -> userID = userID; this -> password = password; this -> email = email; this -> id; // auto increment 함수를 이용해서 할당.. this -> create_date; // 현재의 날짜를 받아와서 할당.. } void print(){ } }; int main() { }
나머지 2개의 멤버변수 같은 경우에는, 컴퓨터가 자동으로 할동하게끔 할수도 있고, 아니면 그냥 아무것도 할당 안할수도 있습니다.
option 같은 거라고 생각하면 될 것 같습니다.
-
댓글 입력