티스토리 뷰

MFC

[MFC] Template (템플릿) cpp 중요 문법

상추님 2019. 11. 13. 14:12
190719-2
 
템플릿 등
 

 
C++ 의 중요 문법
  • 함수 중복
  • new, delete
  • inline 함수
  • static 메서드 / 멤버변수
  • 객체 생성/소멸
  • 객체 복사
  • 객체 생성순서
  • 상속-다형성
    • 추상화 (인터페이스)
  • 템플릿
    • 함수 템플릿
    • 클래스 템플릿
  • 형식변환
  • 연산자 중복
  • RTTI (런타임 타입 인포메이션)
 

 
C언어 VS C++
 
C
#include <iostream>
using namespace std;
int main() {
    int n = 10;
    double d = 5.5;
 
    cout << n << " , " << d << endl;
}
 
C++
#include <iostream>
using namespace std;
int main() {
    int n(10);
    double d(5.5);
 
    cout << n << " , " << d << endl;
}
 
C++
#include <iostream>
using namespace std;
int main() {
    int n = int();
    double d = double(); // 기본값이나 기본생성자
 
    cout << n << " , " << d << endl;
}
 
#include <iostream>
using namespace std;
int main() {
    int n = int(5.6); // C의 형식변환은 (int)5.6 이었으나 C++에선 이와같은 형식변환도 지원한다.
    double d = double('A');
 
    cout << n << " , " << d << endl;
}
5 , 65
 
- 레퍼런스(&)
#include <iostream>
using namespace std;
int main() {
    int n = 10;
    int& m = n; // 래퍼런스(&)
 
    cout << n << " , " << m << endl;
    n = 20;
    cout << n << " , " << m << endl;
}
10 , 10
20 , 20
 
#include <iostream>
using namespace std;
int main() {
    int n = 10;
    int& m = n; // 래퍼런스(&)
    int*p = &n;
 
    cout << n << " , " << m << endl;
    n = 20;
    cout << &n << " , " << &m << endl;
    cout << &*p<< endl;
 
}
10 , 10
0098FDC8 , 0098FDC8
0098FDC8
 

 
객체의 관계 클래스들의 관계를 그림으로 그리는 것이 중요하다.
C++ 에서 메모리 그림을 그리는 것이 중요하다.
 
[const]
#include <iostream>
using namespace std;
int main() {
    int n = 10;
    const int* cn = &n;
 
    cout << n << " , " << *cn << endl;
}
 
#include <iostream>
using namespace std;
int main() {
    int n = 10;
    const int* pn = &n;
 
    n = 100;//가능
    *pn = 100;//불가능
 
    cout << n << " , " << *pn << endl;
}
 
#include <iostream>
using namespace std;
int main() {
    int n = 10;
     int* const pn = &n;
 
    n = 100;//가능
    *pn = 100;//가능
    
    int m = 20;
    pn = &m; // 불가능
 
    cout << n << " , " << *pn << endl;
}
 
 
const 덕에 안정성이 높아진다.
 
#include <iostream>
using namespace std;
int main() {
    int n = 10;
    const int* const pn = &n;
 
    n = 100;//가능
    *pn = 100;//불가능
 
    int m = 20;
    pn = &m;//불가능
 
    cout << n << " , " << *pn << endl;
}
 
 
#include <iostream>
using namespace std;
void Print(int& n) {
    cout << "int : " << n << endl;
}
int main() {
    int m = 10;
    Print(m);
}
 
#include <iostream>
using namespace std;
void Print(const int& n) {
    cout << "int : " << n << endl;
    //n = 0; 불가
}
int main() {
    int m = 10;
    Print(m);
    cout << m << endl;
}
 
c++ 에선 중요하게 사용하는 것이 const이다.
 
기본 형식에는 래퍼런스(&)를 굳이 쓰지 않는다. 결국 포인터(4byte등)가 넘어가기 때문이다.
래퍼런스는 객체타입을 쓸 때 쓴다. (객체를 던지면 전부 복사되기 때문에 참조를 사용하는 것)
이 때 const와 함께 사용한다.
 


기본 매개변수 값
#include <iostream>
using namespace std;
 
// 기본 매개변수 값 설정 (default parameter value)
void Print(const char* s = "Hello!", int n = 10, double d = 5.1) {
    cout << s << ',' << n << ',' << d << endl;
}
int main() {
    Print();
    Print("HI");
}
 
함수 중복 (오버로딩)
#include <iostream>
using namespace std;
 
void Print(){
    cout << "Print()" << endl;
}
void Print(int n) {
    cout << "Print(int n)" << endl;
}
void Print(double d) {
    cout << "Print(double d)" << endl;
}
int main() {
    Print();
    Print(1.1);
    Print(int(200));
}
Print()
Print(double d)
Print(int n)
 
 
 
#include <iostream>
using namespace std;
 
int main() {
    int* p = new int(10);
    cout << *p << endl;
    delete p;
}
 
#include <iostream>
using namespace std;
 
int main() {
    int* p = new int[10]; // 10개
    cout << *p << endl;
    delete[] p; // 대괄호 사용시 10개 메모리를 제거해준다.
}
 
#include <iostream>
using namespace std;
 
int main() {
    int* p = new int[10];
    p[0] = 10;
    cout << p[0] << endl;
    for (int i = 0; i < 10; i++)
        p[i] = (i + 1) * 10;
    for (int i = 0; i < 10; i++)
        cout << p[i] << endl;
    delete[] p;
}

[namespace]
#include <iostream>
using namespace std;
 
namespace MyA {
    void Print() {
        cout << "A - Print()" << endl;
    }
}
namespace MyB {
    void Print() {
        cout << "B - Print()" << endl;
    }
}
 
int main() {
    MyA::Print();
    MyB::Print();
}
A - Print()
B - Print()
 
using MyB::Print;
int main() {
    Print();
}
 
using namespace MyB;
int main() {
    Print();
}
댓글
최근에 올라온 글
최근에 달린 댓글
네이버 이웃추가
«   2024/03   »
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
31
글 보관함