CÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ C++
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

C »ç¿ëÀÚ¸¦ À§ÇÑ C++

1절. CÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ C++

1.1절. »õ·Î¿î include ¹æ¹ý

C++ ¿¡¼­´Â Çì´õÆÄÀÏÀ» ÀÎŬ·çµå ½Ã۱â À§Çؼ­ »õ·Î¿î ¹æ¹ýÀ» »ç¿ëÇÑ´Ù. C++ ¿¡¼­´Â CÀÇ Ç¥ÁØ Çì´õÆÄÀÏÀ» ÀÎŬ·çµå ½Ã۱â À§Çؼ­ ".h" È®ÀåÀÚ¸¦ »ç¿ëÇÏ´Â ´ë½Å¿¡ ".h"¸¦ »ý·«Çϰí Çì´õÆÄÀÏÀÇ °¡Àå¾Õ¿¡ "c" ¸¦ ºÙ¿©¼­ ÀÎŬ·çµå ½ÃŲ´Ù. Á¦´ë·ÎµÈ ÀÎŬ·çµå¸¦ À§Çؼ­ "using namespace std;" ¸¦ Æ÷ÇÔ½Ã۵µ·Ï ÇÑ´Ù. Ç¥ÁØ C++ Çì´õ´Â È®ÀåÀÚ¸¦ »ý·«ÇÏ¸é µÈ´Ù - »ý·«ÇÏÁö ¾Ê¾Æµµ ¹®Á¦´Â ¾øÁö¸¸ -.

¹°·Ð ±âÁ¸ÀÇ C ½ºÅ¸ÀÏ´ë·Î Çì´õÆÄÀÏÀ» ÀÎŬ·çµå ½ÃÄѵµ ¹®Á¦´Â ¾ø´Ù. ±×·¯³ª ¾î¶² ÄÄÆÄÀÏ·¯ÀÇ °æ¿ì(gcc 3.x ¿Í °°Àº) µð¹ö±ë ¿É¼ÇÀ» ÄѳõÀº »óÅ¿¡¼­ ÄÄÆÄÀÏÇÒ°æ¿ì warning ¸Þ½ÃÁö¸¦ Ãâ·ÂÇϱ⵵ ÇÑ´Ù.

// stdlib.h -> cstdlib
#include <cstdlib>
#include <iostream>

using namespace std;
int main()
{
    char *age = "25";

    cout << atoi(age) << endl;
}
			


1.2절. ¶óÀδÜÀ§ ÁÖ¼®»ç¿ë

C ¿¡¼­¿Í ¸¶Âù°¡Áö·Î // ¸¦ ÀÌ¿ëÇÑ ¶óÀδÜÀ§ ÁÖ¼®ÀÇ »ç¿ëÀÌ °¡´ÉÇÏ´Ù.

#include <cstdlib>
#include <iostream>             // iostream ¶óÀ̺귯¸® »ç¿ë

using namespace std;           // Ç¥ÁØ ¶óÀ̺귯¸® namespace ÁöÁ¤
int main()
{
    char *age = "25";

    cout << atoi(age) >> endl; // Ãâ·Â
    return 1;                  // Á¾·á
}
			


1.3절. °£´ÜÇÏ°Ô »ç¿ëÇÒ¼ö ÀÖ´Â ÀÔÃâ·Â ½ºÆ®¸²

C ¿¡¼­´Â °£´ÜÇÑ È­¸é/Űº¸µå ÀÔÃâ·ÂÀÌ¶óµµ ²Ï ¹ø°Å·Î¿î °úÁ¤À» °ÅÃÄ¾ß ÇÏÁö¸¸, C++ ¿¡¼­´Â cout <<, cin >> À» ÀÌ¿ëÇØ¼­ °£´ÜÇÑ ÀÔÃâ·ÂÀ» ½±°Ô ó¸®ÇÒ¼ö ÀÖ´Ù.

#include <iostream>

using namespace std;
int main()
{
    int age;
    char name[32];
    cout << "hello world " << endl;
    cout << "your age : ";
    cin >> age;
    cout << "your name : ";
    cin >> name;

    cout << "Your input is " << name << ":" << age << endl;
}
			


1.4절. º¯¼ö¼±¾ð À§Ä¡Á¦ÇÑ

C ÀÇ °æ¿ì º¯¼ö ¼±¾ðÀº ÇÔ¼öÀÇ °¡ÀåùºÎºÐ¿¡¼­ ÀÌ·ç¾îÁ®¾ß ÇÑ´Ù. ¸¸¾à Áß°£¿¡ ¼±¾ðÀÌ ÀÌ·ç¾îÁø´Ù¸é, ÄÄÆÄÀϽà ¿¡·¯¸¦ ¹ß»ýÇÏ°Ô µÈ´Ù.

C++ Àº ¾î´À À§Ä¡¿¡¼­¶óµµ ¼±¾ðÇØ¼­ »ç¿ëÇÒ¼ö ÀÖ´Ù. ÀÌ·¯ÇÑ Á¡ÀÌ ¶§·Î´Â Äڵ带 ³­ÀâÇÏ°Ô ¸¸µé±âµµ ÇÏÁö¸¸, ¿ÀÈ÷·Á Äڵ带 º¸±â½±°Ô ¸¸µé¾îÁÙ¶§µµ ÀÖ´Ù.

#include <iostream>

using namespace std;
int main()
{
    int a, b;

    cout << "A : " ;
    cin >> a ;
    cout << "B : " ;
    cin >> b ;

    int sum;
    sum = a + b;
    cout << a << "+" << b << "=" << sum << endl;
}
			


1.5절. Àü¿ªº¯¼ö¿Í Áö¿ªº¯¼öÀÇ µ¿ÀÏÀ̸§ »ç¿ë

C ¿¡¼­´Â Àü¿ªº¯¼ö¿Í Áö¿ªº¯¼ö°¡ À̸§ÀÌ °°À»°æ¿ì ¹«Á¶°Ç Áö¿ªº¯¼öÀÇ °ª¸¸À» »ç¿ëÇÒ¼ö ÀÖ¾úÀ¸³ª(Àü¿ªº¯¼ö °ªÀº »ç¿ë ÇÒ¼ö°¡ ¾ø´Ù), C++ ¿¡¼­´Â °¢°¢ ±¸ºÐÇØ¼­ »ç¿ë°¡´ÉÇÏ´Ù.

#include <iostream>
using namespace std;

int my_age = 28;
int main()
{
    int my_age = 35;

    cout << "Local my_age " << my_age << endl;
    cout << "global my_age " << ::my_age << endl;

    return 0;
}
			


1.6절. º¯¼öÀÇ »óÈ£ÂüÁ¶°¡´É

´ÙÀ½°ú °°Àº ¹æ¹ýÀ¸·Î ÇϳªÀÇ º¯¼ö¸¦ ´Ù¸¥º¯¼ö¿¡¼­ ÂüÁ¶ÇÏ¿© »ç¿ëÇÏ´Â°Ô °¡´ÉÇÏ´Ù.

#include <iostream>
using namespace std;

int main()
{   
    int a = 200;
    int &b = a;

    b = 100;
    
    cout << "a is "  << a << endl;
    return 0;
}
			
À§ÀÇ Äڵ带 ½ÇÇà½Ã۸é 100 ÀÌ Ãâ·ÂµÈ´Ù.


1.7절. namespace ÀÇ ¼±¾ð

namespace ¸¦ ÀÌ¿ëÇØ¼­ º¯¼öÀÇ ¼±¾ðÀÌ °¡´ÉÇϸç :: ¿¬»êÀÚ¸¦ ÅëÇØ¼­ ¼±¾ð ¾øÀÌ °ð¹Ù·Î º¯¼öÀÇ ÀÌ¿ëÀÌ °¡´ÉÇÏ´Ù.

#include <iostream>
using namespace std;

namespace first
{
    int a;
    int b;
}
namespace second
{
    int a;
    int b;
}
int main()
{
    first::a = 100;
    first::b = 200;

    second::a = 400;
    second::b = 800;

    cout << first::a + second::a << endl;
    cout << first::b + second::b << endl;
}
			


1.8절. inline ÇÔ¼öÀÇ »ç¿ë

°£´ÜÇÑ ÇÔ¼öµéÀº inline À¸·Î ¼±¾ðÇØ¼­ »ç¿ëÇÔÀ¸·Î½á ¸î°¡Áö ÀÕÁ¡À» ¾òÀ»¼ö ÀÖ´Ù. inline À¸·Î ¼±¾ðµÈ ÇÔ¼ö´Â ÀÏÁ¾ÀÇ macro ¿Í °°ÀÌ ÀÛµ¿À» ÇÏ°Ô µÈ´Ù. Áï ÇÊ¿äÇÒ¶§ ºÒ·¯¿À´Â ¹æ½ÄÀÌ ¾Æ´Ï¶ó, Äڵ忡 ¹Ù·Î insert µÈ È¿°ú¸¦ ÁØ´Ù. À̰ÍÀÌ ÁÖ´Â ÀÕÁ¡Àº Äڵ尡 ¾à°£ Ä¿Áö±ä ÇϰÚÁö¸¸, ºü¸¥ ½ÇÇà ¼Óµµ¸¦ º¸ÀåÇØ Áشٴ Á¡ÀÌ´Ù.

#include <iostream>
#include <cmath>
using namespace std;

inline double mysqrt(double a, double b)
{
    return sqrt (a * a + b * b);
}
int main()
{
    double k = 6, m = 9;

    // ¹ØÀÇ 2°³ÀÇ ¶óÀÎÀº ½ÇÇà½Ã¿¡ ¿ÏÀüÈ÷ 
    // µ¿ÀÏÇÏ°Ô ÀÛµ¿ÇÑ´Ù.  
    cout << mysqrt(k, m) << endl;
    cout << sqrt(k*k + m*m) << endl;

    return 0;
}
			
inline ÀΰͰú ¾Æ´Ñ°ÍÀÇ Â÷À̸¦ ºñ±³Çغ¸°í ½Í´Ù¸é, g++ -S ¸¦ ÀÌ¿ëÇØ¼­ ¾î¼ÀÄڵ带 ¾òÀº´ÙÀ½¿¡ Á÷Á¢ ºñ±³ ÇØº¸±â ¹Ù¶õ´Ù.


1.9절. ¿¹¿Üó¸®

´ç½ÅÀÌ C¿¡¼­ »ç¿ëÇϰí ÀÖ´Ù¸é, for, if, do, while, switch ¿Í °°Àº Ű¿öµåµé¸¦ ¾Ë°í ÀÖÀ»°ÍÀÌ´Ù. C++ ¿¡¼­´Â ¿¹¿Üó¸®(EXECPTION)¿Í °ü·ÃµÈ ¶Ç´Ù¸¥ Ű¿öµåµéÀ» Á¦°øÇÑ´Ù. ¼±Åù® ȤÀº ¿¹¿Ü󸮸¦ À§ÇÑ Á»´õ Á÷°üÀûÀÌ°í ¼Õ½¬¿î ÇÁ·Î±×·¡¹Ö ÀÛ¾÷À» °¡´ÉÇϵµ·Ï µµ¿ÍÁØ´Ù.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int age;
    char *no_vote[] = {"¾ø½À´Ï´Ù", "ÀÖ½À´Ï´Ù."};

    cout << "´ç½ÅÀÇ ³ªÀÌ´Â ? ";
    cin >> age;

    try
    {
        if (age > 18) throw 1;
        else throw 0;
    }
    catch(int result)
    {
        cout << "´ç½ÅÀº ÅõÇ¥±ÇÀÌ " <<  no_vote[result] << endl;
    }

    return 0;
}
			


1.10절. default ÀÎÀÚ»ç¿ë °¡´É

ÇÔ¼öÀÇ ÀÎÀÚ¸¦ »ç¿ëÇÒ¶§ ±âº» ÀÎÀÚ¸¦ ¼³Á¤ÇÒ¼ö ÀÖ´Ù.

  
#include <iostream>
using namespace std;

int mysqrt(int a, int b = 2)
{
    int c = 1;
    for (int i =0; i < b; i++)
    {
        c *= a;
    }
    return c;
}
int main()
{
    cout << mysqrt(5) << endl;
    cout << mysqrt(5, 5) << endl;
}
			


1.11절. Parameters Overload

C++ ÀÇ Áß¿äÇÑ ÀÕÁ¡Áß¿¡ Çϳª°¡ ÀÎÀÚ¸¦ ÅëÇÑ ÇÔ¼ö ¿À¹ö·Îµå°¡ °¡´ÉÇÏ´Ù´Â Á¡ÀÌ´Ù. ¿À¹ö·Îµå ±â´ÉÀ» ÀÌ¿ëÇÔÀ¸·Î½á, ¼­·Î ´Ù¸¥ ¿¬»êÀ» ¼öÇàÇÏ´Â ÇÔ¼ö¸¦ ÇϳªÀÇ À̸§À¸·Î °ü¸® °¡´É Çϵµ·Ï µµ¿ÍÁÖ¸ç, ÀÌ´Â ÄÚµåÀÇ À¯Áö/º¸¼ö/°¡µ¶¼ºÀ» ³ô¿©ÁØ´Ù.

#include <iostream>

using namespace std;

double add (double a, double b)
{
    return a+b;
}

int add (int a, int b)
{
    return a+b;
}

int main()
{
    cout << add(1, 2) << endl;
    cout << add(1.2, 2.4) << endl;
}
			


1.12절. Operator overload

ÇÔ¼öÀÎÀÚ¸¦ ÅëÇÑ ¿À¹ö·Îµå »Ó¸¸ ¾Æ´Ï¶ó, ±âº»ÀûÀÎ ¿¬»êÀÚµéÀÇ ¿À¹ö·Îµå¿ª½Ã °¡´ÉÇÏ´Ù. ´Ù½Ã ¸»Çؼ­ ¿¬»êÀÚÀÇ Á¤ÀǸ¦ ´Ù½Ã ³»¸±¼ö ÀÖµµ·Ï ÇÑ´Ù.

#include <iostream>

using namespace std;
struct vector
{
    double x;
    double y;
};

vector operator * (double a, vector b)
{
    vector r;
    r.x = a * b.x;
    r.y = a * b.y;

    return r;
};

int main()
{
    vector k, m;
    k.x = 2;
    k.y = 4;

    m = 3.141927 * k;
    cout << "(" << m.x << "," << m.y << ")" << endl;

    return 0;
}
			
¿¬»êÀÚ ¿À¹ö·Î´Â »çÄ¢¿¬»êÀÚ ¿Ü¿¡µµ +=, ++, [], (), << µîÀ» Æ÷ÇÔÇÑ ¾à 40°³ ÀÌ»óÀÇ ¿¬»êÀÚ¿¡ ´ëÇØ¼­µµ °¡´ÉÇÏ´Ù.
#include <iostream>

using namespace std;
struct vector
{
    double x;
    double y;
};

ostream& operator << (ostream & o, vector a)
{
    o << "(" << a.x << "," << a.y << ")" ;
    return o;
}

int main()
{
    vector k;
    k.x = 2;
    k.y = 4;

    cout << k << endl;
    cout << "hello "<< endl;

    return 0;
}
			


1.13절. template

ÇÔ¼ö ¿À¹ö·ÎµùÀÌ ²Ï ÆíÇϱä ÇÏÁö¸¸, ¸î°¡Áö ºÒÆíÇÔÀÌ ÀÖ´Ù. Áï ÀÎÀÚÀÇ °¹¼ö¸¸Å­ÀÇ ÇÔ¼ö¸¦ ¸¸µé¾îÁà¾ß ÇÑ´Ù. ¸¸¾à int, float, double ¿¬»êÀ» À§ÇÑ ¿À¹ö·ÎµåµÈ ÇÔ¼ö¸¦ ¸¸µé°íÀÚ ÇÑ´Ù¸é, °ÅÀÇ ¶È°°Àº 3°³ÀÇ ÇÔ¼ö¸¦ Á¤ÀÇÇØ¾ß¸¸ ÇÑ´Ù. template ¸¦ »ç¿ëÇϸé ÀÎÀÚÀÇ ÀÚ·áÇü¿¡ °ü°è¾øÀÌ »ç¿ë°¡´ÉÇÑ (¹ü¿ëÇÑ) ÇÔ¼öÀÇ Á¦ÀÛÀÌ °¡´ÉÇÏ´Ù.

#include <iostream>

using namespace std;

template <class T>
T mymin (T a, T b)
{
    T r;
    r = a;

    if (b < a) r = b;

    return r;
}

int main()
{
    cout << "Litle is : " << mymin(2, 100) << endl;
    cout << "Litle is : " << mymin(2.6, 2.4) << endl;
}
			

À§ÀÇ ÅÛÇø´À» ÀÌ¿ëÇÑ ÄÚµå´Â ²Ï ±¦Âú°Ô ÀÛµ¿Çϱä ÇÏÁö¸¸, ÇѰ¡Áö ¹®Á¦°¡ ÀÖ´Ù. À§ÀÇ ÄÚµå´Â ÀÎÀÚÀÇ Å¸ÀÔÀÌ µ¿ÀÏÇØ¾ß ÇÑ´Ù. ¸¸¾à ÀÎÀÚ°¡ °¢°¢ int, double ŸÀÔÀ» °¡Áø´Ù¸é, ÄÄÆÄÀϽà ¿¡·¯¸¦ ¹ß»ý½Ãų°ÍÀÌ´Ù. À̹®Á¦´Â ÅÛÇø´À» ¼±¾ðÇÒ¶§ ¼­·Î ´Ù¸¥ ÀÎÀÚ¸¦ ¹Þ¾ÆµéÀϼö ÀÖµµ·Ï ¼±¾ðÇÏ¸é µÈ´Ù.

#include <iostream>

using namespace std;

template <class T1, class T2>
T1 mymin (T1 a, T2 b)
{
    T1 r, converted;

    r = a;

    converted = (T1) b;

    if (converted < a) r =  converted;

    return r;
}

int main()
{
    cout << "Litle is : " << mymin(2, 100) << endl;
    cout << "Litle is : " << mymin(2.6, 2.4) << endl;
    cout << "Litle is : " << mymin(3.4, 3) << endl;
}
			
±×¿Ü¿¡µµ ºñ±³¿¬»êÀ» Á¦´ë·Î ¼öÇàÇϱâ À§ÇÑ Çüº¯È¯ÀÌ ÇÊ¿äÇÒ °ÍÀÌ´Ù.


1.14절. ¸Þ¸ð¸® ÇÒ´ç/ÇØÁ¦

¸Þ¸ð¸® ÇÒ´ç°ú ÇØÁ¦¸¦ À§Çؼ­ new ¿Í delete Ű¿öµå¸¦ »ç¿ëÇÒ¼ö ÀÖ´Ù. À̵éÀº C ¿¡¼­ »ç¿ëÇÏ´Â malloc, free ´ë½Å »ç¿ëÇÒ¼ö ÀÖ´Ù. ¸¸¾à ¹è¿­ÀÇ ÇÒ´çÀ» ¿øÇÑ´Ù¸é new[] delete[] ¸¦ »ç¿ëÇÏ¸é µÈ´Ù.

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int *d;

    // int ÇüÀ» Æ÷ÇÔÇÒ¼ö ÀÖ´Â »õ·Î¿î ¸Þ¸ð¸® °ø°£È®º¸Çϰí 
    // ÁÖ¼Ò¸¦ µÇµ¹·ÁÁØ´Ù. 
    d = new int;
    *d = 21;

    cout << "Type a number : ";
    cin >> *d;
    cout << *d + 5 << endl;

    // ÇÒ´ç¹ÞÀº ¸Þ¸ð¸® ¿µ¿ªÀ» ÇØÁ¦ÇÑ´Ù.
    delete d;

    // 15°³ÀÇ int ÇüÀڷḦ ÀúÀåÇÒ¼ö ÀÖ´Â »õ·Î¿î ¸Þ¸ð¸® 
    // °ø°£À» È®º¸Çϰí, ÁÖ¼Ò¸¦ µÇµ¹·ÁÁØ´Ù. 
    d = new int[15];
    d[0] = 1234;
    d[1] = d[0] + 1234;

    cout << d[0] << ":"<< d[1] << ":" << d[2] << endl;
    delete []d;
    return 0;
}
			


1.15절. Class

°£´ÜÈ÷ »ý°¢Çؼ­ Class ¶õ ¹ßÀüµÈ ÇüÅÂÀÇ struct ÀÌ´Ù. µ¥ÀÌŸ¿Í ÇÔ²², µ¥ÀÌŸ¸¦ °¡°øÇÒ METHODS °¡ ¼±¾ðµÉ¼ö ÀÖ´Ù. ´ÙÀ½Àº Class ¼³¸íÀ» À§ÇÑ °£´ÜÇÑ ¿¹Á¦ÀÌ´Ù.

#include <iostream>
#include <cstring>

using namespace std;

class vector
{
    public:
        double x;
        double y;

    inline double surface()
    {
        double s;
        s = x*y;
        if (s < 0)
            s = -s;
        return s;
    }
};

int main()
{
    vector a;
    a.x = 3;
    a.y = 4;

    cout << a.surface() << endl;
    return 0;
}
			
À§ÀÇ ¿¹Á¦Äڵ忡¼­ a¸¦ Ŭ·¡½º vector ÀÇ ÀνºÅϽº(INSTANCE)¶ó°í ÇÑ´Ù.


1.16절. »ý¼ºÀÚ / ¼Ò¸êÀÚ

¸¸µé¼ö ÀÖ´Â ¸Þ¼­µå Áß¿¡´Â »ý¼ºÀÚ(Constructor)¿Í ¼Ò¸êÀÚ (Destructor), À̰͵éÀº ÀνºÅϽº°¡ »ý¼ºµÇ°í ¼Ò¸êµÉ¶§ ÀÚµ¿ÀûÀ¸·Î È£ÃâµÇ´Â ¸Þ¼­µåÀÌ´Ù.

»ý¼ºÀÚ´Â ÀνºÅͽºÀÇ ¿©·¯°¡Áö º¯¼ö¸¦ ÃʱâÈ­Çϰųª, ¸Þ¸ð¸® ÇÒ´çµîÀÇ ÀÛ¾÷À» À§Çؼ­ ¾²ÀδÙ. ´ÙÀ½Àº ¿À¹ö·ÎµåµÈ 2°³ÀÇ »ý¼ºÀÚ¸¦ ÀÌ¿ëÇÑ ¼ÀÇÃÄÚµåÀÌ´Ù.

#include <iostream>

using namespace std;

class vector
{
    public:
        double x;
        double y;
        vector()
        {
            x = 0;
            y = 0;
        }
        vector(double a, double b)
        {
            x = a;
            y = b;
        }

};

int main()
{
    vector k;
    cout << "vector k: " << k.x << "," << k.y << endl;

    vector m(45, 5);
    cout << "vector m: " << m.x << "," << m.y << endl;

    k = vector (22, 13);
    cout << "vector k: " << k.x << "," << k.y << endl;
}
			
ÇÏÁö¸¸ ÀÌ¹Ì ¾Õ¿¡¼­ ¹è¿î default parameter À» »ç¿ëÇϸé, ¹ø°Å·Ó°Ô overload ÇÏÁö ¾Ê°í Äڵ带 ´Ü¼øÈ­ ½Ãų¼ö ÀÖ´Ù.
#include <iostream>

using namespace std;

class vector
{
    public:
        double x;
        double y;
        vector(double a = 0, double b = 0)
        {
            x = a;
            y = b;
        }

};

int main()
{
    vector k;
    cout << "vector k: " << k.x << "," << k.y << endl;

    vector m(45, 5);
    cout << "vector m: " << m.x << "," << m.y << endl;

    vector p(5);
    cout << "vector p: " << p.x << "," << p.y << endl;
}
			

¼Ò¸êÀÚ´Â ±×¸® ÇÊ¿äÇÏÁö ¾Ê´Â°æ¿ì°¡ ¸¹´Ù. º¸ÅëÀº ÀνºÅϽº°¡ Á¦´ë·Î Á¾·áµÇ¾ú´ÂÁö È®ÀÎÇϰí, Á¾·áµÉ¶§ ¾î¶² °ªÀ» °¡Áö°í Á¾·áµÇ´ÂÁö ¾Ë°íÀÚÇÏ´Â ¸ñÀû(DEBUG) À¸·Î ¸¹ÀÌ »ç¿ëµÈ´Ù. ±×·¯³ª ¸¸¾à ÀνºÅϽº¿¡¼­ ¸Þ¸ð¸® ÇÒ´çÀ» Çß´Ù¸é (new ³ª malloc ·Î) ÀνºÅϽº¸¦ Á¾·á½Ã۱â Àü¿¡ ¹Ýµå½Ã ¸Þ¸ð¸®¸¦ ÇØÁ¦(free) ½ÃÄÑÁà¾ß ÇÑ´Ù. ÀÌ·²°æ¿ì ¼Ò¸êÀÚ´Â ¸Å¿ì À¯¿ëÇÏ°Ô »ç¿ëµÈ´Ù.

#include <iostream>

using namespace std;

class person
{
    public:
        char *name;
        int age;

    person(char *n ="no name", int a = 0)
    {
        name = new char[40];
        strncpy(name, n, 40);
        age = a;
    }

    ~person()
    {
        cout << name << " : 40 byte is free : Instance going to be deleted" << e
ndl;
        delete []name;
    }
};

int main()
{
    person me("yundream", 25);
    cout << "name is " << me.name << endl;
    cout << "age is " << me.age << endl;

    person *my;
    my = new person("hello");
    cout << "name is " << my->name << endl;
    cout << "age is " << my->age << endl;
    delete my;
    return 0;
}
			
(ÇÒ´çµÈ ¸Þ¸ð¸®´Â free ¸¦ Çϰųª ÇÁ·Î¼¼½º°¡ Á¾·áµÇÁö ¾Ê´ÂÇÑÀº Ä¿³Î¿¡ µÇµ¹·ÁÁöÁö ¾Ê´Â´Ù.)


1.17절. Ŭ·¡½º ¸Þ¼­µåÀÇ ¼±¾ð°ú Á¤ÀÇ ºÐ¸®

¸¸¾à ¸Þ¼­µå¸¦ inline À¸·Î ÀÛ¼ºÇÏ°í ½ÍÁö ¾Ê´Ù¸é, Ŭ·¡½º¿¡´Â ´ÜÁö ¼±¾ð¸¸À» Æ÷ÇÔÇÏ°Ô À¯ÁöÇϰí, ¸Þ¼­µåÀÇ ¿øÇüÀ» º°µµ·Î °ü¸®Çϵµ·Ï ÇÒ¼ö ÀÖ´Ù.

#include <iostream>
using namespace std;

class vector
{
    public:
        double x;
        double y;

        double surface();
};

double vector::surface()
{
    double s= 0;
    for (double i = 0; i < x; i++)
    {
        s = s + y;
    }
    return s;
}

int main()
{
    vector k;

    k.x = 5;
    k.y = 6;
    cout << k.surface() << endl;

    return 0;
}
			
ÀÌ·¸°Ô ºÐ¸®ÇÒ°æ¿ì inline ¿¡ ºñÇØ¼­ ¾à°£ÀÇ ¼ÓµµÀúÇϰ¡ ÀÖÀ»¼ö ÀÖ°ÚÁö¸¸, À¯Áö/º¸¼ö°¡ ¼ö¿ùÇØÁú°ÍÀÌ´Ù.


1.18절. °´Ã¼ÀÇ ¹è¿­

´ç¿¬È÷ °´Ã¼¸¦ ¹è¿­·Î ¼±¾ðÇÏ´Â °Íµµ °¡´ÉÇÏ´Ù.

#include <iostream>
#include <cmath>
using namespace std;

class vector
{
    public:
        double x;
        double y;

        vector (double a=0, double b=0)
        {
            x = a;
            y = b;
        }

        double module()
        {
            return sqrt (x*x + y*y);
        }
};

int main()
{
    vector t[3] = {vector(4,5), vector(5,5), vector(2,5)};

    cout << t[0].module() << endl;
    cout << t[1].module() << endl;
    cout << t[2].module() << endl;

    return 0;
}
			


1.19절. Ŭ·¡½º ¸â¹öº¯¼öÀÇ static ¼±¾ð

Ŭ·¡½º ¸É¹öº¯¼ö´Â static ·Î ¼±¾ðµÉ¼ö ÀÖÀ¸¸ç, static ·Î ¼±¾ðµÇ¾úÀ»°æ¿ì ¸ðµç ÀνºÅϽº¿¡¼­ °øÀ¯Çؼ­ »ç¿ëÇÒ¼ö ÀÖ´Ù. ´Ü. static À¸·Î ¼±¾ðµÈ º¯¼öÀÇ ÃʱâÈ­´Â Ŭ·¡½ºÀÇ ¹Û¿¡¼­¸¸ °¡´ÉÇÏ´Ù.

#include <iostream>
#include <cmath>
using namespace std;

class vector
{
    public:
        double x;
        double y;
        static int count;

        vector (double a=0, double b=0)
        {
            x = a;
            y = b;
            count ++;
        }

        ~vector()
        {
            count --;
        }
};

int vector::count = 0;

int main()
{
    cout << "Number of vector : " << endl;
    vector a;
    cout << vector::count << endl;
    vector b;
    cout << vector::count << endl;

    vector *r, *u;

    r = new vector;
    cout << vector::count << endl;
    u = new vector;
    cout << vector::count << endl;

    delete r;
    cout << vector::count << endl;
    delete u;
    cout << vector::count << endl;

    return 0;
}
			
À§ÀÇ vector Ŭ·¡½º´Â count ¶ó´Â static º¯¼ö¸¦ °¡Áö°í ÀÖ´Ù. ÀÌ º¯¼ö´Â ÇöÀç vector Ŭ·¡½ºÀÇ ÀνºÅϽºÀÇ °¹¼ö¸¦ °è¼öÇϱâ À§ÇÑ ¿ëµµ·Î »ç¿ëµÈ´Ù. vector Ŭ·¡½ºÀÇ »õ·Î¿î ÀνºÅϽº°¡ ¸¸µé¾îÁö¸é count ¸¦ Áõ°¡Çϰí ÀνºÅϽº°¡ ¼Ò¸êµÇ¸é count ¸¦ °¨¼Ò½ÃÅ´À¸·Î½á ÀνºÅϽºÀÇ °¹¼ö¸¦ À¯ÁöÇÑ´Ù.


1.20절. Ŭ·¡½º ¸â¹öº¯¼öÀÇ »ó¼ö¼±¾ð

Ŭ·¡½º ¸â¹öº¯¼ö°¡ static ·Î ¼±¾ðµÇ´Â°Í°ú ¸¶Âù°¡Áö·Î »ó¼ö (constant)·Î ¼±¾ðµÉ¼öµµ ÀÖ´Ù. ÀÌ º¯¼ö´Â Ŭ·¡½º¾È¿¡¼­ °ªÀÌ ÇÒ´çµÇ¸ç, ÀνºÅϽº¿¡¼­ º¯°æµÉ¼ö ¾ø´Ù. ±×·¯³ª ´ÜÁö const ·Î¸¸ ¼±¾ðÇßÀ»°æ¿ì ÄÄÆÄÀÏ·¯¿¡ µû¶ó¼­ ÄÄÆÄÀÏÀÌ ¾ÈµÉ¼öµµ ÀÖ´Ù. ¿¹¸¦µé¾î gnu ÄÄÆÄÀÏ·¯ÀÇ °æ¿ì const static ·Î ¼±¾ðÇØ¾ß µÉ°æ¿ì°¡ ÀÖ´Ù.

#include <iostream>
#include <cmath>
using namespace std;

class vector
{
    public:
        double x;
        double y;
        const static double pi = 3.1415927;

        vector (double a=0, double b=0)
        {
            x = a;
            y = b;
        }

        double cilinder_volume()
        {
            return x*x/4*pi*y;
        }
};

int main()
{
    cout << "pi is: " << vector::pi << endl;
    vector k (3,4);
    cout << "Result: " << k.cilinder_volume() << endl;
    return 0;
}
			


1.21절. this Æ÷ÀÎÅÍ

Ŭ·¡½º¿¡¼­ °¢ ¸Þ¼­µå´Â ÁÖ¼Ò¿¡ ÀÇÇÑ ¹æ½ÄÀ¸·Î ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù. ÀÌ·¸°Ô ÇÒ¼ö ÀÖ´Â ÀÌÀ¯´Â this ¶ó´Â °¡»óÀÇ Æ÷ÀÎÅÍ ¶§¹®ÀÌ´Ù. Ŭ·¡½º¿¡ ¼±¾ðµÈ ¸ðµç ¸Þ¼­µå´Â this ¸¦ ¸í½ÃÇÏÁö ¾Ê´õ¶óµµ this °¡ Àִ°ÍÀ¸·Î °£Áֵǰí ÁÖ¼Ò¿¡ ÀÇÇØ¼­ ÇÔ¼ö°¡ È£ÃâµÈ´Ù. ÀÌ·¸°Ô ÇÏ´Â ÀÌÀ¯´Â Ŭ·¡½º³»ÀÇ ¸â¹öÇÔ¼ö¸¦ °´Ã¼¿¡ ÀÇÇØ¼­ ¼ÒÀ¯Çϵµ·Ï Çϱâ À§ÇÔÀÌ ¸ñÀûÀÌ´Ù. Áï this ´Â º¸ÀÌÁö ¾Ê´Â Æ÷ÀÎÅÍ·Î °´Ã¼¿Í ¸â¹öÇÔ¼ö¸¦ ³»ºÎÀûÀ¸·Î ¿¬°áÇÏ´Â ÀÏÀ» ÇÑ´Ù.

#include <iostream>
#include <cmath>

using namespace std;


class vector
{
    protected:
        double k;
    public :
        double x;
        double y;
        vector(double a= 0, double b= 0)
        {
            x = a;
            y = b;
        }
        double module()
        {
            cout << "module " <<  x << " : " << y<< endl;
            return sqrt(x*x + y*y);
        }

        void set_length(double a = 1)
        {
            double length;
            length = this->module();

            x = x/length *a;
            y = y/length *a;
        }
};


int main()
{
    vector a(3,5);
    cout << "--> " << a.module() << endl;
    a.set_length(2);
    cout << "--> " << a.module() << endl;
    a.set_length();
    cout << "--> " << a.module() << endl;
}
			


1.22절. »ó¼Ó

Ŭ·¡½º´Â ´Ù¸¥ Ŭ·¡½º·Î ºÎÅÍ ÆÄ»ý(Derived)µÉ¼ö ÀÖ´Ù. ÀÌ »õ·Î¿î Ŭ·¡½º´Â ¿øº»Å¬·¡½ºÀÇ ¸Þ¼­µå¿Í º¯¼ö¸¦ »ó¼Ó (Inherits) ¹Þ°Ô µÈ´Ù. ÀÌ·¸°Ô ÇØ¼­ ÆÄ»ýµÈ Ŭ·¡½º´Â »õ·Î¿î ¸Þ¼­µå¿Í º¯¼öµéÀ» Ãß°¡ÇÔÀ¸·Î½á È®Àå½ÃÄÑ ³ª°¥¼ö ÀÖ°Ô µÈ´Ù.

	
#include <iostream>
#include <cmath>
using namespace std;
    
// ¿øº» Ŭ·¡½º
class vector
{
    public:
        double x;
        double y;
        const static double pi = 3.1415927; 

        vector (double a=0, double b=0)
        {
            x = a;
            y = b;
        }

        double surface()
        {
            return x * y;
        }
};

// vector ·ÎºÎÅÍ ÆÄ»ýµÈ »õ·Î¿î Ŭ·¡½º
// ¿øº» vector Ŭ·¡½ºÀÇ Ç¥¸éÀûÀ» ±¸ÇÏ´Â ÀÛ¾÷¿Ü¿¡ 
// üÀûÀ» ±¸ÇÏ´Â ÀÛ¾÷À» ÇÒ¼öÀÖµµ·Ï È®ÀåµÇ¾ú´Ù. 
class trivector: public vector
{
    public :
        double z;

        // trivector »ý¼ºÀÚ°¡ È£ÃâµÇ±â Àü¿¡
        // vector »ý¼ºÀÚ°¡ È£ÃâµÇ¾î¼­ m, n ÀÎÀÚ¸¦ 
        // ÃʱâÈ­ ÇÑÈÄ, °Å±â¿¡ 3Â÷¿ø Áö¿øÀ» À§Çؼ­ p °¡ 
        // ÃʱâÈ­ µÈ´Ù.  
        trivector(double m =0, double n =0, double p=0): vector(m,n)
        {
            z = p;
        }

        // ¶Ç´Ù¸¥ »ý¼ºÀÚ·Î ¸¸¾à¿¡ 2Â÷¿ø Á¤º¸°¡ 
        // µé¾î¿ÔÀ»°æ¿ì 3Â÷¿øÀ¸·Î º¯°æÇÑ´Ù.  
        trivector(vector a, double p = 0)
        {
            x = a.x;
            y = a.y;
            z = p;
        }

        // 3Â÷¿ø µ¥ÀÌŸ¸¦ ÀÌ¿ëÇØ¼­ üÀûÀ» ±¸ÇÑ´Ù. 
        // surface()¸Þ¼­µå¸¦ È£ÃâÇØ¼­ Ç¥¸éÀûÀ» ±¸Çϰí 
        // °Å±â¿¡ ³ôÀÌÀÎ z ¸¦ °öÇØÁÖ¸é µÈ´Ù. 
        double volume()
        {
            return this->surface() * z;
        }
};

int main()
{
    vector a(4, 5);
    trivector b(1, 2, 3);
    trivector c(a);

    cout << "surface a: " << a.surface() << endl;
    cout << "volume  b: " << b.volume() << endl;
    cout << "surface b: " << b.surface() << endl;
    cout << "volume  c: " << c.volume() << endl;

    trivector d(a,5.8);
    cout << "volume  d: " << d.volume() << endl;
}
			


1.23절. ´ÙÁß»ó¼Ó

¹Ù·Î À§¿¡¼­ »ó¼Ó¿¡ ´ëÇØ¼­ ¾Ë¾ÆºÃ´Âµ¥, C++ Àº 1°³ ÀÌ»óÀÇ Å¬·¡½º·Î ºÎÅÍ »ó¼Ó¹Þ´Â °Íµµ °¡´ÉÇÏ´Ù. ±×·¯³ª ´ÙÁß»ó¼ÓÀ» ÀÌ¿ëÇØ¼­ Ŭ·¡½º¸¦ ¸¸µé°æ¿ì ³ªÁß¿¡ À¯Áö/º¸¼ö°¡ °ï¶õÇØÁö´Â ¹®Á¦°¡ »ý±æ¼ö ÀÖÀ½À¸·Î, ´ëüÀûÀ¸·Î ´ÙÁß»ó¼ÓÀº Áö¾çÇÏ´Â Ãß¼¼ÀÌ´Ù.

#include <iostream>
#include <cmath>
using namespace std;

// ¿øº» Ŭ·¡½º
class vector
{
    public:
        double x;
        double y;
        const static double pi = 3.1415927;

        vector (double a=0, double b=0)
        {
            x = a;
            y = b;
        }

        double surface()
        {
            return x * y;
        }
};

class height
{
    public :
        double z;
        height (double a)
        {
            z = a;
        }

        int is_negative()
        {
            if (z < 0) return 1;
            else       return 0;
        }
};

class trivector: public vector, public height
{
    public :
        trivector(double a= 0, double b=0, double c=0): vector(a,b), height(c)
        {
        }
        double volume()
        {
            return fabs(x* y* z);
        }
};

int main()
{
    trivector a(2, 3, -5);

    cout << a.volume() << endl;
    cout << a.surface() << endl;
    cout << a.is_negative() << endl;
}
			


1.24절. ĸ½¶È­(Àº´Ð)

¾Æ¸¶ ´ç½ÅÀÌ C++ À» óÀ½Á¢ÇØ º¸¾Ò´Ù¸é, À§ÀÇ Äڵ忡¼­ public: ¶ó°í ÇÏ´Â »ý¼ÒÇÑ Å°¿öµå¸¦ º¸¾ÒÀ»°ÍÀÌ´Ù. À̰ÍÀº C++ ¿¡¼­ »õ·ÎÃß°¡µÈ Ű¿öµå·Î ¸Þ¼­µå³ª ¸â¹öº¯¼ö¿¡ ¿¢¼¼½º·¹º§À» ºÎ¿©ÇÏ°Ô µÈ´Ù.

public: ´Â ÇÁ·Î±×·¥¾îµð¿¡¼­µçÁö ¿¢¼¼½º ÇÒ¼ö ÀÖÀ½À» ³ªÅ¸³½´Ù. À̰ÍÀº ¿øº»Å¬·¡½º¿Í ÆÄ»ýŬ·¡½º¿¡°Ô ¸ðµÎ µ¿ÀÏÇÏ°Ô Àû¿ëµÈ´Ù.

private: ´Â ´ÜÁö ¿øº» Ŭ·¡½ºÀÇ ¸Þ¼­µå¸¦ ÅëÇØ¼­¸¸ Á¢±ÙÀÌ °¡´ÉÇÏ´Ù.

protected: private ¿Í ºñ½ÁÇÏ°Ô Å¬·¡½º ¸Þ¼­µå¸¦ ÅëÇØ¼­¸¸ Á¢±ÙÀÌ °¡´ÉÇÏÁö¸¸, private ¿Í´Â ´Þ¸® ¿øº»»Ó ¾Æ´Ï¶ó ÆÄ»ýµÈ Ŭ·¹½º¿¡¼­ÀÇ Á¢±Ùµµ °¡´ÉÇÏ´Ù.

#include <iostream>
#include <cmath>

using namespace std;


class vector
{
    private:
        double x;
        double y;
    public :
        double surface()
        {
            return x * y;
        }
};

int main()
{
    vector b;
    b.x = 2; // ÄÄÆÄÀÏ ¿¡·¯¹ß»ý
    b.y = 3; // ÄÄÆÄÀÏ ¿¡·¯¹ß»ý
}
			
À§ÀÇ °æ¿ì c++ ÄÄÆÄÀÏ·¯·Î ÄÄÆÄÀÏÇÒ°æ¿ì `double vector::x' is private ¿Í °°Àº ¿¡·¯¸Þ½ÃÁö¸¦ Ãâ·ÂÇϰí ÄÄÆÄÀÏ ÁßÁöµÈ´Ù. vector Ŭ·¡½ºÀÇ ¸â¹öº¯¼ö x, y ´Â private ·Î ¼±¾ðµÇ¾î ÀÖÀ½À¸·Î ´ÜÁö ÇöÀç Ŭ·¡½ºÀÇ ¸Þ¼­µå¸¦ ÅëÇØ¼­¸¸ Á¢±Ù°¡´ÉÇϱ⠶§¹®ÀÌ´Ù. ÀÌ·²°æ¿ì x, y ÀÔ·ÂÀ» À§ÇÑ Àü¿ë ¸Þ¼­µå¸¦ Çϳª ¸¸µé¾î¾ß ÇÒ°ÍÀÌ´Ù.
#include <iostream>
#include <cmath>

using namespace std;


class vector
{
    private:
        double x;
        double y;
    public :
        double surface()
        {
            return x * y;
        }
        void input(double a, double b)
        {
            x = a;
            y = b;
        }
};

int main()
{
    vector b;
    b.input(11, 40.5);
    cout << b.surface() << endl;
}
			


1.25절. °¡»óÇÔ¼ö

¿øº»Å¬·¡½º¿¡¼­ ÆÄ»ýµÈ »õ·Î¿î Ŭ·¡½º´Â ¿øº» Ŭ·¡½ºÀÇ ¸Þ¼­µå¿Í ¸â¹öº¯¼ö¸¦ »ó¼Ó¹Þ´Â´Ù´Â °ÍÀ» ¹è¿ö¼­ ¾Ë°í ÀÖ´Ù. ±×·±µ¥ ÀÌ·±°æ¿ì¸¦ »ý°¢ÇÒ¼ö ÀÖÀ»°ÍÀÌ´Ù. vector ¿¡ module ¶õ ¸Þ¼­µå°¡ Àִµ¥, ÀÌ ¸Þ¼­µå´Â ´ÙÀ½°ú °°´Ù.

double module()
{
    return sqrt(x*x + y*y);	
}
			
¸¸¾à vector ¿¡¼­ ÆÄ»ýµÈ trivector À̶õ Ŭ·¡½º¸¦ ¼±¾ðÇß´Ù¸é, trivector Ŭ·¡½º´Â vector->module() ¸¦ »ó¼Ó¹Þ°Ô µÉ°ÍÀÌ´Ù. ±×·¯³ª trivector ÀÇ °æ¿ì sqrt ¿¬»êÀ» ÇÒ¶§ 3Â÷¿ø µ¥ÀÌŸ¸¦ °¡Áö°í ¿¬»êÀ» ÇØ¾ß ÇÒ°ÍÀÌ´Ù.
double module()
{
	return sqrt(x*x + y*y + z*z);
}
			
ÀÌó·³ ¸Þ¼­µå¸¦ »ó¼Ó¹Þ¾ÒÀ»¶§, »ó¼Ó¹ÞÀº ¸Þ¼­µåÀÇ ¿¬»ê¹æ½ÄÀÌ º¯°æµÉ°æ¿ì virtual ·Î ¼±¾ðÇÏ¸é µÈ´Ù.
#include <iostream>
#include <cmath>

using namespace std;


class vector
{
    public :
        double x;
        double y;
        virtual double module()
        {
            return sqrt(x*x + y*y);
        }
};

class trivector: public vector
{
    public :
        double z;

    trivector(double m=0, double n=0, double p=0)
    {
        x = m;
        y = n;
        z = p;
    }

    double module()
    {
        return sqrt(x*x + y*y + z*z);
    }
};

int main()
{
    trivector b(2,3,4);
    cout << b.module() << endl;
}
			


1.26절. ÆÄÀϽºÆ®¸² ó¸®

C++ Àº ÆÄÀÏ󸮸¦ À§ÇÑ ¸Å¿ì °£´ÜÇÑ ¹æ¹ýÀ» Á¦°øÇÑ´Ù. ´ÙÀ½Àº ÆÄÀÏÀ» Àбâ À§ÇÑ ÄÚµåÀÌ´Ù.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream f;
    char c;

    f.open("seek3.c", ios::in);
    while (! f.eof())
    {
        f.get(c);
        cout << c;
    }

    f.close();

    return 0;
}
			
´ÙÀ½Àº ÆÄÀÏ¿¡ ¾²±â À§ÇÑ ÄÚµåÀÌ´Ù.
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;

int main()
{
    fstream f;
    f.open("text.txt", ios::out);

    f << "Hello world " << endl;

    f.close();
    return 0;
}
			


1.27절. Á¤ÇüÈ­µÈ Ãâ·Â

º¸Åë Ç¥ÁØ C ¾ð¾î¿¡¼­´Â printf ¸¦ ÀÌ¿ëÇØ¼­ Á¤ÇüÈ­µÈ Ãâ·ÂÀ» ¼öÇàÇÑ´Ù. C++ ¿¡¼­´Â width() ¿Í setw()¸¦ ÀÌ¿ëÇØ¼­ Á¤ÇüÈ­µÈ Ãâ·ÂÀ» ÇÑ´Ù. À̰͵éÀº ´ÜÁö °¡ÀåÃÖ±ÙÀÇ Ãâ·Â¿¡¸¸ ¿µÇâÀ» ¹ÌÄ£´Ù.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    for (int i = 1; i <= 1000; i *=2)
    {
        cout.width(7);
        cout << i << endl;
    }
    for (int i = 0; i <=10 ;i ++)
    {
        cout << setw(3) << i << setw(5) << i * i * i << endl;
    }
    return 0;
}
			


1.28절. ¹®Àڹ迭À» file ó·³ÀÌ¿ëÇϱâ

Á» ÀÌ»óÇϰÔ(ȤÀº ¾µ¸ð¾ø´Â °Íó·³) µé¸±¼ö ÀÖ°ÚÁö¸¸, ¹®Àڹ迭À» ÆÄÀÏó·³ ¿¬»êÇÏ´Â°Ô °¡´ÉÇÏ´Ù. À̰ÍÀº ÆÄÀÏ ½ºÆ®¸²°ú ¸Þ¸ð¸®¸¦ ¿¬°áÇØ¼­ »ç¿ëÇÏ´Â ÇÁ·Î±×·¡¹Ö ±â¹ýÀ» °¡´ÉÇϵµ·Ï ÇØÁØ´Ù.

#include <iostream>
#include <cmath>
#include <cstring>
#include <strstream>

using namespace std;

int main()
{
    char a[1024];
    ostrstream b(a, 1024);

    b.seekp(0); // ½ºÆ®¸²ÀÇ Ã¹¹øÂ°·Î À̵¿ 
    b << "2+2 = " << 2+2 << ends; // ends ÀÓ¿¡ ÁÖÀÇ
    cout << a << endl;

    double v = 49;
    strcpy(a, "A sinus: ");

    b.seekp(strlen(a));
    b << "sin (" << v << ") = " << sin(v) << ends;
    cout << a << endl;
}
			
#include <iostream>
#include <cmath>
#include <cstring>
#include <strstream>

using namespace std;

int main()
{
    char a[1024];
    istrstream b(a, 1024);

    strcpy(a, "45.656");
    double k, p;

    b.seekg(0);
    b >> k;

    k = k+1;
    cout << k << endl;

    strcpy(a, "444.23 56.89");
    b.seekg(0);
    b >> k >> p;

    cout << k << ", " << p + 1 << endl;
    return 0;
}
			


category_C++
category__2
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.