• 객체와 객체 배열의 동적 생성 및 반환


객체의 동적 생성과 생성자도 동일하다.

1
2
3
4
5
6
7
8
9
10
/*
    클래스이름 *포인터변수 = new 클래스이름;    //기본생성자호출
    클래스이름 *포인터변수 = new 클래스이름(생성자매개변수리스트);    //매개변수 있는 생성자 호출
    delete 포인터변수;
*/
Circle *= new Circle;    //기본생성자 호출
Circle *= new Circle(30);    //생성자 Circle(30)호출
 
delete p;
delete q;
cs



객체 배열의 동적 생성과 생성자도 동일하다.

1
2
3
4
5
6
7
8
9
/*
    클래스이름 *포인터변수 = new 클래스이름 [배열 크기];    //기본생성자 호출
    delete [] 포인터변수;  
*/
Circle *= new Circle[3];    //기본생성자 호출
Circle *= new Circle[3] { Circle(1), Circle(2), Circle(3) };    //다음과 같이 초기화 가능.
 
delete [] p;
delete [] q;
cs



  • this 포인터

this는 객체 자신에 대한 포인터로서 클래스의 멤버함수 내에서만 사용된다.

this는 전역변수도 아니고, 지역 변수도 아닌 객체 자신에대한 주소이다.


나 자신, 너 자신, 엄마 자신, 아빠 자신 등과 같이 각 사람마다 자기 자신이 있듯이,

객체에도 각 객체마다 this가 있다. 즉 자기 자신에 대한 포인터 이다.


아래의 코드를 보면 매개변수가 있는 생성자에 매개변수를 radius로 받고 있다.

하지만 this-> 를 사용함으로써, this->radius는 멤버 radius가 되고, 그냥 radius는 매개변수 radius가 된다.

기본 생성자 함수의 경우는 this-> 를 생략하고, radius=1 로만 해도 무관하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Circle{
    int radius;
public:
    Circle(){
        this->radius=1;
    }
    Circle(int radius){
        this->radius=radius;
    }
    void setRadius(int radius){
        this->radius = radius;
    }
};
cs



this의 제약조건은 2가지가 있다.

첫번째로, 멤버함수 에서만 사용 가능하고,

두번째로, 정적 멤버함수(static member function)는 this를 사용할 수 없다.


그런데 this는 어디에도 선언하지 않았는데 왜 오류가 나지 않을까?

사실 this는 컴파일 과정에서 모든 멤버함수들에 묵시적으로 삽입이 되어 있다. 다음과 같이 말이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Circle{
    int radius;
public:
    Circle(Circle* this){
        this->radius=1;
    }
    Circle(Circle* thisint radius){
        this->radius=radius;
    }
    void setRadius(Circle* thisint radius){
        this->radius = radius;
    }
};
cs



  • String 클래스를 이용한 문자열 사용

다음과 같이 string클래스를 넣어주고, append로 문자열을 추가할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
 
using namespace std;
 
int main(){
    
    string s = "I love";
    s.append(" C++");
    cout<<s<<endl;
 
    return 0;
}
cs


string의 동적 객체 생성은 다음과 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
 
using namespace std;
 
int main(){
    
    string *= new string("C++");
    cout<<*s<<'\n';
    
    s->append("great!");
    cout<<*s<<'\n';
    
    delete s;
 
    return 0;
}
cs



문자열 삽입

1
2
string s("I love C++");
s.insert(2"really ");    // s = I really love C++
cs


문자열 길이

1
2
3
string s("I love C++");
int leng = s.length();    // leng = 10
int size = s.size();    // size = 10
cs


문자열 삭제

1
2
3
4
5
6
7
string s("I love C++");
 
s.erase(07);
cout<<s<<'\n';    // s = C++
 
s.clear();
cout<<s<<'\n';    // s = ""
cs



서브스트링

1
2
3
string s("I love C++");
cout<<s.substr(24)<<'\n';    // 인덱스 2에서 4개문자 리턴 s = "love" 
cout<<s.substr(2)<<'\n';    // 인덱스 2에서 끝까지 리턴 s = "love C++"
cs



문자열 검색 (문자나 문자열의 첫 번째 인덱스 리턴)

1
2
3
string s("I love C++");
cout<<s.find("love")<<endl;     //s에서 "love"검색. 인덱스 2리턴.     
cout<<s.find('C'3)<<endl;        //인덱스 3부터 'C'문자 검색. 인덱스 7리턴.
cs



문자열의 숫자 변환, stoi(), string to int

1
2
string n="2019";
int num = stoi(n);
cs





Posted by Jyoel :