´ÙÀ½ ÀÌÀü Â÷·Ê

3. mychar classÀÇ »ç¿ë

'mychar class' ´Â char ¿Í char * datatypeÀÇ ¿Ïº®ÇÑ ´ëü¹°ÀÌ´Ù. ¿©·¯ºÐÀº 'mychar class' ¸¦ char¾²´Â °Í°ú ¶È°°Àº ¾²¸é¼­ ´õ ¸¹Àº ±â´É¼ºÀ» ¾òÀ» ¼ö ÀÖ´Ù. ¿©·¯ºÐÀº 'libmychar.a' ¸¦ includeÇØ¾ß ÇÏ°í library ¸¦ "C++" libraryµéÀÌ À§Ä¡ÇÑ /usr/lib directory¿¡ Ä«ÇÇÇؾßÇÑ´Ù ('libmychar.a'´Â Appendix H¿¡ ÀÖ´Â makefile¿¡¼­ ¸¸µé ¼ö ÀÖ´Ù ). 'libmychar.a'À» »ç¿ëÇϱâ â À§Çؼ­´Â ¿©·¯ºÐÀÇ ÇÁ·Î±×·¥À» ´ÙÀ½°ú °°ÀÌ ÄÄÆÄÀÏÇϽÿÀ.


        g++ example.cpp -lmychar

´ÙÀ½ »ùÇÃÄڵ带 º¸½Ã¿À.
        mychar aa;

        aa = " Washington DC is the capital of USA ";

        // You can use aa.val like a 'char *' variable in programs !!
        for (unsigned long tmpii = 0; tmpii < aa.length(); tmpii++)
        {
                fprintf(stdout, "aa.val[%ld]=%c ", tmpii, aa.val[tmpii]);
        }

        // Using pointers on 'char *' val ...
        // Note: You must use a temporary local variable and assign the
        // pointer to aa.val. If you directly use aa.val and when
        // aa.val is incremented with aa.val++, then aa will go 
        // call destructor and later when aa.val is accessed that
        // will cause core dump !!
        for (char *tmpcc = aa.val; *tmpcc != 0; tmpcc++)  
        {
                // MUST use temporary variable tmpcc !! See note above.
                fprintf(stdout, "aa.val=%c ", *tmpcc);
        }

mychar class ¸¦ Á¤ÀÇÇÑ ¿ÏÀüÇÑ ¿¹Á¦ ÇÁ·Î±×·¥ "example_mychar.cpp" Àº Appendix A¿¡ ÀÖ°í mychar class ´Â Appendix B¿¡ ÀÖ´Ù.

3.1 ¿¬»êÀÚµé

'mychar class'´Â ´ÙÀ½°ú °°Àº ¿¬»êÀÚµéÀ» Á¦°øÇÑ´Ù.

¿¬»êÀÚµéÀ» »ç¿ëÇÑ ¿¹Á¦
        mychar aa;
        mychar bb("Bill Clinton");

        aa = "put some value string";  // assignment operator
        aa += "add some more"; // Add to itself and assign operator
        aa = "My name is" + " Alavoor Vasudevan "; // string cat operator

        if (bb == "Bill Clinton")  // boolean equal to operator
                cout << "bb is eqaul to 'Bill Clinton' " << endl;

        if (bb != "Al Gore")   // boolean 'not equal' to operator
                cout << "bb is not equal to 'Al Gore'" << endl;

3.2 ÇÔ¼öµé

'mychar class'´Â ´ÙÀ½°í °°Àº ÇÔ¼öµéÀ» Á¦°øÇÑ´Ù.

3.3 ±âŸ ÇÔ¼öµé

±âŸ mychar ÇÔ¼öµéÀ» ¿©±â¿¡ ¸ð¾ÆµÎ¾ú´Ù. ÇÏÁö¸¸ À̰͵éÀ» »ç¿ëÇÏÁö´Â ¸¶¶ó. ´ë½Å '+', '+=', '==' µî°ú °°Àº ¿¬»êÀÚµéÀ» »ç¿ëÇ϶ó. À̰͵éÀº 'mychar' class 'private'¸â¹öµéÀÌ´Ù.

¿¹¸¦ µé¾î Á¤¼ö¸¦ ¹®ÀÚ¿­·Î ¹Ù²Ù±â À§Çؼ­´Â ´ÙÀ½°ú °°ÀÌ Ç϶ó.
        mychar  aa;

        aa = 34;  //¿¬»êÀÚ ¡®=¡¯ ´Â int À» string ·Î ¹Ù²Û´Ù.
        cout << "The value of aa is : " << aa.val << endl;

        aa = 234.878; // ¿¬»êÀÚ '=' ´Â float À» string·Î ¹Ù²Û´Ù.
        cout << "The value of aa is : " << aa.val << endl;

        aa = 34 + 234.878;
        cout << "The value of aa is : " << aa.val << endl;
        // aa ´Â '268.878' ·Î µÈ´Ù.
        // mychar¸¦ cast ÇØ¾ß ÇÑ´Ù.
        aa = (mychar) 34 + " Honourable President Ronald Reagan " + 234.878;
        cout << "The value of aa is : " << aa.val << endl;
        // '34 Honourable President Ronald Reagan 234.878' ·Î Ãâ·ÂµÈ´Ù.


´ÙÀ½ ÀÌÀü Â÷·Ê