»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
¼¼ÊõÎĵµ > VCÎĵµ > ÆäËûÎĵµ
C++ÔËËã·ûÖØÔØ̽ÌÖ
·¢±íÈÕÆÚ£º2004-01-30 00:00:00×÷Õߣº° ³ö´¦£º  

¡¡¡¡Ç°ÑÔ

¡¡¡¡¶à̬ÐÔÊÇÃæÏò¶ÔÏó³ÌÐòÉè¼ÆµÄÖØÒªÌØÕ÷Ö®Ò»¡£ËüÓëÇ°Ãæ½²¹ýµÄ·â×°ÐԺͼ̳ÐÐÔ¹¹³ÉÁËÃæÏò¶ÔÏó³ÌÐòÉè¼ÆµÄÈý´óÌØÕ÷¡£ÕâÈý´óÌØÕ÷ÊÇÏ໥¹ØÁªµÄ¡£·â×°ÐÔÊÇ»ù´¡£¬¼Ì³ÐÐÔÊǹؼü£¬¶à̬ÐÔÊDz¹³ä£¬¶ø¶à̬ÓÖ±ØÐë´æÔÚÓڼ̳еĻ·¾³Ö®ÖС£

¡¡¡¡Ëùν¶à̬ÐÔÊÇÖ¸·¢³öͬÑùµÄÏûÏ¢±»²»Í¬ÀàÐ͵ĶÔÏó½ÓÊÕʱµ¼ÖÂÍêÈ«²»Í¬µÄÐÐΪ¡£ÕâÀïËù˵µÄÏûÏ¢Ö÷ÒªÊÇÖ¸¶ÔÀàµÄ³ÉÔ±º¯ÊýµÄµ÷Ó㬶ø²»Í¬µÄÐÐΪÊÇÖ¸²»Í¬µÄʵÏÖ¡£ÀûÓöà̬ÐÔ£¬Óû§Ö»Ðè·¢ËÍÒ»°ãÐÎʽµÄÏûÏ¢£¬¶ø½«ËùÓеÄʵÏÖÁô¸ø½ÓÊÕÏûÏ¢µÄ¶ÔÏ󡣶ÔÏó¸ù¾ÝËù½ÓÊÕµ½µÄÏûÏ¢¶ø×ö³öÏàÓ¦µÄ¶¯×÷(¼´²Ù×÷)¡£

¡¡¡¡º¯ÊýÖØÔغÍÔËËã·ûÖØÔØÊǼòµ¥Ò»Àà¶à̬ÐÔ¡£º¯ÊýÖØÔصĸÅÄî¼°Ó÷¨ÔÚ¡¶º¯ÊýÖØÔØ¡·Ò»½²ÖÐÒÑÌÖÂÛ¹ýÁË£¬ÕâÀïÖ»×÷¼òµ¥µÄ²¹³ä£¬ÎÒÃÇÖصãÌÖÂÛµÄÊÇÔËËã·ûµÄÖØÔØ¡£

¡¡¡¡Ëùνº¯ÊýÖØÔؼòµ¥µØ˵¾ÍÊǸ³¸øͬһ¸öº¯ÊýÃû¶à¸öº¬Òå¡£¾ßÌåµØ½²£¬C++ÖÐÔÊÐíÔÚÏàͬµÄ×÷ÓÃÓòÄÚÒÔÏàͬµÄÃû×Ö¶¨Ò弸¸ö²»Í¬ÊµÏֵĺ¯Êý£¬¿ÉÒÔÊdzÉÔ±º¯Êý£¬Ò²¿ÉÒÔÊǷdzÉÔ±º¯Êý¡£µ«ÊÇ£¬¶¨ÒåÕâÖÖÖØÔغ¯ÊýʱҪÇóº¯ÊýµÄ²ÎÊý»òÕßÖÁÉÙÓÐÒ»¸öÀàÐͲ»Í¬£¬»òÕ߸öÊý²»Í¬¡£¶ø¶ÔÓÚ·µ»ØÖµµÄÀàÐÍûÓÐÒªÇ󣬿ÉÒÔÏàͬ£¬Ò²¿ÉÒÔ²»Í¬¡£ÄÇÖÖ²ÎÊý¸öÊýºÍÀàÐͶ¼Ïàͬ£¬½ö½ö·µ»ØÖµ²»Í¬µÄÖØÔغ¯ÊýÊÇ·Ç·¨µÄ¡£ÒòΪ±àÒë³ÌÐòÔÚÑ¡ÔñÏàͬÃû×ÖµÄÖØÔغ¯Êýʱ½ö¿¼ÂǺ¯Êý±í£¬Õâ¾ÍÊÇ˵Ҫ¿¿º¯ÊýµÄ²ÎÊý±íÖУ¬²ÎÊý¸öÊý»ò²ÎÊýÀàÐ͵IJîÒì½øÐÐÑ¡Ôñ¡£ ÓÉ´Ë¿ÉÒÔ¿´³ö£¬ÖØÔغ¯ÊýµÄÒâÒåÔÚÓÚËü¿ÉÒÔÓÃÏàͬµÄÃû×Ö·ÃÎÊÒ»×éÏ໥¹ØÁªµÄº¯Êý£¬ÓɱàÒë³ÌÐòÀ´½øÐÐÑ¡Ôñ£¬Òò¶øÕ⽫ÓÐÖúÓÚ½â¾ö³ÌÐò¸´ÔÓÐÔÎÊÌâ¡£È磺ÔÚ¶¨ÒåÀàʱ£¬¹¹Ô캯ÊýÖØÔظø³õʼ»¯´øÀ´Á˶àÖÖ·½Ê½£¬ÎªÓû§Ìṩ¸ü´óµÄÁé»îÐÔ¡£

¡¡¡¡ÏÂÃæÎÒÃÇÖصãÌÖÂÛÔËËã·ûÖØÔØ¡£

¡¡¡¡ÔËËã·ûÖØÔؾÍÊǸ³ÓèÒÑÓеÄÔËËã·û¶àÖغ¬Òå¡£C++ÖÐͨ¹ýÖØж¨ÒåÔËËã·û£¬Ê¹ËüÄܹ»ÓÃÓÚÌض¨ÀàµÄ¶ÔÏóÖ´ÐÐÌض¨µÄ¹¦ÄÜ£¬Õâ±ãÔöÇ¿ÁËC++ÓïÑÔµÄÀ©³äÄÜÁ¦¡£

¡¡¡¡ÔËËã·ûÖØÔصļ¸¸öÎÊÌâ

¡¡¡¡1. ÔËËã·ûÖØÔصÄ×÷ÓÃÊÇʲô£¿

¡¡¡¡ËüÔÊÐíÄãΪÀàµÄÓû§Ìṩһ¸öÖ±¾õµÄ½Ó¿Ú¡£

¡¡¡¡ÔËËã·ûÖØÔØÔÊÐíC/C++µÄÔËËã·ûÔÚÓû§¶¨ÒåÀàÐÍ(Àà)ÉÏÓµÓÐÒ»¸öÓû§¶¨ÒåµÄÒâÒå¡£ÖØÔصÄÔËËã·ûÊǺ¯Êýµ÷ÓõÄÓï·¨ÐÞÊΣº

class Fred

{

public:

// ...

};

#if 0

// ûÓÐËã·ûÖØÔØ£º

Fred add(Fred, Fred);

Fred mul(Fred, Fred);

Fred f(Fred a, Fred b, Fred c)

{

return add(add(mul(a,b), mul(b,c)), mul(c,a)); // ¹þ¹þ£¬¶à¿ÉЦ...

}

#else

// ÓÐËã·ûÖØÔØ£º

Fred operator+ (Fred, Fred);

Fred operator* (Fred, Fred);

Fred f(Fred a, Fred b, Fred c)

{

return a*b + b*c + c*a;

}

#endif 

¡¡¡¡2. Ëã·ûÖØÔصĺô¦ÊÇʲô£¿

¡¡¡¡Í¨¹ýÖØÔØÀàÉϵıê×¼Ëã·û£¬Äã¿ÉÒÔ·¢¾òÀàµÄÓû§µÄÖ±¾õ¡£Ê¹µÃÓû§³ÌÐòËùÓõÄÓïÑÔÊÇÃæÏòÎÊÌâµÄ£¬¶ø²»ÊÇÃæÏò»úÆ÷µÄ¡£

¡¡¡¡×îÖÕÄ¿±êÊǽµµÍѧϰÇúÏß²¢¼õÉÙ´íÎóÂÊ¡£

¡¡¡¡3. ÄÄЩÔËËã·û¿ÉÒÔÓÃ×÷ÖØÔØ£¿

¡¡¡¡¼¸ºõËùÓеÄÔËËã·û¶¼¿ÉÓÃ×÷ÖØÔØ¡£¾ßÌå°üº¬£º

¡¡¡¡ËãÊõÔËËã·û£º+,-,*,/,%,++,--;

¡¡¡¡Î»²Ù×÷ÔËËã·û£º&,|,~,^,£¼£¼,£¾£¾

¡¡¡¡Âß¼­ÔËËã·û£º!,&&,||;

¡¡¡¡±È½ÏÔËËã·û£º£¼,£¾,£¾=,£¼=,==,!=;

¡¡¡¡¸³ÖµÔËËã·û£º=,+=,-=,*=,/=,%=,&=,|=,^=,£¼£¼=,£¾£¾=;

¡¡¡¡ÆäËûÔËËã·û£º[],(),-£¾,,(¶ººÅÔËËã·û),new,delete,new[],delete[],-£¾*¡£

¡¡¡¡ÏÂÁÐÔËËã·û²»ÔÊÐíÖØÔØ£º

¡¡¡¡.,.*,::,?:

¡¡¡¡4. ÔËËã·ûÖØÔغó£¬ÓÅÏȼ¶ºÍ½áºÏÐÔÔõô°ì£¿

¡¡¡¡Óû§ÖØÔØж¨ÒåÔËËã·û£¬²»¸Ä±äÔ­ÔËËã·ûµÄÓÅÏȼ¶ºÍ½áºÏÐÔ¡£Õâ¾ÍÊÇ˵£¬¶ÔÔËËã·ûÖØÔز»¸Ä±äÔËËã·ûµÄÓÅÏȼ¶ºÍ½áºÏÐÔ£¬²¢ÇÒÔËËã·ûÖØÔغó£¬Ò²²»¸Ä±äÔËËã·ûµÄÓï·¨½á¹¹£¬¼´µ¥Ä¿ÔËËã·ûÖ»ÄÜÖØÔØΪµ¥Ä¿ÔËËã·û£¬Ë«Ä¿ÔËËã·ûÖ»ÄÜÖØÔØË«Ä¿ÔËËã·û¡£

¡¡¡¡5. ±àÒë³ÌÐòÈçºÎÑ¡ÓÃÄÄÒ»¸öÔËËã·ûº¯Êý£¿

¡¡¡¡ÔËËã·ûÖØÔØʵ¼ÊÊÇÒ»¸öº¯Êý£¬ËùÒÔÔËËã·ûµÄÖØÔØʵ¼ÊÉÏÊǺ¯ÊýµÄÖØÔØ¡£±àÒë³ÌÐò¶ÔÔËËã·ûÖØÔصÄÑ¡Ôñ£¬×ñÑ­×ź¯ÊýÖØÔصÄÑ¡ÔñÔ­Ôò¡£µ±Óöµ½²»ºÜÃ÷ÏÔµÄÔËËãʱ£¬±àÒë³ÌÐò½«È¥Ñ°ÕÒ²ÎÊýÏàÆ¥ÅäµÄÔËËã·ûº¯Êý¡£

¡¡¡¡6. ÖØÔØÔËËã·ûÓÐÄÄЩÏÞÖÆ£¿

¡¡¡¡(1) ²»¿ÉÒÜÔìеÄÔËËã·û¡£±ØÐë°ÑÖØÔØÔËËã·ûÏÞÖÆÔÚC++ÓïÑÔÖÐÒÑÓеÄÔËËã·û·¶Î§ÄÚµÄÔÊÐíÖØÔصÄÔËËã·ûÖ®ÖС£

¡¡¡¡(2) ÖØÔØÔËËã·û¼á³Ö4¸ö¡°²»Äܸı䡱¡£

¡¡¡¡¡¤²»ÄܸıäÔËËã·û²Ù×÷ÊýµÄ¸öÊý£»

¡¡¡¡¡¤²»ÄܸıäÔËËã·ûÔ­ÓеÄÓÅÏȼ¶£»

¡¡¡¡¡¤²»ÄܸıäÔËËã·ûÔ­ÓеĽáºÏÐÔ£»

¡¡¡¡¡¤²»ÄܸıäÔËËã·ûÔ­ÓеÄÓï·¨½á¹¹¡£

¡¡¡¡7. ÔËËã·ûÖØÔØʱ±ØÐë×ñÑ­ÄÄЩԭÔò£¿

¡¡¡¡ÔËËã·ûÖØÔØ¿ÉÒÔʹ³ÌÐò¸ü¼Ó¼ò½à£¬Ê¹±í´ïʽ¸ü¼ÓÖ±¹Û£¬Ôö¼Ó¿É¶ÁÐÔ¡£µ«ÊÇ£¬ÔËËã·ûÖØÔØʹÓò»Ò˹ý¶à£¬·ñÔò»á´øÀ´Ò»¶¨µÄÂé·³¡£

¡¡¡¡Ê¹ÓÃÖØÔØÔËËã·ûʱӦ×ñÑ­ÈçÏÂÔ­Ôò£º

¡¡¡¡(1) ÖØÔØÔËËã·ûº¬Òå±ØÐëÇå³þ¡£

¡¡¡¡(2) ÖØÔØÔËËã·û²»ÄÜÓжþÒåÐÔ¡£

¡¡¡¡ÔËËã·ûÖØÔغ¯ÊýµÄÁ½ÖÖÐÎʽ

¡¡¡¡ÔËËã·ûÖØÔصĺ¯ÊýÒ»°ãµØ²ÉÓÃÈçÏÂÁ½ÖÖÐÎʽ£º³ÉÔ±º¯ÊýÐÎʽºÍÓÑÔªº¯ÊýÐÎʽ¡£ÕâÁ½ÖÖÐÎʽ¶¼¿É·ÃÎÊÀàÖеÄ˽ÓгÉÔ±¡£

¡¡¡¡1. ÖØÔØΪÀàµÄ³ÉÔ±º¯Êý

¡¡¡¡ÕâÀïÏȾÙÒ»¸ö¹ØÓÚ¸ø¸´ÊýÔËËãÖØÔظ´ÊýµÄËÄÔòÔËËã·ûµÄÀý×Ó¡£¸´ÊýÓÉʵ²¿ºÍÐ鲿¹¹Ô죬¿ÉÒÔ¶¨ÒåÒ»¸ö¸´ÊýÀ࣬ȻºóÔÙÔÚÀàÖÐÖØÔظ´ÊýËÄÔòÔËËãµÄÔËËã·û¡£ÏÈ¿´ÒÔÏÂÔ´´úÂ룺

#include £¼iostream.h£¾

class complex

{

public:

complex() { real=imag=0; }

complex(double r, double i)

{

real = r, imag = i;

}

complex operator +(const complex &c);

complex operator -(const complex &c);

complex operator *(const complex &c);

complex operator /(const complex &c);

friend void print(const complex &c);

private:

double real, imag;

};

inline complex complex::operator +(const complex &c)

{

return complex(real + c.real, imag + c.imag);

}

inline complex complex::operator -(const complex &c)

{

return complex(real - c.real, imag - c.imag);

}

inline complex complex::operator *(const complex &c)

{

return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);

}

inline complex complex::operator /(const complex &c)

{

return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),

(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));

}

void print(const complex &c)

{

if(c.imag£¼0)

cout£¼£¼c.real£¼£¼c.imag£¼£¼'i';

else

cout£¼£¼c.real£¼£¼'+'£¼£¼c.imag£¼£¼'i';

}

void main()

{

complex c1(2.0, 3.0), c2(4.0, -2.0), c3;

c3 = c1 + c2;

cout£¼£¼"\nc1+c2=";

print(c3);

c3 = c1 - c2;

cout£¼£¼"\nc1-c2=";

print(c3);

c3 = c1 * c2;

cout£¼£¼"\nc1*c2=";

print(c3);

c3 = c1 / c2;

cout£¼£¼"\nc1/c2=";

print(c3);

c3 = (c1+c2) * (c1-c2) * c2/c1;

cout£¼£¼"\n(c1+c2)*(c1-c2)*c2/c1=";

print(c3);

cout£¼£¼endl;

¡¡¡¡¸Ã³ÌÐòµÄÔËÐнá¹ûΪ£º

c1+c2=6+1i

c1-c2=-2+5i

c1*c2=14+8i

c1/c2=0.45+0.8i

(c1+c2)*(c1-c2)*c2/c1=9.61538+25.2308i 

¡¡¡¡ÔÚ³ÌÐòÖУ¬Ààcomplex¶¨ÒåÁË4¸ö³ÉÔ±º¯Êý×÷ΪÔËËã·ûÖØÔغ¯Êý¡£½«ÔËËã·ûÖØÔغ¯Êý˵Ã÷ΪÀàµÄ³ÉÔ±º¯Êý¸ñʽÈçÏ£º

¡¡¡¡£¼ÀàÃû£¾ operator £¼ÔËËã·û£¾(£¼²ÎÊý±í£¾)

¡¡¡¡ÆäÖУ¬operatorÊǶ¨ÒåÔËËã·ûÖØÔغ¯ÊýµÄ¹Ø¼ü×Ö¡£

¡¡¡¡³ÌÐòÖгöÏֵıí´ïʽ£º

¡¡¡¡c1+c2

¡¡¡¡±àÒë³ÌÐò½«¸ø½âÊÍΪ£º

¡¡¡¡c1.operator+(c2)

¡¡¡¡ÆäÖУ¬c1ºÍc2ÊÇcomplexÀàµÄ¶ÔÏó¡£operator+()ÊÇÔËËã+µÄÖØÔغ¯Êý¡£

¡¡¡¡¸ÃÔËËã·ûÖØÔغ¯Êý½öÓÐÒ»¸ö²ÎÊýc2¡£¿É¼û£¬µ±ÖØÔØΪ³ÉÔ±º¯Êýʱ£¬Ë«Ä¿ÔËËã·û½öÓÐÒ»¸ö²ÎÊý¡£¶Ôµ¥Ä¿ÔËËã·û£¬ÖØÔØΪ³ÉÔ±º¯Êýʱ£¬²»ÄÜÔÙÏÔʽ˵Ã÷²ÎÊý¡£ÖØÔØΪ³ÉÔ±º¯Êýʱ£¬×ÜʱÒþº¬ÁËÒ»¸ö²ÎÊý£¬¸Ã²ÎÊýÊÇthisÖ¸Õë¡£thisÖ¸ÕëÊÇÖ¸Ïòµ÷ÓøóÉÔ±º¯Êý¶ÔÏóµÄÖ¸Õë¡£

¡¡¡¡2. ÖØÔØΪÓÑÔªº¯Êý

¡¡¡¡ÔËËã·ûÖØÔغ¯Êý»¹¿ÉÒÔΪÓÑÔªº¯Êý¡£µ±ÖØÔØÓÑÔªº¯Êýʱ£¬½«Ã»ÓÐÒþº¬µÄ²ÎÊýthisÖ¸Õë¡£ÕâÑù£¬¶ÔË«Ä¿ÔËËã·û£¬ÓÑÔªº¯ÊýÓÐ2¸ö²ÎÊý£¬¶Ôµ¥Ä¿ÔËËã·û£¬ÓÑÔªº¯ÊýÓÐÒ»¸ö²ÎÊý¡£µ«ÊÇ£¬ÓÐЩÔËÐзû²»ÄÜÖØÔØΪÓÑÔªº¯Êý£¬ËüÃÇÊÇ£º=,(),[]ºÍ-£¾¡£

¡¡¡¡ÖØÔØΪÓÑÔªº¯ÊýµÄÔËËã·ûÖØÔغ¯ÊýµÄ¶¨Òå¸ñʽÈçÏ£º

¡¡¡¡friend £¼ÀàÐÍ˵Ã÷·û£¾ operator £¼ÔËËã·û£¾(£¼²ÎÊý±í£¾)

¡¡¡¡{¡­¡­}

¡¡¡¡ÏÂÃæÓÃÓÑÔªº¯Êý´úÂë³ÉÔ±º¯Êý£¬ÖØÔرàдÉÏÊöµÄÀý×Ó£¬³ÌÐòÈçÏ£º

#include £¼iostream.h£¾

class complex

{

public:

complex() { real=imag=0; }

complex(double r, double i)

{

real = r, imag = i;

}

friend complex operator +(const complex &c1, const complex &c2);

friend complex operator -(const complex &c1, const complex &c2);

friend complex operator *(const complex &c1, const complex &c2);

friend complex operator /(const complex &c1, const complex &c2);

friend

void print(const complex &c);

private:

double real, imag;

};

complex operator +(const complex &c1, const complex &c2)

{

return complex(c1.real + c2.real, c1.imag + c2.imag);

}

complex operator -(const complex &c1, const complex &c2)

{

return complex(c1.real - c2.real, c1.imag - c2.imag);

}

complex operator *(const complex &c1, const complex &c2)

{

return complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);

}

complex operator /(const complex &c1, const complex &c2)

{

return complex((c1.real * c2.real + c1.imag + c2.imag) / (c2.real * c2.real + c2.imag * c2.imag),

(c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));

}

void print(const complex &c)

{

if(c.imag£¼0)

cout£¼£¼c.real£¼£¼c.imag£¼£¼'i';

else

cout£¼£¼c.real£¼£¼'+'£¼£¼c.imag£¼£¼'i';

}

void main()

{

complex c1(2.0, 3.0), c2(4.0, -2.0), c3;

c3 = c1 + c2;

cout£¼£¼"\nc1+c2=";

print(c3);

c3 = c1 - c2;

cout£¼£¼"\nc1-c2=";

print(c3);

c3 = c1 * c2;

cout£¼£¼"\nc1*c2=";

print(c3);

c3 = c1 / c2;

cout£¼£¼"\nc1/c2=";

print(c3);

c3 = (c1+c2) * (c1-c2) * c2/c1;

cout£¼£¼"\n(c1+c2)*(c1-c2)*c2/c1=";

print(c3);

cout£¼£¼endl;

¡¡¡¡¸Ã³ÌÐòµÄÔËÐнá¹ûÓëÉÏÀýÏàͬ¡£Ç°ÃæÒѽ²¹ý£¬¶ÔÓÖÄ¿ÔËËã·û£¬ÖØÔØΪ³ÉÔ±º¯Êýʱ£¬½öÒ»¸ö²ÎÊý£¬ÁíÒ»¸ö±»Òþº¬£»ÖØÔØΪÓÑÔªº¯Êýʱ£¬ÓÐÁ½¸ö²ÎÊý£¬Ã»ÓÐÒþº¬²ÎÊý¡£Òò´Ë£¬³ÌÐòÖгöÏÖµÄ c1+c2

¡¡¡¡±àÒë³ÌÐò½âÊÍΪ£º

¡¡¡¡operator+(c1, c2)

¡¡¡¡µ÷ÓÃÈçϺ¯Êý£¬½øÐÐÇóÖµ£¬

¡¡¡¡complex operator +(const coplex &c1, const complex &c2)

¡¡¡¡3. Á½ÖÖÖØÔØÐÎʽµÄ±È½Ï

¡¡¡¡Ò»°ã˵À´£¬µ¥Ä¿ÔËËã·û×îºÃ±»ÖØÔØΪ³ÉÔ±£»¶ÔË«Ä¿ÔËËã·û×îºÃ±»ÖØÔØΪÓÑÔªº¯Êý£¬Ë«Ä¿ÔËËã·ûÖØÔØΪÓÑÔªº¯Êý±ÈÖØÔØΪ³ÉÔ±º¯Êý¸ü·½±ã´Ë£¬µ«ÊÇ£¬ÓеÄË«Ä¿ÔËËã·û»¹ÊÇÖØÔØΪ³ÉÔ±º¯ÊýΪºÃ£¬ÀýÈ磬¸³ÖµÔËËã·û¡£ÒòΪ£¬ËüÈç¹û±»ÖØÔØΪÓÑÔªº¯Êý£¬½«»á³öÏÖÓ븳ֵÓïÒå²»Ò»Öµĵط½¡£ ÆäËûÔËËã·ûµÄÖØÔؾÙÀý

¡¡¡¡1).ϱêÔËËã·ûÖØÔØ

¡¡¡¡ÓÉÓÚCÓïÑÔµÄÊý×éÖв¢Ã»Óб£´æÆä´óС£¬Òò´Ë£¬²»ÄܶÔÊý×éÔªËؽøÐдæÈ¡·¶Î§µÄ¼ì²é£¬ÎÞ·¨±£Ö¤¸øÊý×鶯̬¸³Öµ²»»áÔ½½ç¡£ÀûÓÃC++µÄÀà¿ÉÒÔ¶¨ÒåÒ»ÖÖ¸ü°²È«¡¢¹¦ÄÜÇ¿µÄÊý×éÀàÐÍ¡£Îª´Ë£¬Îª¸ÃÀඨÒåÖØÔØÔËËã·û[]¡£

¡¡¡¡ÏÂÃæÏÈ¿´¿´Ò»¸öÀý×Ó£º

#include £¼iostream.h£¾

class CharArray

{

public:

CharArray(int l)

{

Length = l;

Buff = new char[Length];

}

~CharArray() { delete Buff; }

int GetLength() { return Length; }

char & operator [](int i);

private:

int Length;

char * Buff;

};

char & CharArray::operator [](int i)

{

static char ch = 0;

if(i£¼Length&&i£¾=0)

return Buff[i];

else

{

cout£¼£¼"\nIndex out of range.";

return ch;

}

}

void main()

{

int cnt;

CharArray string1(6);

char * string2 = "string";

for(cnt=0; cnt£¼8; cnt++)

string1[cnt] = string2[cnt];

cout£¼£¼"\n";

for(cnt=0; cnt£¼8; cnt++)

cout£¼£¼string1[cnt];

cout£¼£¼"\n";

cout£¼£¼string1.GetLength()£¼£¼endl;

¡¡¡¡¸ÃÊý×éÀàµÄÓŵãÈçÏ£º

¡¡¡¡(1) Æä´óС²»ÐÄÊÇÒ»¸ö³£Á¿¡£

¡¡¡¡(2) ÔËÐÐʱ¶¯Ì¬Ö¸¶¨´óС¿ÉÒÔ²»ÓÃÔËËã·ûnewºÍdelete¡£

¡¡¡¡(3) µ±Ê¹ÓøÃÀàÊý×é×÷º¯Êý²ÎÊýʱ£¬²»Ðķֱ𴫵ÝÊý×é±äÁ¿±¾Éí¼°Æä´óС£¬ÒòΪ¸Ã¶ÔÏóÖÐÒѾ­±£´æ´óС¡£

¡¡¡¡ÔÚÖØÔØϱêÔËËã·ûº¯ÊýʱӦ¸Ã×¢Ò⣺

¡¡¡¡(1) ¸Ãº¯ÊýÖ»ÄÜ´øÒ»¸ö²ÎÊý£¬²»¿É´ø¶à¸ö²ÎÊý¡£

¡¡¡¡(2) ²»µÃÖØÔØΪÓÑÔªº¯Êý£¬±ØÐëÊÇ·ÇstaticÀàµÄ³ÉÔ±º¯Êý¡£ 2). ÖØÔØÔö1¼õ1ÔËËã·û

¡¡¡¡Ôö1¼õ1ÔËËã·ûÊǵ¥Ä¿ÔËËã·û¡£ËüÃÇÓÖÓÐǰ׺ºÍºó׺ÔËËãÁ½ÖÖ¡£ÎªÁËÇø·ÖÕâÁ½ÖÖÔËË㣬½«ºó׺ÔËËãÊÓΪÓÖÄ¿ÔËËã·û¡£±í´ïʽ

¡¡¡¡obj++»òobj--

¡¡¡¡±»¿´×÷Ϊ£º

¡¡¡¡obj++0»òobj--0

¡¡¡¡ÏÂÃæ¾ÙÒ»Àý×Ó˵Ã÷ÖØÔØÔö1¼õ1ÔËËã·ûµÄÓ¦Óá£

#include £¼iostream.h£¾

class counter

{

public:

counter() { v=0; }

counter operator ++();

counter operator ++(int );

void print() { cout£¼£¼v£¼£¼endl; }

private:

unsigned v;

};

counter counter::operator ++()

{

v++;

return *this;

}

counter counter::operator ++(int)

{

counter t;

t.v = v++;

return t;

}

void main()

{

counter c;

for(int i=0; i£¼8; i++)

c++;

c.print();

for(i=0; i£¼8; i++)

++c;

c.print();

¡¡¡¡3). ÖØÔغ¯Êýµ÷ÓÃÔËËã·û

¡¡¡¡¿ÉÒÔ½«º¯Êýµ÷ÓÃÔËËã·û()¿´³ÉÊÇϱêÔËËã[]µÄÀ©Õ¹¡£º¯Êýµ÷ÓÃÔËËã·û¿ÉÒÔ´ø0¸öÖÁ¶à¸ö²ÎÊý¡£ÏÂÃæͨ¹ýÒ»¸öʵÀýÀ´ÊìϤº¯Êýµ÷ÓÃÔËËã·ûµÄÖØÔØ¡£

#include £¼iostream.h£¾

class F

{

public:

double operator ()(double x, double y) const;

};

double F::operator ()(double x, double y) const

{

return (x+5)*y;

}

void main()

{

F f;

cout£¼£¼f(1.5, 2.2)£¼£¼endl;

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

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