'Programming/C++ STL'에 해당되는 글 64건

  1. 2013.03.11 다중상속의 문제점
  2. 2013.03.11 상속시 public, private 키워드는 하나씩만 적용된다
  3. 2013.03.04 c++ function overloading 2
  4. 2013.03.04 c++ class member function
  5. 2013.03.04 c++ namespace
  6. 2013.03.03 c++ class와 struct
  7. 2013.02.15 c++ cout 제어하기
  8. 2013.02.15 c++ inheritance(상속)
  9. 2013.02.15 c++ template
  10. 2013.02.14 Essential C++
Programming/C++ STL2013. 3. 11. 19:07
계륵이라는 느낌
일단 내용만 보고 느껴지는 문제는
다중상속시 다른 클래스들을 파악해야 하므로
모듈/캡슐화를 통해 모르고 끌어쓰는 편리함을 포기해야 한다는 점?

물론 평행다중 상속이 아닌
수직다중 상속 같운 경우에도 scope문제로 바뀔뿐
재사용성을 위한 상속이 재사용성을 해치는 문제가 발생한다

[링크 : http://gyumee.egloos.com/3200829
[링크 : http://www.winapi.co.kr/clec/cpp3/29-3-2.htm]
[링크 : http://www.langdev.org/posts/20]
[링크 : http://cplusplus.com/doc/tutorial/inheritance/
Posted by 구차니
Programming/C++ STL2013. 3. 11. 19:04
포인터 변수 같은 느낌?

class deri : public a,b
에서
a는 public
b는 private로 된다고 하니 주의해야할듯

[링크 : http://www.gpgstudy.com/forum/viewtopic.php?topic=11211]

'Programming > C++ STL' 카테고리의 다른 글

visual studio express에서의 상속 클래스  (0) 2013.03.13
다중상속의 문제점  (0) 2013.03.11
c++ function overloading  (2) 2013.03.04
c++ class member function  (0) 2013.03.04
c++ namespace  (0) 2013.03.04
Posted by 구차니
Programming/C++ STL2013. 3. 4. 23:35
영어사전을 뒤져보면, overriding은 우선시 되서 하는 것이고 overloading은 과적이다.
overriding이 상속으로 함수의 내용을 갈아 엎는걸 의미하는 것을 보면 '최우선시'가 적당한것 같고
overloading은 하나의 이름으로 여러가지 일을 하는 것이니 '과적'도 적당한것 같고

그런데 막상 단어만 두면 엄청 헷갈리는 녀석이다 -_-

overriding 미국식 [|oʊvər|raɪdɪŋ] play 영국식 [|əʊvə|raɪdɪŋ] play 
다른 무엇보다 더 중요한, 최우선시 되는
overloading
(조선공학) 과적(過積)(고봉싣기)  



class CRectangle {
int width, height;
public:
CRectangle ();
CRectangle (int,int);
int area (void) {return (width*height);}
};

CRectangle::CRectangle () {
width = 5;
height = 5;
}

CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}

int _tmain(int argc, _TCHAR* argv[])
{
CRectangle rect (3,4);
CRectangle rectb;
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}  

일단 overloading의 제약사항은 아래의 세가지 이다.
동일 return type
동일 function name 
다른 function variable

c++의 동적 타입 바인딩을 통해서
컴파일 / 런타임 시에 입력받는 변수형을 추적하여 해당 함수로 연결해줄수 있지만
리턴되는 값이 들어가는 곳에 대해서는 형변환이 있을수도 있으니 어떤걸로 할 수 없으니 사용이 불가능 하다고
들은거 같긴한데 먼소리인지 모르겠고 -_- 

'Programming > C++ STL' 카테고리의 다른 글

다중상속의 문제점  (0) 2013.03.11
상속시 public, private 키워드는 하나씩만 적용된다  (0) 2013.03.11
c++ class member function  (0) 2013.03.04
c++ namespace  (0) 2013.03.04
c++ class와 struct  (0) 2013.03.03
Posted by 구차니
Programming/C++ STL2013. 3. 4. 23:15
class는 struct 에서 함수를 포함하는 개념인데
물론, class안에서 함수를 선언할수 있지만 확실히 보기 쉽지 않아지는게 문제이니
되도록이면 class 안에는 prototype만 선언하고 class 밖에서 함수 본체를 만들어 주는게 나을 듯하다.

class CRectangle {
int *width, *height;
public:
CRectangle (int,int);
void ~CRectangle ()
{
delete width;
delete height;
}
int area () {return (*width * *height);}
};

void CRectangle::CRectangle (int a, int b) {
width = new int;
height = new int;
*width = a;
*height = b;
}

int _tmain(int argc, _TCHAR* argv[])
{
CRectangle rect (3,4), rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
 

1. 생성자/소멸자는 리턴형이 존재하지 않는다.
    error C2577: 'CRectangle' : 소멸자은(는) 반환 형식을 가질 수 없습니다.
    error C2533: 'CRectangle::{ctor}' : 생성자에서 반환 형식을 사용할 수 없습니다.

2. class 안에 함수를 넣어도 무방하다
class name {
    function_name() { /* function content */ } 
    function_prototype(); 
}

return_type name::function_prototype()
{

[링크 : http://www.cplusplus.com/doc/tutorial/classes/]


---
그리고 c++에서 부터는 prototype에 변수의 형만 적고, 변수명을 생략가능해진다.
CRectangle (int,int);
CRectangle::CRectangle (int a, int b) { /* ... */ }

'Programming > C++ STL' 카테고리의 다른 글

상속시 public, private 키워드는 하나씩만 적용된다  (0) 2013.03.11
c++ function overloading  (2) 2013.03.04
c++ namespace  (0) 2013.03.04
c++ class와 struct  (0) 2013.03.03
c++ cout 제어하기  (0) 2013.02.15
Posted by 구차니
Programming/C++ STL2013. 3. 4. 00:18
namespace는 어떻게 보면 java에서의 패키지와 유사한 묶음에 대한 접근관리라고 하면 되려나?

아래와 같은형식으로 선언이 되며
namespace _spacename_
{


다른 namespace 에서는 동일한 변수 명을 선언해도 문제가 없다.
대신에 :: scope 연산자를 통해서
어떠한 영역의 변수인지를 알려주어야 한다.

물론 namespace 역시 선언되지 이전에 먼저 사용할수는 없으며
사용시에는 에러가 발생한다.
using namespace std;
using namespace first;

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

int _tmain(int argc, _TCHAR* argv[])
{
cout << x << endl;
cout << y << endl;
cout << second::x << endl;
cout << second::y << endl;
return 0;
} 

cpp_console.cpp(7) : error C2871: 'first' : 같은 이름을 가진 네임스페이스가 없습니다.
cpp_console.cpp(23) : error C2065: 'x' : 선언되지 않은 식별자입니다.
cpp_console.cpp(24) : error C2065: 'y' : 선언되지 않은 식별자입니다. 

물론, using은 중복되는 여러개를 사용할 수 있으나
using namespace std;

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

using namespace first;

int _tmain(int argc, _TCHAR* argv[])
{
cout << x << endl;
cout << y << endl;
cout << second::x << endl;
cout << second::y << endl;
return 0;
} 
[링크 : http://www.cplusplus.com/doc/tutorial/namespaces/]

아무래도 네임스페이스 내에 어떤 변수가 있을지 모르니
되도록이면 여러개의 using namespace는 쓰지 않는게 정신건강에 좋을듯 하다. 
실험은 조금 더 해봐야 겠지만..
해제하는 명령은 존재하지 않는것 같기에
using namespace는 {} 에 영향을 받으므로 블럭으로 감싸서 어느정도 회피는 가능하다고 한다.
[링크 : http://blog.naver.com/harkon/120061325101]

'Programming > C++ STL' 카테고리의 다른 글

c++ function overloading  (2) 2013.03.04
c++ class member function  (0) 2013.03.04
c++ class와 struct  (0) 2013.03.03
c++ cout 제어하기  (0) 2013.02.15
c++ inheritance(상속)  (0) 2013.02.15
Posted by 구차니
Programming/C++ STL2013. 3. 3. 23:48
struct가 변수만 모아놓을수 있었다면
class는 struct에서 확장되어 함수까지 포함하는 개념이다.

단, c++에서 struct로도 함수를 포함해 선언할 수 있지만 class member가 public으로 선언되고
class로 선언시에는 private로 선언되는 차이가 있다고 한다.
[링크 : http://www.dal.kr/chair/cpp/cpp313.html ]

class의 접근제어는
public:
private:
protected: 
로 만들어 지며

class testclass
{
private:
    int priv_a;

public:
    int pub_a;

private:
    int priv_b;
식으로 접근을 제어할 수 있다.
단, 기본적으로 private로 되고 선언된 아래로는 끝까지 이어지니 구획을 구분해서 쓰는게 용이하고
private는 위에서 서술하였지만, 기본적으로 설정이 되니 일반적으로는 public: 만 명시적으로 사용한다.

물론 constructor / destructor 도 강제로(?) private로 만들수는 있지만
그럼 그걸 어떻게 쓸래? 라는 문제가 발생하니 생성자와 파괴자는 public: 으로 선언하자

using namespace std;

class CRectangle {
int width, height;
CRectangle (int,int);

public:
int area () {return (width*height);}
};

CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}

int _tmain(int argc, _TCHAR* argv[])
{
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}

cpp_console.cpp(25) : error C2248: 'CRectangle::CRectangle' : private 멤버('CRectangle' 클래스에서 선언)에 액세스할 수 없습니다.
cpp_console.cpp(11) : 'CRectangle::CRectangle' 선언을 참조하십시오.
cpp_console.cpp(9) : 'CRectangle' 선언을 참조하십시오.
cpp_console.cpp(26) : error C2248: 'CRectangle::CRectangle' : private 멤버('CRectangle' 클래스에서 선언)에 액세스할 수 없습니다.
cpp_console.cpp(11) : 'CRectangle::CRectangle' 선언을 참조하십시오.
cpp_console.cpp(9) : 'CRectangle' 선언을 참조하십시오. 

[링크 : http://www.cplusplus.com/doc/tutorial/classes/ ]

'Programming > C++ STL' 카테고리의 다른 글

c++ class member function  (0) 2013.03.04
c++ namespace  (0) 2013.03.04
c++ cout 제어하기  (0) 2013.02.15
c++ inheritance(상속)  (0) 2013.02.15
c++ template  (0) 2013.02.15
Posted by 구차니
Programming/C++ STL2013. 2. 15. 23:52
그냥 sprintf 쓰는게 편할지도..

#include <iomanip>
를 포함해서 사용하며
setiosflag()
setfill()
setw()
setprecision()
함수등을
"cout <<" 이후에 넣어서 설정을 하여 사용한다.


[링크 : http://arachnoid.com/cpptutor/student3.html]
[링크 : http://www.cplusplus.com/reference/iolibrary/]
[링크 : http://msdn.microsoft.com/ko-kr/library/943z481t.aspx]

'Programming > C++ STL' 카테고리의 다른 글

c++ namespace  (0) 2013.03.04
c++ class와 struct  (0) 2013.03.03
c++ inheritance(상속)  (0) 2013.02.15
c++ template  (0) 2013.02.15
Essential C++  (0) 2013.02.14
Posted by 구차니
Programming/C++ STL2013. 2. 15. 23:44
java를 안쓰고 c만 쓰다 보니
class나 class의 상속에 대한 개념만 알지, 직접 사용해본적이 없어서 잘 모르겠지만..

아무튼 java의 extend 키워드 대신 c++에서는
: 를 이용해서 상속을 하게 된다.

그리고 java에서는 복잡성의 문제로 다중상속을 지원하지 않지만
c에서는 다중상속을 지원하므로 , 로 여러개의 클래스를 적어줄 수 있다.

class derived_class_name: public base_class_name;
class derived_class_name: public base_class_name, public base_class_name;

[링크 : http://www.cplusplus.com/doc/tutorial/inheritance/



다중상속 예
class CRectangle: public CPolygon, public COutput; // CPolygon과 COutput 클래스로 부터 상속
class CTriangle: public CPolygon, public COutput;

'Programming > C++ STL' 카테고리의 다른 글

c++ class와 struct  (0) 2013.03.03
c++ cout 제어하기  (0) 2013.02.15
c++ template  (0) 2013.02.15
Essential C++  (0) 2013.02.14
참조에 의한 전달(pass by reference)  (0) 2013.02.09
Posted by 구차니
Programming/C++ STL2013. 2. 15. 23:38
template의 식별자로서
typename 과 class는 키워드만 다를뿐 동일한 작동을 한다고 한다.

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

[링크 : http://www.cplusplus.com/doc/tutorial/templates/


---
타입을 넣지 않고 숫자만 해도 일단은 되는데 아마도 이러면 int 형으로 인식을 하겠..지?

#include <iostream>

using namespace std;

template <class myType>
myType GetMax (myType a, myType b)
{
        return (a>b?a:b);
}

int main()
{
        cout << GetMax(1,2);
        return 0;
} 

아무튼, template <> 안에 갯수에 따라서 그거 보다 작은 숫자의 형을 지정할 수는 있지만 그걸 넘는 숫자를 지정할 수는 없다.
template <class myType>
myType GetMax (myType a, myType b)
{
        return (a>b?a:b);
}

int a,b;

GetMax<int>(a,b); // okay
GetMax<int,int>(a,b); // error

---
int a;
short b;
GetMax<int>(a,b); // okay 

그나저나.. short 형으로 해도 큰 문제는 없지만(영역을 안 넘으면) char로 하면
C에서 처럼 0~255 범위가 아닌 문자로 인식을 하기 때문에 비교를 제대로 못하는 것 같이 작동한다.
template <class T, class U>
T GetMin (T a, U b)
{
  return (a<b?a:b);
}


int a;
short b;
GetMin<int>(a,b); //okay
GetMin<int,int>(a,b); //okay

'Programming > C++ STL' 카테고리의 다른 글

c++ cout 제어하기  (0) 2013.02.15
c++ inheritance(상속)  (0) 2013.02.15
Essential C++  (0) 2013.02.14
참조에 의한 전달(pass by reference)  (0) 2013.02.09
C++ 첫걸음 *-_-*  (0) 2013.02.09
Posted by 구차니
Programming/C++ STL2013. 2. 14. 23:59
시간이 없어서, 피곤해서 잘 못읽고 있는데
c++ 내용을 압축해서 하다 보니 읽기에는 좋은듯 한 책이다.


아무튼 C의 확장으로서 C++을 설명하는데
객체지향 보다는 템플렛을 이용한 범용 프로그래밍(제너릭 프로그래밍)이
오히려 C++의 강점이 아닐까? 라는 생각이 들게 한 책이다.

책 내용이 객제지향보다 제너릭 프로그래밍이 먼저인 이유가 있다면 말이다..


아무튼, stdlib 등에서 제공하는 sort등을 써본적이 없이 직접구현했던 이유중에 하나가
라이브러리로 존재하지만 이걸 사용하기에는 부족한게 많았고 변형하다 보면
결국에는 새로 짜는 셈이 되다보니 활용도가 낮았는데

c++ 에서는 이러한 표준 라이브러리의 효율성을 올리기 위해 변수 타입을 주고 받고
템플릿을 통해 범용 함수를 만들고, 함수 객체를 통해 손쉽게 함수 포인터를 대체 함으로서
더욱 강력하고 안정적이며 빠른 프로그래밍을 추구한 느낌을 받았다.
이에 비하면 객체지향은 구색 맞추기라는 느낌이라고 해야하려나..



템플렛(template)은 두개의 키워드 template와 typename으로 선언되고 추가적으로  < >를 사용한다.
template <typename T> return_type function name(T val)

T는 매크로 처럼 치환되어 여러개의 함수가 타입별로 생성되는 효과를 지닌다.
자세한건 짜보고 정리..

[링크: http://ikpil.com/725]

'Programming > C++ STL' 카테고리의 다른 글

c++ inheritance(상속)  (0) 2013.02.15
c++ template  (0) 2013.02.15
참조에 의한 전달(pass by reference)  (0) 2013.02.09
C++ 첫걸음 *-_-*  (0) 2013.02.09
unsigned char -> int 변환 주의사항  (0) 2013.02.04
Posted by 구차니