c++

c++ 이론 이것 저것

2024. 2. 2. 19:44
글 목차


728x90

c++은 함수가 기억된 메모리의 주소를 기억하고 가져올 수 있습니다.

#include<iostream>
using namespace std;

int p(int a, int b) {return a + b;}
int m(int a, int b) {return a - b;}
int c(int a, int b, int (*cal)(int, int)) {return cal(a, b);}

int main() {
    cout << c(1,4,&m);
}

 

m 함수의주소를 c함수의 인자로 사용해서 m 함수를 호출하는 간단한 코드입니다.

 

심지어 auto라는 타입을 사용해서 더 간단하게 수정할 수도 있습니다.

#include<iostream>
using namespace std;

int p(int a, int b) {return a + b;}
int m(int a, int b) {return a - b;}
int c(int a, int b, auto (*cal)) {return cal(a, b);}

int main() {
    cout << c(1,4,&p);
}

 

 

c++ 의 참조자는 대입되는 변수와 같은 값, 같은 주소값을 가집니다.

#include<iostream>
using namespace std;

int main() {
    int x;
    int& y = x;
    
    cout << &y << " " << &x;
}

 

인라인 함수는 코드 자체를 삽입하는 함수에요 빨라져요, 근데 함수의 이점이 사라집니다.

#include<iostream>
using namespace std;

inline int sum(int a, int b) {return a + b;}

int main() {
    cout << sum(1, 2);
}

 

 

#include 뒤에 <> 와 " "의 차이

<>는 시스템 헤더 파일이나 표준 라이브러리의 헤더 파일을 나타내고 " " 안의 헤더파일은 프로젝트 파일 내부를 뒤져요

 

네임스페이스 사용법

.h로 끝나는 파일을 만들어요

namespace kim {
    int a;
    int b = 5;
}

.cpp 파일에서 이를 사용해요

#include "space.h"
#include <iostream>
using namespace std;
using kim::b;

int main() {
    cout << b;
}

 

클래스를 만들어 볼게요

#include <iostream>
using namespace std;

class Car {

    int num;
    
public:
    Car (int num) {
        this->num = num;
    }
    
    int getNum() {
        return num;
    }
};

int main() {
    Car car(2222);
    cout << car.getNum();
}

 

연산자 오버로딩

#include <iostream>
using namespace std;

class Node {
public:
    int r_, c_;
    Node (int r, int c) : r_(r), c_(c) {}
    Node operator+(const Node& other) const {
        return Node(r_ + other.r_, c_ + other.c_);
    }
};

int main () {
    Node n1(1, 2);
    Node n2(5, 4);
    cout << (n1 + n2).r_;
}

 

정적 멤버 변수

클래스의 모든 인스턴스가 공유한다는 것,, 자바랑 같은 듯

#include <iostream>
using namespace std;

class Node {
public:
    static int x;
    int k;
    Node (int k) {
        this->k = k;
    }
    void display() {
        cout << x << " " << k << '\n';
    }
};

int Node::x = 0;

int main () {
    Node n1 = Node(1);
    Node n2 = Node(2);
    n1.x = 3;
    n1.display();
    n2.display();
}

 

프렌드 함수

프렌드 함수는 이항연산자 오버로딩에 쓰이는 것 같다. 연산자 함수의 첫번째 인수가 해당 클래스가 아닌 경우 직접적으로 클래스 멤버로 오버로딩을 할  수 없는 것 때문인 듯,,

#include <iostream>
using namespace std;

class Node {
private:
    int r;
    int c;
public:
    Node(int r, int c) {
        this->r = r;
        this->c = c;
    }
    Node operator+(int k) {
        return Node(this->r * k, this->c * k);
    }
    friend Node operator+(int k, Node n) {
        return n + k;
    }
    void display() {
        cout << r << " " << c;
    }
};

int main () {
    Node node = 3 + Node(1, 2);
    node.display();
}

 

프렌드 클래스

어떤 클래스와 크게 연관이 있는 클래스의 경우 그 클래스를 프랜드 클래스로 두게 되면 private 멤버에 접근 할 수 있어서 편리하다

#include <iostream>
using namespace std;

class Node {
private:
    int r;
    int c;
public:
    Node(int r, int c) {
        this->r = r;
        this->c = c;
    }
    friend class Display;
};

class Display {
public:
    void display(Node* target) const {
        cout << target->r << " " << (*target).c;
    }    
};

int main () {
    Node node = Node(2, 3);
    Display d = Display();
    d.display(&node);
}

 

클래스의 상속과 오버라이딩

스트링을 매개변수로 줄 때는 참조변수로 주는게 특이한 점인듯, 외엔 자바랑 대동소이

#include <iostream>
using namespace std;

class Person {
private:
    int age;
    string name;
public:
    Person(int age, const string& name) {
        this->age = age;
        this->name = name;
    }
    int getAge() {
        return age;
    }
    string getName() {
        return name;
    }
    void greeting() {
        cout << " 사람입니다.\n";
    }
};

class Student : public Person {
private:
    int student_id;
public:
    Student(int sid, int age, const string& name) : Person(age, name) {
        student_id = sid;
    }
    int getSid() {
        return student_id;
    }
    void greeting() {
        cout << " 학생입니다.\n";
    }
};

int main () {
    Student s = Student(111, 20, "홍길동");
    Person p = Person(12, "이순신");
    int sid;
    int age;
    string name;
    sid = s.getSid();
    age = s.getAge();
    name = s.getName();
    cout << sid << " " << age << " " << name;
    s.greeting();
    age = p.getAge();
    name = p.getName();
    cout << age << " " << name;
    p.greeting();
}
728x90
c++ 이론 이것 저것