»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
 °²È«¼¼Êõ¼¼ÊõÎĵµ
  ¡¤ °²È«ÅäÖÆ
  ¡¤ ¹¤¾ß½éÉÜ
  ¡¤ ºÚ¿Í½Ìѧ
  ¡¤ ·À»ðǽ
  ¡¤ Â©¶´·ÖÎö
  ¡¤ ÆƽâרÌâ
  ¡¤ ºÚ¿Í±à³Ì
  ¡¤ ÈëÇÖ¼ì²â
 °²È«¼¼ÊõÂÛ̳
  ¡¤ °²È«ÅäÖÆ
  ¡¤ ¹¤¾ß½éÉÜ
  ¡¤ ·À»ðǽ
  ¡¤ ºÚ¿ÍÈëÇÖ
  ¡¤ Â©¶´¼ì²â
  ¡¤ Æƽⷽ·¨
  ¡¤ É±¶¾×¨Çø
 °²È«¼¼Êõ¹¤¾ßÏÂÔØ
  ¡¤ É¨Ã蹤¾ß
  ¡¤ ¹¥»÷³ÌÐò
  ¡¤ ºóÃÅľÂí
  ¡¤ ¾Ü¾ø·þÎñ
  ¡¤ ¿ÚÁîÆƽâ
  ¡¤ ´úÀí³ÌÐò
  ¡¤ ·À»ðǽ
  ¡¤ ¼ÓÃܽâÃÜ
  ¡¤ ÈëÇÖ¼ì²â
  ¡¤ ¹¥·ÀÑÝʾ
¼¼ÊõÎĵµ > VCÎĵµ > ÆäËûÎĵµ
C++µÄiostream±ê×¼¿â½éÉÜ(3)
·¢±íÈÕÆÚ£º2005-04-15 23:27:18×÷Õߣº¹ ³ö´¦£º  

¡¡¡¡CÓïÑÔÌṩÁ˸ñʽ»¯ÊäÈëÊä³öµÄ·½·¨£¬C++ҲͬÑù£¬µ«ÊÇC++µÄ¿ØÖÆ·ûʹÓÃÆðÀ´¸üΪ¼òµ¥·½±ã£¬ÔÚc++ÏÂÓÐÁ½Öз½·¨¿ØÖƸñʽ»¯ÊäÈëÊä³ö¡£
¡¡¡¡£±.ÓÐÁ÷¶ÔÏóµÄ³ÉÔ±º¯Êý¡£
¡¡¡¡ÀýÈ磬ÏÂÁгÌÐòÒÔ³ÉÔ±º¯ÊýµÄ·½Ê½¿ØÖÆÊä³öµÄ¾«¶È:

//³ÌÐò×÷Õß:¹ÜÄþ 
//Õ¾µã:www.cndev-lab.com 
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß 
 
#include <iostream
using namespace std;   
   
int main()    
{   
    float pi=3.14159f; 
    cout<<pi<<endl; 
    cout.precision(2); 
    cout<<pi<<endl; 
    system("pause");   
}

¡¡¡¡£².ʹÓÃC++ÊäÈëÊä³ö¿ØÖÆ·û£¬¿ØÖÆ·ûÊÇÔÚÍÏÎļþiomanip.hÖж¨ÒåµÄ¶ÔÏó£¬Óë³ÉÔ±º¯ÊýÓÐÒ»ÑùµÄЧ¹û£¬¿ØÖÆ·û²»±ØÏñ³ÉÔ±º¯ÊýѧÄÇÑùµ¥¶Àµ÷Óã¬Ëü¿ÉÒÔÖ±½Ó²åÈëÁ÷ÖÐʹÓá£
¡¡¡¡ÀýÈ磬ÏÂÁгÌÐòÒÔ¿ØÖÆ·ûµÄ·½Ê½¿ØÖÆÊä³öµÄ¾«¶È:

//³ÌÐò×÷Õß:¹ÜÄþ 
//Õ¾µã:www.cndev-lab.com 
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß 
 
#include <iostream
#include <iomanip
using namespace std;   
   
int main()    
{   
    float pi=3.14159f; 
    cout<<pi<<endl; 
    cout<<setprecision(4); 
    cout<<pi<<endl; 
    system("pause");   
}

¡¡¡¡Ï±íÎÒÃÇÁгöÁËһЩ±È½Ï³£ÓõĿØÖÆ·ûºÅ£¬ÓÉÓÚƪ·ùÓÐÏÞ¶ÁÕßÇë¸ù¾Ý×Ô¼ºµÄÐèÒª²éÔÄÏà¹ØÊé¼®£º

¡¡¡¡¶ÔÓÚiostream±ê×¼¿âÀ´Ëµ°üº¬ÁËÖÚ¶àµÄ³ÉÔ±º¯Êý£¬¸÷º¯Êý¶¼ÓÐÆä×ÔÉíµÄ×÷Óã¬Æª·ùÎÊÌâ±ÊÕßÔÚÕâÀï²»ÄÜһһ˵Ã÷Àý¾Ù£¬ÓÉÓÚ±ê×¼ÊäÈë¶ÔÏócinÌṩÊäÈëµÄʱºò»á×Ô¶¯ÒÔ¿Õ¸ñ×÷Ϊ·Ö½ç£¬¸øÎÒÃÇ»ñÈ¡Ò»ÐдøÓпոñµÄÍêÕû×Ö·û´®´øÀ´ÁËÀ§ÄÑ£¬ÔÚÕâÀï²¹³äÒ»¸ö·Ç³£ÓÃÓеijÉÔ±º¯Êý£­£­£­£­getline()¡£

¡¡¡¡Æ亯ÊýÔ­ÐÍΪ£º
¡¡¡¡getlin(chiar *str,int size,char='\n');

¡¡¡¡µÚÒ»¸ö²ÎÊýÊÇ×Ö·ûÊý×飬ÓÃÓÚ´æ·ÅÕûÐÐÎı¾£¬µÚ¶þ¸ö²ÎÊý¶ÁÈ¡µÄ×î´ó×Ö·û¸öÊý£¬µÚÈý¸ö²ÎÊýΪ×÷Ϊ·Ö½ç½çÏÞµÄ×Ö·û£¬Ä¬ÈÏʶÊÇ\n£¬»»Ðзû¡£

¡¡¡¡Ê¾Àý´úÂëÈçÏ£º

//³ÌÐò×÷Õß:¹ÜÄþ 
//Õ¾µã:www.cndev-lab.com 
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß 
 
#include <iostream
#include <iomanip
using namespace std;   
   
int main()    
{   
    char str[100]; 
    cin.getline(str,sizeof(str),'\n'); 
    cout<<str<<endl; 
    system("pause");   
}

¡¡¡¡Í¨¹ýÉÏÃæÄÚÈݵÄѧϰ£¬ÎÒÃǶÔi/oÓÐÁËһЩ»ù±¾µã»ù±¾µÄÈÏʶ£¬ÏÖÔÚÊǸÃÇÐÈëÕýÌâµÄʱºòÁË£¬ÏêϸѧϰһÏ£¬ÈçºÎÖØÔØ×óÒÆÓëÓÒÒƲÙ×÷·û¡£

¡¡¡¡ÏÈ˵×óÒÆ£¨<<£©²Ù×÷·û£¬Ò²¾ÍÊÇÎÒÃdz£ËµµÄÊä³ö²Ù×÷·û¡£
¡¡¡¡¶ÔÓÚ×Ô¶¨ÒåÀàÀ´Ëµ£¬ÖØÔØ×óÒƲÙ×÷·ûµÄ·½·¨ÎÒÃdz£Ê¹ÓÃÀàµÄÓÑÔª·½Ê½½øÐвÙ×÷¡£


¡¡¡¡Ê¾Àý´úÂëÈçÏ£º

//³ÌÐò×÷Õß:¹ÜÄþ 
//Õ¾µã:www.cndev-lab.com 
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß 
 
#include <iostream
using namespace std; 
 
class Test 

    public
        Test(int age = 0,char *name = "\0") 
        { 
            Test::age = age; 
            strcpy(Test::name,name); 
        } 
        void outmembers(ostream &out) 
        { 
            out<<"Age£º"<<age<<endl<<"Name£º"<<this->name<<endl; 
        } 
        friend ostreamoperator <<(ostream& ,Test&); 
    protected
        int age; 
        char name[50]; 
}; 
ostreamoperator <<(ostream& out,Test &temp) 

    temp.outmembers(out); 
    return out; 

int main()    

    Test a(24,"¹ÜÄþ"); 
    cout<<a; 
    system("pause"); 
}

¡¡¡¡ÉÏÀý´úÂëÖУ¬ÎÒÃǶÔvoid outmembers(ostream &out)µÄ²ÎÊýʹÓÃostream¶¨ÒåÖ÷ÒªÊÇΪÁË¿ÉÒÔÏòËü´«µÝÈκÎostreamÀà¶ÔÏó²»¹âÊÇcoutÒ²¿ÉÒÔÊÇofstrem»òÕßÊÇostrstreamºÍostringstreamÀà¶ÔÏó£¬×öµ½Í¨ÓÃÐÔ¡£

¡¡¡¡ÖØÔØÔËËã·û£¬ÎÒÃÇÖªµÀ¿ÉÒÔÊǷdzÉÔ±·½Ê½Ò²¿ÉÒÔÊdzÉÔ±·½Ê½µÄ£¬¶ÔÓÚ<<À´ËµÍ¬ÑùÒ²¿ÉÒÔÊdzÉÔ±·½Ê½£¬µ«ÎÒÊ®·Ö²»ÍƼöÕâô×ö£¬ÒòΪ¶ÔÓÚÀàµÄ³ÉÔ±º¯ÊýÀ´Ëµ£¬µÚÒ»¸ö²ÎÊýʼÖÕÊǻᱻÒþ²ØµÄ£¬¶øÇÒÒ»¶¨Êǵ±Ç°ÀàÀàÐ͵ġ£

¡¡¡¡ÏÂÃæµÄʾÀý´úÂë¾ÍÊǽ«ÉÏÃæµÄ<<ÖØÔغ¯ÊýÐ޸ijɳÉÔ±·½Ê½µÄ×ö·¨£º

//³ÌÐò×÷Õß:¹ÜÄþ 
//Õ¾µã:www.cndev-lab.com 
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß 
 
#include <iostream>  
using namespace std; 
 
class Test 

    public
        Test(int age = 0,char *name = "\0") 
        { 
            Test::age = age; 
            strcpy(Test::name,name); 
        } 
        void outmembers(ostream &out) 
        { 
            out<<"Age£º"<<age<<endl<<"Name£º"<<this->name<<endl; 
        } 
        ostreamoperator <<(ostream &out) 
        { 
            this->outmembers(out); 
            return out; 
        } 
    protected
        int age; 
        char name[50]; 
}; 
int main()    

    Test a(24,"¹ÜÄþ"); 
    a<<cout
    system("pause"); 

¡¡¡¡´Ó´úÂëʵÏÖÉÏ£¬ÎÒÃǽ«º¯ÊýÐ޸ijÉÁËostream& operator <<(ostream &out)£¬ÆȲ»µÃÒѽ«ostreamÀàÐ͵ÄÒýÓòÎÊý·Åµ½Á˺óÃ棬ÕâÊÇÒòΪ£¬³ÉÔ±·½Ê½ÔËËã·ûÖØÔغ¯ÊýµÚÒ»¸ö²ÎÊý»á±»Òþ²Ø£¬¶øÇÒÒ»¶¨Êǵ±Ç°ÀàÀàÐ͵ģ¬ÕâºÍostreamÀàÐͳåÍ»ÁË¡£ÓÉ´ËÎÒÃÇÔÚʹÓÃcoutÊä³öµÄʱºò¾Í±ØÐëд³Éa<<cout;£¬ÕâÑùÒ»À´´úÂëµÄ¿É¶ÁÐоʹó´ó½µµÍÁË£¬Õâµ½µ×ÊÇ×óÒÆ»¹ÊÇÓÒÒÆÄØ£¿Îª´ËÎÒÔÙÒ»´Î˵Ã÷£¬¶ÔÓÚ×óÒƺÍÓÒÒÆÔËËã·ûµÄÖØÔØÊÇÊ®·Ö²»ÍƼöʹÓóÉÔ±º¯ÊýµÄ·½Ê½±àдµÄ¡£

¡¡¡¡ÎªÁ˹®¹Ìѧϰ£¬ÏÂÃæÎÒÃÇÒÔfstream¶ÔÏóÊä³öΪÀý×öÒ»¸öÁ·Ï°¡£

¡¡¡¡´úÂëÈçÏ£º

//³ÌÐò×÷Õß:¹ÜÄþ 
//Õ¾µã:www.cndev-lab.com 
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß 
#include <iostream
#include <fstream
using namespace std;   
   
class Test   
{   
    public:   
        Test(int age = 0,char *name = "\0")   
        {   
            Test::age = age;   
            strcpy(Test::name,name);   
        }   
        void outmembers(ostream &out)   
        {   
            out<<"Age£º"<<age<<endl<<"Name£º"<<this->name<<endl;   
        }   
        friend ostreamoperator <<(ostream& ,Test&);   
    protected:   
        int age;   
        char name[50];   
};   
ostreamoperator <<(ostream& out,Test &temp)   
{   
    temp.outmembers(out);   
    return out;   
}   
int main()      
{   
    Test a(24,"¹ÜÄþ"); 
    ofstream myfile("c:\\1.txt",ios::out,0); 
    if (myfile.rdstate() == ios_base::goodbit) 
    { 
        myfile<<a; 
        cout<<"Îļþ´´½¨³É¹¦£¬Ð´ÈëÕý³££¡"<<endl; 
    } 
    if (myfile.rdstate() == ios_base::badbit) 
    { 
        cout<<"Îļþ´´½¨Ê§°Ü£¬´ÅÅÌ´íÎó£¡"<<endl; 
    } 
    system("pause");  
}

¡¡¡¡¶ÔÓÚ×óÒÆÔËËã·ûÖØÔغ¯ÊýÀ´Ëµ£¬ÓÉÓÚ²»ÍƼöʹÓóÉÔ±·½Ê½£¬ÄÇôʹÓ÷dzÉÔ±·½Ê½ÔÚÀàÓжàÖؼ̳еÄÇé¿öÏ£¬¾Í²»ÄÜʹÓÃÐ麯Êý½øÐÐ×óÒÆÔËËã·ûÖØÔصÄÇø·Ö£¬ÎªÁË´ïµ½Äܹ»Çø·ÖÏÔʾµÄÄ¿µÄ£¬¸øÿ¸öÀà·Ö±ðÌí¼Ó²»Í¬µÄÐ麯ÊýÊDZØÒªµÄ¡£

¡¡¡¡Ê¾Àý´úÂëÈçÏ£º

//³ÌÐò×÷Õß:¹ÜÄþ   
//Õ¾µã:www.cndev-lab.com   
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß   
 
#include <iostream
#include <fstream
using namespace std;   
   
class Student   
{   
    public:   
        Student(int age = 0,char *name = "\0")   
        {   
            Student::age = age;   
            strcpy(Student::name,name);   
        }   
        virtual void outmembers(ostream &out) = 0; 
        friend ostreamoperator <<(ostream& ,Student&);   
    protected:   
        int age;   
        char name[50];   
};   
ostreamoperator <<(ostream& out,Student &temp)   

    temp.outmembers(out);   
    return out;   

class Academician:public Student 

    public
        Academician(int age = 0,char *name = "\0",char *speciality="\0"):Student(age,name)  
        { 
            strcpy(Academician::speciality,speciality); 
        } 
        virtual void outmembers(ostream &out)   
        {   
            out<<"Age£º"<<age<<endl<<"Name£º"<<name<<endl<<
            "speciality£º"<<speciality<<endl;   
        } 
    protected
        char speciality[80]; 
}; 
class GraduateStudent:public Academician 

    public
        GraduateStudent(int age = 0,char *name = "\0",char *speciality="\0",
        char *investigate="\0"):Academician(age,name,speciality) 
        { 
            strcpy(GraduateStudent::investigate,investigate); 
        } 
        virtual void outmembers(ostream &out)   
        {   
            out<<"Age£º"<<age<<endl<<"Name£º"<<name<<endl<<
            "speciality£º"<<speciality<<endl<<"investigate£º"<<investigate<<endl;   
        } 
    protected
        char investigate[100]; 
}; 
int main() 
{   
    Academician a(24,"¹ÜÄþ","Computer Science"); 
    cout<<a; 
    GraduateStudent b(24,"ÑÏÑàÁá","Computer Science","GIS System"); 
    cout<<b; 
    system("pause");  
}

¡¡¡¡ÔÚÉÏÃæµÄ´úÂëÖÐΪÁËÄܹ»Çø·ÖÊä³öa¶ÔÏóÓëb¶ÔÏó£¬ÎÒÃÇÓÃÐ麯ÊýµÄ·½Ê½ÖØÔØÁ˼̳ÐÀàAcademicianÓë¶àÖؼ̳ÐÀàGraduateStudentµÄoutmembers³ÉÔ±º¯Êý£¬ÓÉÓÚostream& operator <<(ostream& out,Student &temp) ÔËËã·ûÖØÔغ¯ÊýÊÇStudent»ùÀàµÄ£¬Student &temp²ÎÊýͨ¹ýÐ麯ÊýµÄ¶¨Òå¿ÉÒÔÊÊÓ¦²»Í¬ÅÉÉúÀà¶ÔÏó£¬ËùÒÔÔÚÆäÄÚ²¿µ÷ÓÃtemp.outmembers(out); ϵͳ¿Éʶ±ð²»Í¬¼ÌÀàµÄoutmembers()³ÉÔ±º¯Êý¡£

¡¡¡¡×îºó¿´Ò»Ï£¬ÓÒÒÆÔËËã·ûµÄÖØÔØ£¬ÓÒÒÆÔËËã·ûÎÒÃÇÒ²³£½ÐËüÊäÈëÔËËã·ûºÅ£¬¶ÔÓÚËüÀ´Ëµ£¬¾ßÌåʵÏÖºÍ×óÒÆÔËËã·ûµÄÖØÔزî±ð²¢²»´ó£¬¶ÔÓÚÓжà³ÉÔ±¶ÔÏóµÄÀàÀ´Ëµ£¬Ö»Òª±£Ö¤Äܹ»ÍêÕûÊäÈë¸÷³ÉÔ±¶ÔÏó´óÊý¾Ý¾Í¿ÉÒÔÁË¡£

¡¡¡¡Ê¾ÀýÈçÏ£º

//³ÌÐò×÷Õß:¹ÜÄþ   
//Õ¾µã:www.cndev-lab.com   
//ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß   
   
#include <iostream>   
using namespace std;   
   
class Test   
{   
    public:   
        Test(int age = 0,char *name = "\0")   
        {   
            Test::age = age;   
            strcpy(Test::name,name);   
        }   
        void inputmembers(istream &out)   
        { 
            cout<<"please input age£º"; 
            cin>>Test::age; 
            cout<<"please input name£º"; 
            cin>>Test::name; 
        }   
        friend istreamoperator >>(istream& ,Test&);   
    public:   
        int age;   
        char name[50];   
};   
istreamoperator >>(istream& input,Test &temp)   
{   
    temp.inputmembers(input);   
    return input;   
}   
int main()      
{   
    Test a;   
    cin>>a; 
    cout<<a.age<<"|"<<a.name<<endl; 
    system("pause");   
}

¡¾·µ»Ø¶¥²¿¡¿ ¡¾´òÓ¡±¾Ò³¡¿ ¡¾¹Ø±Õ´°¿Ú¡¿

¹ØÓÚÎÒÃÇ / ¸øÎÒÁôÑÔ / °æȨ¾Ù±¨ / Òâ¼û½¨Òé / ÍøÕ¾±à³ÌQQȺ   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net ¼ÓÔØʱ¼ä 0.0017