one님의 블로그

[C++로 시작하는 객체지향 프로그래밍] 내용 정리 3 본문

학과 공부/C++

[C++로 시작하는 객체지향 프로그래밍] 내용 정리 3

one14 2024. 10. 27. 15:03

■ void f(int* p1, int* &p2) , f(q1, q2)

     ⇒  q1은 값에 의한 전달, *p1변경시, *q1변경, p1=q3로 바꾸면 q1변경X

     ⇒  q2은 참조에 의한 전달, *p2변경시, *q2변경, p2=q3로 바꾸면 q1변경O

     - void m(int list[], int size) == void m(int* list, int size)

 

#include <algorithm>

     - min_element(list, list+6) 최소 요소에 대한 포인터 반환

     - max_element, sort, random_suffle, find 포인터 반환

 

■ 동적메모리 할당 int* p = new int(4);   int는 동적 배열

     - new 로 할당된 메모리 delete/ 프로그램 종료 시까지 존재

     - delete p; (or) delete [] llist; (배열일 때)

     - new에 의해 할당된 메모리: freestore/heap 안에 존재

     - (*p).length() == p->length()

 

 

■ this 포인터: 객체의 데이터 필드 참조 시, this->data= data 이런식

 

■ 소멸자(destructor): 객체 삭제시, 자동호출됨 (~Circle(), 틸드문자(~)가짐)

 

■ operator [] 함수: string 클래스의 멤버함수

     - vector, operator+, operator< : string클래스의 비멤버함수

     -예) s1.operator[] (0), operator+(s1, s2), operator <(s1, s2)

 

■ bool operator<(Rational &secondRational) const

     - r1.operator<(r2) == r1<r2

 

■ 오버로딩에 의해 연산자 우선순위와 결합성 변경 불가

     - 오버로딩에 의해 피연산자 개수 변경불가 (예) ++는 단항연산자

 

■ 오버로딩 불가능 연산자 : ‘ ?: ‘ , ‘ . ‘ , ‘ .* ‘ , ‘ :: ‘

 

■ 첨자 연산자 [] (subscript operator)

     - (예) int& operator[](int index)

     - [] 연산자는 접근자, 변경자로 동작

 

■ 증강대입연산자 (+=, -=, *=, /=, %=) Lvalue 연산자임

     - Rational& Rational::operator+=(const Rational &secondRational)

     - r2 = r1 += Rational(2, 3)

 

■ 단항연산자 오버로딩 Rational Rational::operator-()

     - Rational r3=-r2

 

■ ++, --연산자 Rational Rational::operator++() 전위 증가, Lvalue

     - Rational Rational::operator++(int dummy) : 후위증가, Lvalue X

 

■ class Date{ … friend class a; …}; class a{… Date birthdate(2010,3,4);…}; 가능

string s2("Welcome");

s2.append(" to C and C++", 0, 5); //Welcome to C
s2.append(" to C and C++", 5); //Welcome to C
s2.assign("Dallas, Texas", 0, 5); //Dalla
s2.append(" to C and C++", 0, 5); //Dalla
string s1("Welcome");
string s2("Welcomg");

s1.compare(s2) //-2

s1.substr(3) //come

s1.substr(3,3) //com

s1.find("co") // 3

s1..find("co", 5) //-1 (없음) , string::npos 반환함

s2.insert(1, 4, 'B'); //WBBBBelcomg

 

 

stringstream ss;
ss << 3.1415;
string s = ss.str();  //itoa

   - eof() : 문자열 스트림 내 모든 항목 읽었을 때, True 반환

 

※  참조에 의한 전달이 더 효율적

 

getline(cin, city, '\n'); //엔터 기준으로 입력받음

 

Circle circleArray[3] = {Circle(3), Circle(4), Circle(5)}; //객체 배열

 

728x90