본문 바로가기

2010년 6월 10일 목요일

반응형

<복습>

   

.cpp

   

<iostream>

   

std::cout<<

cin>>

   

   

for(int i=0) //변수선언 어디서나.

   

  • bull 변수 //true or false

    예>bool a

    a=true

    false

       

  • referance

    예>int& ref = 10(X) //변수 이름에다가 하나 더 붙이는 것이지 상수를 붙일 수는

    없다.

       

    int& ref = a

    ref =5 //ref 값이 5로 바뀜

       

    단 여기에 const를 사용하면 변경 불가능.

    int& ref = a

    ref =5(X)

       

    p250참고)

    const int* ref = &a; //a의 주소값이 들어가 있다. a는 5를 가지고 있다.

    //ref가 가르키는 값을 고정시킨다. = *ref는 수정을 할 수 없다.(ref가 가르키는 값을 읽기만 하겠당ㅋ,

    *ref에 들어가는 값을 고정시키겠다) >>위에 놈 고정.

       

    int* const ref = &a; //a를 가르키는 ref블록을 고정시키겠다.

    //*ref = 7 이 가능 a = 7 로 변경된다.

    //ref = &b; <<<요놈은 불가능 a를 가르키는 것을

    고정시켜 놨으므로 그걸 바꾸라고 하니 안됨.

    //즉 a만 가르킴. >>밑에 놈 고정

       

       

    <new>

       

    함수 오버로딩.(p360~ 365 참고)

       

    void func( );

    void func(int a ); //매개변수의 데이터 타입이나 개수가 다르면 다른 함수로

    void func( int a, int b);//생각한다.

    //C에서는 위에 꺼를 똑같은 거로 생각한다.

       

       

    #include <iostream>

       

    using namespace std;

       

    void function (void)

    {

      cout<<"function(void) call"<<"\n";

    }

       

    void function(char c)

    {

      cout<<"function(char c) call"<<"\n";

    }

       

    void function(int a , int b)

    {

      cout<<"function(int a , int b) call"<<"\n";

    }

    int main(void)

    {

      function();

       

      function('a');

       

      function(1213);

    }

       

       

       

       

       

    리턴타입이 다르면 함수 오버로딩이 불가능하다. 무엇을 호출해야 될지 모르기 때문이다.

       

      #include <iostream>

        

      using namespace std;

        

      int function (char d)

      {

        cout<<"function(void) call"<<"\n";

        return 1;

      }

        

      void function(char c)

      {

        cout<<"function(char c) call"<<"\n";

      }

        

        

      int main(void)

      {

        

        

        function('a');         //컴파일왈~char 두갠데 어디로???

        

        function(1213);

      }

    ***************************************************

    >>이건 가능하다.

       

      #include <iostream>

        

      using namespace std;

        

      int function (char d)

      {

        cout<<"function(void) call"<<"\n";

        return 1;

      }

        

      void function(char c)

      {

        cout<<"function(char c) call"<<"\n";

      }

        

        

      int main(void)

      {

        

        

        function('a');

        

        function(1213);

      }

       

       

    #include <iostream>

       

    using namespace std;

       

    int function (int a = 0)

    {

      return a+1;

    }

       

    int main(void)

    {

      cout<< function(11)<<"\n"//전달하는 값이 있는 경우 11이들어감

      cout<<function()<<"\n";  //달하는 값이 없으면 함수 정의하는 곳에

            // 값이 들어간다.

       

      return 0;

    }

       

       

    화면 캡처: 2010-06-10, 오전 9:52

       

       

    #include <iostream>

       

    //using namespace std;

       

    int BoxVolume(int lengthint width = 1 , int height = 1); //디폴트 매개변수는 

                      //항상 오른쪽 모아놔야 한다

       

    int main()

    {

       

      std::cout <<" [3, 3, 3]  :"<<BoxVolume( 333<<std::endl;   //앞에서부터 입려된다.

      std::cout <<" [5, 5, def]  :"<<BoxVolume( 55<<std::endl;

      std::cout <<" [7, def, def]  :"<<BoxVolume( 7<<std::endl;  

      return 0;

       

    }

       

    int BoxVolume(int lengthint width, int height)

    {

      return length*width*height;

    }

       

       

       

    화면 캡처: 2010-06-10, 오전 10:17

       

    함수오버로딩과 디폴트 메개변수를 혼용해서 쓰면 안 된다. 함수 오버로딩만 사용

    하는 것이 좋다. <컴파일왈~어디로 가란말이야 !!

       

       

       

    전역변수가 들어가는 곳.(메모리)

       

       

    bss(초기화안한 것) data(초기화 한 것) -전역 ,static<프로그램이 종료하면

    메모리에서 사라짐.

    heap(동적)

       

       

    stack(지역 변수) >>>코드블록 내에서만 사용가능.

       

       

       

       

    동적메모리 할당(p390)

       

    필요한 만큼만 메모리를 사용하기 위해 만든 것이 동적메모리 할당.

    메모리 낭비를 방지하기 위해서 …

       

    new라는 명령어를 사용해서 변수를 선언한 다음에는 delete라는 명령어를 사용

    해서 지워줘야 한다.

       

    #include <iostream>

       

    using std::cin;

    using std::cout;

    using std::endl;

       

    int main()

    {

       

      int size;

      cout <<할당하고자 하는 배열의 크기 :";

      cin >> size;

       

      int *arr = new int [size];

       

      for(int i=0; i<size; i++)

        arr[i] = i + 10;

       

      for(int j=0; j<size; j++)

        cout<<"arr["<<j<<"] = "<< arr[j]<<endl;

       

      delete []arr;

       

       

      //int arr[size];  //배열의 크기가 잡혀 있지 않아서 에러발생.

        

      //return 0;

    }

       

       

       

    //동적 메모리는 프로그램을 실행한 다음에 메모리를 할당.

    //일반 변수는 컴파일 타임에 메모리를 할당.

       

       

       

       

    화면 캡처: 2010-06-10, 오전 11:16

       

       

    #include <iostream>

    using namespace std;

       

    int main()

    {

      int size;

      cout << " 개의 정수를 입력하시겠소? ";

      cin >> size;

       

        

       

      int* arr = new int [size]; //필요한 만큼 메모리를 할당한다.

       

      cout << 정수를 입력하시오.\n";

      for(int i = 0; i <size; ++i)

        cin >> arr[i];

       

      int sum = 0;

      for (i =0; i < size; ++i)

      {

        sum+= arr[i];

      }

      float ave = (float)sum / (float)size;

      cout <<  = " << sum << ", 평균 = " << ave << "\n";

       

      delete[] arr;

        

      return 0;

    }

       

       

       

    화면 캡처: 2010-06-10, 오후 12:14

       

       

       

       

       

    #include <iostream>

    using namespace std;

       

    int main()

    {

      short* p = new short [100];

       

      cout << "p = " << p << "\n";

       

      delete[] p;

       

      cout << "p = " << p << "\n";

       

      delete[] p;

       

      return 0;

    }

       

       

       

       

    화면 캡처: 2010-06-10, 오후 12:25

       

       

    지운 것을 또 지웠는데 오류가 발생하지 않는다. 왜 이러지 나야 되는데

       

       

    #include <iostream>

    using namespace std;

       

    int main()

    {

      short* p = new short [100];

       

      cout << "p = " << p << "\n";

       

      delete[] p;

      p = NULL;

       

      cout << "p = " << p << "\n";

       

      delete[] p;

      p = NULL;

       

      return 0;

    }

       

       

       

       

    화면 캡처: 2010-06-10, 오후 12:28

       

       

    NULL을 넣어주면 아무 이상 없이 동작

       

       

    <오후>

    'a' - a가 저장되는 것이 아닌 a의 아스키코드가 저장된다.

    ' ' 안에는 하나의 글자만 저장된다.

    '\n' 글자 하나이다. 하나의 의미를 가진다.

       

    집 = 나 >>내가 집에 간다 ㅋㅋㅋ

    " "를 끝에 0을 집어 넣는다. NULL문자가 입력된다. 끝에 들어가는 0도 아스키

    코드가 입력된다.

       

    앞에 const 가르키는 곳이 상수

    뒤에 const 변수 자체가 상수

       

       

       

       

       

   

   

화면 캡처: 2010-06-10, 오후 2:33

   

   

   

   

   

   

   

   

   

   

직접 문자 측정 함수 제작

   

   

#include <stdio.h>

   

int strlen2(const char*);

   

int main()

  {

    int inum;

    inum = strlen2("test");

    printf("string length : %d\n" , inum);

    return 0;

   

  }

   

   

   

int strlen2(const char* p )

   

{

  int icount;

  for(icount=0 ; 0 != *p  ; ++icount )

  {

   

  ++p;

    

   

  }

    

   

  return icount;

   

}

   

   

   

   

화면 캡처: 2010-06-10, 오후 3:55

   

   

   

   

#include <stdio.h>

#include <malloc.h>

   

int main()

{

 char *p;

 int size;

 int *dynp;

   

 p = "abc";

   

   

 dynp = malloc(100);

 free(dynp);

   

 printf("   p 주소            : %p\t\n\n", p, p);

    

 printf("   main 주소         : %p\n", main);

 printf("   printf 주소       : %p\n\n", printf);

   

 printf("   동적할당메모리 주소 : %p\n\n", dynp);

   

 printf("   %s 문자열 주소     : %p\t\n""abc""abc");

   

 return 0;

   

   

   

   

   

   

api = 라이브러리 공부

   

   

   

   

   

   

   

   

   

반응형
-->