블로그 이미지
송시혁

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

Notice

Tag

Recent Post

Recent Comment

Recent Trackback

Archive

2013. 11. 25. 15:46 c++


함수 오버로딩 (이미 생성자에는 2개의 변수가 0으로 초기화 된 상태)

문자열의 길이를 구한 다음, 메모리를 동적할당 받아 확보한 후, 

해당 문자열을 널까지 포함하여 cpString에 복사한다.


복사 함수, 내용은 위에 함수와 흡사. 레퍼런스 변수를 인자로 받는다. 


소멸자. 동적 메모리를 제거한다. 


cout << ???형태를 함수로 만들기 위한 과정이다. 

cout은 ostream형이고 포인터가 인자가 된다. 


<<에 해당하는 함수이다. 첫 번째 인자의 ,cout의 주소를 받고, 출력할 대상이 되는 2번째 인자로 가진다.

위에 있는 print()함수를 호출하기만 하면 cout <<??형태가 가능해진다.

반환형이 ostream&라는 것에 유의하자.


대입 연산자 함수

+연산자 함수









posted by 송시혁
2013. 11. 25. 10:13 c++



연산자 오버로딩 결과


실행 결과에 따른 설명.

1. 위의 함수를 보면서 생각해 보자. 클래스형 변수가 생성되면서 디폴트가 생성된다.

2. 아래 cout 내용(cout <<"덧셈 연산자 호출\n";)

3. temp가 반환된다. 임시 객체 생성 이유는 나중에 main()함수에서 obj1에 대입하는데 연산한 결과를 임시로 저장

    공간이 필요하기 때문이다. 따라서 복사 생성자도 같이 호출이 된다. 

4. temp가 함수가 종료함에 따라 사라짐으로 소멸자 생성. temp는 객체 변수.

5. 아래 main()함수에서 덧셈결과를 왼쪽에 대입하므로 대입 연산자 호출

6. 결과가 나왔음으로 임시 객체가 필요없다. 따라서 임시 객체 소멸.
























posted by 송시혁
2013. 11. 21. 16:15 c++




함수의 가장 왼쪽의 virtual을 붙이면 가상함수가 된다. 거기에 0을 대입하면 순수가상함수가 되며, 

이 순수 가상함수를 지니고 있는 객체를 추상클래스라고 부른다.



명칭부호화 







어셈블리에서 명칭부호화를 확인 할 수 있다.



위의 그림은 명칭 부호화가 되어있다. 







소스에서 extern "C"를 붙이고 어셈블리 파일로 컴파일 하면 다음과 같이 사라진다. 그러나 한 개의 함수만 붙일 수 있다.


그래서 위의 소스와 같이 extern "C"를 함수 2개의 붙이고 컴파일하면 에러가 뜬다.  반드시 한 개의 함수만 할

수 있다. 아래 그림은 제일 위 test()함수만 extern "C"를 붙여놓은 경우이다.








smart는 추상클래스이다. 아래 그림도 객체를 만들 수 없다라는 에러를 띄울 것이다. 












posted by 송시혁
2013. 11. 13. 19:05 c++


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

class car
{
  public:
    string Color;
    string Vender;
    string Model;

    car()//생성자 
    {
      Color="없음";                                    
      Vender="없음";
      Model="없음";
      cout << "car class 생성자 구동\n";
    }

    void print()
    {
      cout <<"Color  = "  << Color <<endl;
      cout <<"Vender = "  << Vender <<endl;
      cout <<"Model  = "  << Model <<endl;
    }

    ~car()//소멸자 생성.
    {
      cout << "car class 소멸자 구동 시작\n";
      print();
      
      cout << "car class 소멸자 구동 끝\n";
    }
};


class bmw_745
{
  public:
    string Color;
    string Vender;
    string Model;

    bmw_745()
    {
      Color="white";
      Vender="bmw";
      Model="745Li";
      cout << "bmw class 생성자 구동\n";
    }
  
    ~bmw_745()
    {
      cout << "bmw_745 class 소멸자 구동 시작\n";
      print();
      
      cout << "bmw_745 class 소멸자 구동 끝\n";
    }
    void print()
    {
      cout <<"Color  = "  << Color <<endl;
      cout <<"Vender = "  << Vender <<endl;
      cout <<"Model  = "  << Model <<endl;
    }
  
    

};

//class Mornig:car

class Mornig:public car  //class car를 상속받는다.
{
  public:
    
    Mornig()
    {
      Color="red";
      Vender="kia";
      Model="morning";
      cout << "Mornig class 생성자 구동\n";      
    }

    ~Mornig()
    {
      cout << "Mornig class 소멸자 구동 시작\n";
      print();
      
      cout << "Mornig class 소멸자 구동 끝\n";
    }
};




int main()
{
  car aCar;
  aCar.Color="blue";
  aCar.Vender="bmw";
  aCar.Model="sonata";
  
//  aCar.print();

  bmw_745 Mycar;
//  Mycar.print();

  Mornig Momcar;
//  Momcar.print();

  return 0;
}

/*
 상속받은 클래스는 부모생성자와 자식생성자 2개를 생성한다.
 (실행결과 3,4행은 Mornig class의 의해서 생성된것.)  
 소멸자는  역순으로 자식생성자, 부모생성자순으로 소멸된다.

*/






'c++' 카테고리의 다른 글

11월 25일 연산자 오버로딩 (정리 중)  (0) 2013.11.25
11월 21일 추상클래스  (0) 2013.11.21
11월 13일 상속 클래스 기초1.  (0) 2013.11.13
11월 12일 파일 임의 접근  (0) 2013.11.13
11월 11일 fstream 클래스  (0) 2013.11.11
posted by 송시혁
2013. 11. 13. 12:18 c++


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

class car
{
  public:
    string Color;
    string Vender;
    string Model;

    void print()
    {
      cout <<"Color  = "  << Color <<endl;
      cout <<"Vender = "  << Vender <<endl;
      cout <<"Model  = "  << Model <<endl;
    }
};

//여기서 car는 부모 class


//아래 class는 car class와 동일한 클래스를 상속받지 않고 만든 클래스.


class bmw_745
{
  public:
    string Color;
    string Vender;
    string Model;

    bmw_745()
    {
      Color="white";
      Vender="bmw";
      Model="745Li";
    }
    void print()
    {
      cout <<"Color  = "  << Color <<endl;
      cout <<"Vender = "  << Vender <<endl;
      cout <<"Model  = "  << Model <<endl;
    }
  
    

};

//-------------------------------------------------------------------
//상속받은 클래스


//class Mornig:car

/*
1. 상속은 ':'를 사용하고 뒤에 class를 지정한다.
   여기서는 mornig class가 카의 class를 상속받는다.

2. 그러면, 카의 들어있는 Color, Vender, Model변수를 가진다.
   함수 void print()또한 가지게 된다.

3. main()함수에  상속받은 mornig class를 사용하기 위해서는
   ':' class중간에 'public'을 삽입한다. 왜냐하면 void print()
   함수는 상속받은 mornig class 안에 존재하지만,  private상태로 존재한다. 

*/


class Mornig:public car
{
  public:
    
    Mornig()
    {
      Color="red";
      Vender="kia";
      Model="morning";      
    }

};




int main()
{
  car aCar;
  aCar.Color="blue";
  aCar.Vender="bmw";
  aCar.Model="sonata";
  
  aCar.print();

  bmw_745 Mycar;
  Mycar.print();

  Mornig Momcar;
  Momcar.print();

  return 0;
}


'c++' 카테고리의 다른 글

11월 21일 추상클래스  (0) 2013.11.21
11월 13일 상속 클래스의 생성과 소멸  (0) 2013.11.13
11월 12일 파일 임의 접근  (0) 2013.11.13
11월 11일 fstream 클래스  (0) 2013.11.11
11월 11일 출력 형식 지정자  (0) 2013.11.11
posted by 송시혁
2013. 11. 13. 10:06 c++


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

int main()
{
  fstream fp;
  int score;

  fp.open("sample.txt", ios::in|ios::binary);
  //파일에서 읽기 모드, 이진파일 모드
  if(true==fp.fail())
  {
    return 1;
  }
  //실패시 종료
  while(fp.read((char *)&score, 4), false==fp.eof())
  {
    cout << setw(3<< right << score << endl;
  }
  //4바이트만큼 score에 4바이트 만큼 읽는다.
  //파일의 끝이 아닐 때 까지 while문은 반복한다.

  fp.seekg(4, ios::cur);
  //파일에 입력할때 지정한 만큼 파일 포인터가 이동한다.
  //1. 바이트 2. 이동 기준 설정. 여기서는 현재위치가 기준이다(ios:cur)
  //ios::end= 파일의 끝 위치에서 이동, ios::beg= 파일의 처음위치에서 이동.

  fp.read((char *)&score, 4);
  //다시 4바이트 만큼 읽는다.
  cout << setw(3<< right << score << endl;
  fp.close();

  return 0;
}


posted by 송시혁
2013. 11. 11. 19:14 c++


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

int main()
{
  fstream fs;      //파일 객체 fs 생성.
  
  fs.open("test.txt", ios::out);  
  //open함수 뒤에 두번째 인자로 ios::out 설정,
  //파일을 출력모드

  if(true==fs.fail())//open함수 실행 여부 검사.
  {
    return 0;
  }
  
  fs << "keyboard" << endl;//파일에 문자열 "keyboard"를 저장.
  fs << "monitor" << endl;// 파일에 문자열 "monitor"를 저장. 
  
  fs.close();    //파일을 닫음.
  
  fs.open("test.txt", ios::app); //ios::app는 파일 추가 모드 
  fs << "desk" << endl;//파일에 문자열 desk를 추가로 저장.
  fs.close();  //파일을 닫음.

  fs.open("test.txt", ios::in);
  //ios::in는 파일에서 내용을 읽기 모드 설정.
  string temp;
  //문자열 변수 temp에 저장.
  
  //파일에 객체를 읽고 파일의 끝이 아니면 반복문 계속 
  while(fs >>  temp, !fs.eof())
  {
    cout << temp << endl;// temp에 담겨져 있는 내용 화면에 출력
  }
  
  fs.close();

  return 0;
}


posted by 송시혁
2013. 11. 11. 19:13 c++

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

int main()
{
  int a=1234;
  
  cout << "a= " << a << endl;    //a의 값 출력
  cout << "a= " << setw(5<< a << endl;  //출력폭이 5이므로 빈칸까지 출력
  cout << "a= " << hex << setw(5<<<< endl;//헥사값으로 빈칸까지 출력.
  cout << "a= " << dec << setw(5<<<< endl;//10진수로 빈칸까지 출겨하여 5자리 맞춤.

  double b=45.8769;
  
  cout << "b= " << b << endl;//45.8769출력.
  cout << "b= " << fixed << b << endl;//실수형을 본래 형태로 출력.

  cout << "b= " << setprecision(0<< b << endl;
  //소수점 이하 자리수. 0으로 설정하면 자연수만 나온다. 단, 반올림한다.

  double c = 123;

  cout << "c= " << c << endl;//123출력
  cout << "c= " << setprecision(0<<  c << endl;//자연수 123출력
  cout << "c= " << showpoint << c << endl;
  //소수점 출력. 여기서는 소수점이 없기 때문에 .만 출력
  //반드시 출력.
  cout << 987.0 << endl;
  
  

  return 0;
}


posted by 송시혁
2013. 11. 11. 18:54 c++

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

int main()
{
  ifstream input;
  
  input.open("FileTest.txt");

  //fail()함수는 반환형이 bool형이다. 
  //성공시 true, 실패시 false를 반환한다.
  //bool형의 크기는 1

  if(true==input.fail())//파일의 존재 여부 확인 if~else문
  {
    cout << "파일이 존재하지 않습니다!!" <<endl;
    return 0;
  }
  else
  {
    cout << "파일 열기 후 실행~~~ "<< endl;
    input.close();
  }

  return 0;
}
//---------------------------------------------------------------------------------------------------

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

int main()
{
  ofstream output;  //출력 파일 객체 생성.
  char cont='y';
  char word[50]={0, };

  output.open("test.txt");

  if(true==output.fail())  //파일 존재 여부 확인 
  {
    return 1;
  }
  
  while(cont=='y'||cont=='Y')  
  {
    cout << "단어입력: ";
    cin >> word;    //키보드로부터 입력
    
    output << word << endl; 
    //변수 word는 출력 객체로서 내용을 파일에 저장한다.
    //endl을 받았으므로 엔터까지 저장된다.

    cout << "계속하시겠습니까? (y/n) ";
    cin >> cont;    
    //y나 대문자 Y를 누른경우 계속해서 while문을 반복하여
    //파일에 입력한 내용을 저장한다. 
  }  
  output.close();

  return 0;
}


//---------------------------------------------------------------------------------------------------

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

int main()
{
  ifstream input;    //입력 파일 객체 생성.(파일에서 읽어오기 위함.)
  char word[50]={0, };
  
  input.open("test.txt"); //
  if(true==input.fail())  //파일 존재 여부 검사.
  {
    return 1;
  }

  while(false==input.eof()) //파일의 끝이 아니면~~
  {
    input >> word;    //문자열 변수 word에 파일의 있는 문자열 저장.
    cout << word << endl; //화면에 출력.
  }
  input.close();

  return 0;
}

//객체.eof() 는 파일의 끝인지 아닌지 판단한다.
//반환형이 bool형이다.
//끝이면, true, 아니면 false를 반환한다.


'c++' 카테고리의 다른 글

11월 11일 fstream 클래스  (0) 2013.11.11
11월 11일 출력 형식 지정자  (0) 2013.11.11
11월 11일 파일 클래스  (0) 2013.11.11
11월 7일 생성자와 소멸자 순서  (0) 2013.11.07
11월 6일 생성자와 소멸자  (0) 2013.11.06
posted by 송시혁
2013. 11. 11. 18:38 c++

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

int main()
{
  ofstream output;    //output 객체 생성
  output.open("text.txt");  
  //output 객체로 파일 open, text.txt파일이 생성이 된다.
  output << "즐거운 프로그래밍!!!" <<endl;
  text.txt 파일 안에 내용이 써진다.
  
  output.close();//닫는다. c에서 close()함수와 같다.

  return 0;
}

output 객체를 사용시 ofstream을 사용한다. 
파일과 관련되 입출력은 fstream헤더 파일에서 제공한다.
객체.open("파일 이름"); , 점은 객체 구성원에 대한 참조 연산자.

//------------------------------------------------------------------------------------------------------------------------------

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

int main()
{
  ifstream input;    //입력 파일 객체 생성.
  char message[80];
  
  input.open("test.txt"); //파일을 연다. 
  input >> message;  //파일에 내용을 message 버퍼에 저장
  cout << message << endl;//화면에 출력.(파일에 내용을)
  input.close();  


  return 0;
}


posted by 송시혁
prev 1 2 next