본 문서는 인프런의 [하루 10분|C++] 누구나 쉽게 배우는 C++ 프로그래밍 입문 강의를 공부하며 작성한 개인 노트입니다.
분할 컴파일: 프로그램의 세 부분을 나누어서 컴파일
#include <iostream>, using namespace std 포함#define이나 const를 사용하는 기호 상수struct.h (.h가 헤더 파일)#include "struct.h"로 연결(파일명).cpp#include "struct.h"#ifndef ~ #endif#ifndef STRUCT
#include <iostream>
using namespace std;
struct MyStruct 
{
	string name;
    int age;
};
void display(MyStruct&);
#endif추상화: 어떠한 객체를 사실적으로 표현하는 것이 아니라 공통된 특징을 간결한 방식으로 이해하기 쉽게 표현
클래스: 추상화를 사용자 정의 데이터형으로 변환해주는 수단
#include <iostream>
using namespace std;
class Stock
{
private:
	string name;
    int shares;
    float share_val;
    double total_val;
    void set_total() { total_val = shares * share_val; }
    
public:
	void acquire(string&, int, float);
    void buy(int, float);
    void sell(int, float);
    void update(float);
    void show();
    Stock();
    ~Stock();
};
void Stock::acquire(string& co, int n, float pr) {
	name = co;
    shares = n;
    share_val = pr;
    set_total();
}
void Stock::buy(int, float) {
	shares += n;
    share_val = pr;
    set_total();
}
void Stock::sell(int n, float pr) {
	shares -= n;
    share_val = pr;
    set_total();
}
void Stock::update(float pr) {
	share_val = pr;
    set_total();
}
void Stock::show() {
	cout << "회사 명 : " << name << endl;
    cout << "주식 수: " << shares << endl;
    cout << "주가 : " << share_val << endl;
    cout << "주식 총 가치 : " << total_val << endl;
}
	
int main() {
	Stock temp;
    temp.acquire("Samsung", 100, 1000);
    temp.show();
    temp.buy(10, 1200);
    temp.show();
    temp.sell(5, 800);
    temp.show();
    
    return 0;
}::): 클래스에 종속된 메서드로 설정void Stock:: acquire(string&, int, flat) {}Stock.h 파일에 using namespace~클래스 선언 분리func.cpp 파일에 함수 정의 분리 후 #include "Stock.h"#include "Stock.h"cpp에서 클래스 생성자와 파괴자를 기본적으로 제공해줌으로 정의하지 않더라도 문제가 되지는 않음
생성자 정의 (acquire 함수 대체)
Stock::Stock(string co, int n, float pr)
{
	name = co;
    shares = n;
    share_val = pr;
    set_total();
}main에 사용
int main() {
	Stock temp = Stock("Samsung", 100, 1000);
}파라미터를 받지 않고 기본값으로 초기화하는 생성자
Stock::Stock() {
	name = "";
    shares = 0;
    share_val = 0;
    set_total();
}파괴자
~ 붙임 (예) Stock::~Stock()this: 멤버 함수를 호출하는데 쓰인 객체를 의미
Stock Stock::topval(Stock& s) {
	if (s.share_val > share_val) 
    	return s;
    else return this;
}클래스 객체 배열 선언: 표준 데이터형 선언 방식과 같음
Stock s[4] = {
	Stock("A", 10, 1000), 
    Stock("B", 20, 1200),
    Stock("C", 30, 1300)
};(예) func.cpp의 메서드, main.cpp에서 예시
func.cpp
Stock &Stock::topval(Stock& s) {
	if (s.share_val > share_val)
    	return s;
    else return *this;int main() {
	Stock s[4] = {
	Stock("A", 10, 1000), 
    Stock("B", 20, 1200),
    Stock("C", 30, 1300)
	};
	
    Stock *first = &s[0];
    for (int i = 1; i <4; i++) 
    	first = &first->topval(s[i]);
    
    first->show();
   	
    return 0;
}