문제 :

다음은 Phone 클래스이다.

1 2 3 4 5 6 7 8 9 10 11 class Phone { // 전화 번호를 표현하는 클래스 string name; string telnum; string address; public: Phone(string name="", string telnum="", string address="") { this->name = name; this->telnum = telnum; this->address = address; } };

Phone 클래스의 객체를 입출력하는 아래 코드와 실행 결과를 참조하여 «, » 연산자를 작성하고 Phone 클래스를 수정하는 등 프로그램을 완성하라.

1 2 3 Phone girl, boy; cin » girl » boy; cout « girl « endl « boy « endl;

실행 결과 :

목적 및 힌트 :

스트림 입출력 연산자( «, » ) 작성 연습

코드 :

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 #include  using namespace std; class Phone { // 전화 번호를 표현하는 클래스 string name; string telnum; string address; public: Phone(string name="", string telnum="", string address="") { this->name = name; this->telnum = telnum; this->address = address; } friend ostream& operator « (ostream& os, Phone p); // friend 작성 friend istream& operator » (istream& ins, Phone& p); // friend 작성, Phone& p 참조자 사용 }; ostream& operator « (ostream& os, Phone p) { os « “(” « p.name « “,” « p.telnum « “,” « p.address « “)”; return os; } istream& operator » (istream& ins, Phone& p) { cout « “이름:”; ins » p.name; cout « “전화번호:”; ins » p.telnum; cout « “주소:”; ins » p.address; return ins; } int main() { Phone girl, boy; cin » girl » boy; // 전화 번호를 키보드로부터 읽는다. cout « girl « endl « boy « endl; // 전화 번호를 출력한다. }

공유하기

게시글 관리

코딩은 내일부터 ;

저작자표시 비영리 (새창열림)

💻 개발 & CS > C++ 프로그래밍’ 카테고리의 다른 글

명품 C++ programming 실습 문제 11장 11번  (1) 2020.04.02
명품 C++ programming 실습 문제 11장 10번  (1) 2020.04.02
명품 C++ programming 실습 문제 11장 8번  (1) 2020.04.02
명품 C++ programming 실습 문제 11장 7번  (1) 2020.04.02
명품 C++ programming 실습 문제 11장 6번  (1) 2020.03.27