ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

C++ - ÖØÔØI/O²Ù×÷·ûµÄ×¢Òâ(Ò»)
2013-11-20 14:24:42 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:523´Î
Tags£º  ÖØÔØ I/O ²Ù×÷ ·ûµÄ ×¢Òâ

¡¡¡¡1. Êä³ö²Ù×÷·û£¨ostream£©ÖØÔØ

¡¡¡¡º¯Êý£º std::ostream &operator¡¶ £¨std::ostream& os, const ClassA& ca£©£»

¡¡¡¡ostreamÐèÒªÐ޸ģ¬ ²»Äܸ´ÖÆ£¬ ËùÒÔÓ¦¸ÃΪ·Ç³£Á¿ÒýÓÃÀàÐÍ£¨nonconst &£©£» Êä³öÀ಻ÐèÒªÐ޸ģ¬ Ó¦¸ÃΪ³£Á¿ÒýÓÃÀàÐÍ£¨const &£©£»

¡¡¡¡º¯ÊýÓпÉÄÜʹÓÃÄÚ²¿µÄ˽ÓгÉÔ±£¬ ÐèÒª¶¨ÒåΪÓÑÔª£¨friend£©£»

¡¡¡¡ÖØÔزÙ×÷·ûÓ¦¸ÃΪ·ÇÀà³ÉÔ±º¯Êý£¨nonmember function£©¡£ Èç¹ûΪÀà³ÉÔ±º¯Êý£¬ ÔòÒ²±ØÐëΪ±ê×¼¿â³ÉÔ±º¯Êý£¬ ÏÔÈ»ÎÞ·¨Íê³É¡£

¡¡¡¡×¢Ò⺯Êý²»ÒªÓиñʽÐÅÏ¢£¨minimal formatting£©£¬ ΪÁ˺ͱê×¼ÊäÈë²Ù×÷·û½øÐÐͳһ¡£

¡¡¡¡2. ÊäÈë²Ù×÷·û£¨istream£©ÖØÔØ

¡¡¡¡º¯Êý£º std::istream &operator¡· £¨std::istream& is, ClassA& ca£©£»

¡¡¡¡»ù±¾Í¬Êä³ö²Ù×÷·û£»

¡¡¡¡²ÎÊý¶¼Îªnonconst, ¶¼ÐèÒªÐ޸ģ»

¡¡¡¡²Ù×÷·ûº¯ÊýÓ¦¸Ã°üÀ¨´íÎó»Ö¸´£¨error recovery£©£¬±£Ö¤ÊäÈë´íÎóʱ£¬ ²»»á²úÉúδ֪´íÎó£»

¡¡¡¡¿ÉÒÔÔö¼ÓI/OÌõ¼þ״̬£¨condition state£©½øÐÐÅжϣ¬ ÊäÈë´íÎóÔ­Òò¡£

¡¡¡¡´úÂëÈçÏ£¬ °üº¬ÒÔÉÏËùÓÐÌØÐÔ£¬ ×¢Òâ×¢ÊÍ£º

¡¡¡¡/*

¡¡¡¡* cppprimer.cpp

¡¡¡¡*

¡¡¡¡*  Created on: 2013.11.7

¡¡¡¡*

¡¡¡¡Author: Caroline

¡¡¡¡*/

¡¡¡¡#include <iostream>

¡¡¡¡#include <cstddef>

¡¡¡¡#include <utility>

¡¡¡¡class HighHeel {

¡¡¡¡friend std::ostream &operator¡¶ £¨std::ostream& os, const HighHeel& hh£©£»

¡¡¡¡friend std::istream &operator¡· £¨std::istream& is, HighHeel& hh£©£»

¡¡¡¡friend HighHeel operator+ £¨const HighHeel& lhs, const HighHeel& rhs£©£»

¡¡¡¡public:

¡¡¡¡int heel£¨£© const{

¡¡¡¡return £¨wedgeHeel + kittenHeel + stilettoHeel£©£»

¡¡¡¡}

¡¡¡¡int boot£¨£© const{

¡¡¡¡return £¨kinkyBoot + thighHighBoot£©£»

¡¡¡¡}   private:

¡¡¡¡int wedgeHeel = 2;

¡¡¡¡//ÆÂ¸úЬ

¡¡¡¡int kittenHeel = 2; //ÖиúЬ

¡¡¡¡int stilettoHeel = 2; //ϸ¸úЬ

¡¡¡¡int kinkyBoot = 5; //³¤Í²Å®Ñ¥

¡¡¡¡int thighHighBoot = 5; //¹ýÏ¥³¤Ñ¥

¡¡¡¡};

¡¡¡¡std::ostream &operator¡¶ £¨std::ostream& os, const HighHeel& hh£©

¡¡¡¡{

¡¡¡¡os ¡¶ "Heels: " ¡¶ hh.heel£¨£© ¡¶ " "

¡¡¡¡¡¶ "Boots: " ¡¶ hh.boot£¨£©£»

¡¡¡¡return os;

¡¡¡¡}

   

Ê×Ò³ ÉÏÒ»Ò³ 1 2 3 4 5 ÏÂÒ»Ò³ βҳ 1/5/5
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºc++ÖÐ*& ºÍ**& ·ûºÏ×÷.. ÏÂһƪ£ºC++Ö¸Ïòº¯ÊýµÄÖ¸Õë

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Linuxϵͳ¼ò½é (2025-12-25 21:55:25)
¡¤Linux°²×°MySQL¹ý³Ì (2025-12-25 21:55:22)
¡¤Linuxϵͳ°²×°½Ì³Ì£¨ (2025-12-25 21:55:20)
¡¤HTTP Åc HTTPS µÄ²î„ (2025-12-25 21:19:45)
¡¤ÍøÕ¾°²È«±ØÐ޿ΣºÍ¼ (2025-12-25 21:19:42)