본문 바로가기

Language Proficiency/C++

Overloading Ostream Operator

The task is to overload the << operator for Person class in such a way that for p being an instance of class Person the result of:

std::cout << p << " " << <some_string_value> << std::endl;

produces the following output:

first_name=<first_name>,last_name=<last_name> <some_string_value>

where:

  • <first_name> is the value of p's first_name_
  • <last_name> is the value of p's last_name_
  • <some_string_value> is an arbitrary std::string value

Input Format

The input is read by the provided locked code template. In the only line of the input there are 3 space-separated strings first_namelast_nameevent. The values of first_name and last_name will be used to create an object p of type Person. The value of event will be used by the provided code to produce the output.

Constraints

  • Each word in the input contains only English letters and is no longer than 15 characters

Output Format

The output should be produced by the provided locked code template. This code will use the implementation of Person public methods and the overloaded << operator to produce the output. Specifically, the output wiil be produced by the following code:

cout << p << " " << event << endl;

Sample Input 0

john doe registered

Sample Output 0

first_name=john,last_name=doe registered






출력에 대한 연산자 오버로딩 문제이다.

ostream을 이용해서 << 연산자를 오버로딩 하는 것은 의외로 다양하게 응용할 수 있는 부분이 많기 때문에, ostream을 이용한 << 오버로딩은 쉽게 구현할 수 있을 정도로 익혀주자.




'Language Proficiency > C++' 카테고리의 다른 글

Cpp exception handling  (0) 2018.08.03
Messages Order  (0) 2018.08.03
Sets-STL  (0) 2018.08.03
Lower Bound-STL  (0) 2018.08.03
Strings  (0) 2018.08.03