객체(object)
라고 한다.instanciation
이라고 하고 만들어진 것을 instance
라고 한다.class Friend
{
public: // 접근 제어자access specifier (public, private, protected)
string name;
string address;
int age;
double height;
double weight;
void print()
{
cout << name << " " << address << " " << age << " " << height << " " << height << endl;
}
};
int main()
{
Friend jj { "Jack", "seoul", 2, 30, 10 }; // instanciation, instance
jj.print();
vector<Friend> my_friends;
my_friends.resize(2);
for(auto &ele : my_friends)
ele.print();
return 0;
}
public
: 클래스 밖에 서도 접근 가능private
: 클래스 내에서만 접근 가능protected
public
으로 설정하지않으면private
이 기본이다.
private
으로 설정된 것을 접근하기 위해서는 접근 함수(access function)을 만들어줘야 한다.
class Date
{
int m_month;
int m_day;
int m_year;
public:
void setDate(const int& month_input, const int& day_input, const int& year_input)
{
m_month = month_input;
m_day = day_input;
m_year = year_input;
}
void copyFrom(const Date &original)
{
m_month = original.m_month;
m_day = original.m_day;
m_year = original.m_year;
}
void setMonth(const int& month_input)
{
m_month = month_input;
}
//setDay, setYear ... // setters
const int& getDay() // getters
{
return m_day;
}
};
int main()
{
Date today; // { 8, 4, 2025 ];
today.setDate(8, 4, 2025);
today.setMonth(10);
cout << today.getDay() << endl;
Date copy;
copy.copyFrom(today);
return 0;
}
class Fraction
{
private:
int m_numerator;
int m_denominator;
public:
// 파라미터가 없는 생성자를 만들거나, 생성자 함수에 기본 값을 넣거나
Fraction(const int& num_in = 1, const int& den_in = 1)
{
m_numerator = num_in;
m_denominator = den_in;
}
void print()
{
cout << m_numerator << " / " << m_denominator << endl;
}
};
int main()
{
Fraction one_thirds(1, 3);
one_thirds.print();
Fraction no_constructor;
no_constructor.print();
return 0;
}
1 / 3
1 / 1
class B
{
private:
int m_b;
public:
B(const int& m_b_in)
: m_b(m_b_in){}
};
class Something
{
private:
int m_i;
double m_d;
char m_c;
int m_arr[5];
B m_b;
public:
Something()
: m_i(1), m_d(3.14), m_c('a'), m_arr{ 1,2,3,4,5 }, m_b(m_i - 1)
{
// m_i = 1;
// m_d = 3.14;
// m_c = 'a';
}
void print()
{
cout << m_i << " " << m_d << " " << m_c << endl;
for (auto& e:m_arr)
cout<<e<<" ";
cout << endl;
}
};
int main() {
Something s;
s.print();
return 0;
}
class Student
{
private:
int m_id;
string m_name;
public:
// 위임 생성자 사용
Student(const string& name_in)
: Student(0, name_in) {}
Student(const int& id_in, const string& name_in)
: m_id(id_in), m_name(name_in) {}
void print()
{
cout << m_id << " " << m_name << endl;
}
};
int main()
{
Student st1(0, "Jack Jack");
st1.print();
Student st2("Smith");
st2.print();
return 0;
}
instance
가 메모리에서 해제될 때 내부에서 자동으로 호출된다.delete
으로 매모리를 해제할 때만 소멸자가 호출된다.class Simple
{
private:
int m_id;
public:
Simple(const int& id_in)
: m_id(id_in)
{
cout << "Constructor " << m_id << endl;
}
// 소멸자
~Simple()
{
cout << "Destructor " << m_id << endl;
}
};
int main()
{
Simple *s1 = new Simple(0); // 동적 할당
Simple s2(1);
delete s1;
return 0;
}
this
라는 숨겨진 포인터를 사용한다.this 포인터
는 멤버 함수가 호출된 객체의 주소를 가리키는 숨겨진 포인터다.class Something
{
private:
int data;
public:
Something(int data)
{
this->data = data;
// this->data 는 멤버 변수이고
// data 는 매개 변수
}
}
- 클래스 멤버 함수가 작업 중이던 객체(인스턴스)를 반환하는 방식이 유용할 때 -> 체이닝 기법
class Calc
{
private:
int m_value;
public:
Calc(int init_value)
: m_value(init_value)
{}
Calc& add(int value) { m_value += value; return *this; }
Calc& sub(int value) { m_value -= value; return *this; }
Calc& mult(int value) { m_value *= value; return *this; }
void print()
{
cout << m_value <<endl;
}
};
int main()
{
Calc cal(10);
cal.add(10).sub(1).mult(2).print();
// cal.add(10); cal.sub(1); cal.mult(2);
// cal.print();
return 0;
}
#include "Calc.h"
int main()
{
Calc cal(10);
cal.add(10).sub(1).mult(2).print();
// cal.add(10); cal.sub(1); cal.mult(2);
// cal.print();
return 0;
}
#pragma once
#include <iostream>
class Calc
{
private:
int m_value;
public:
Calc(int init_value);
Calc& add(int value);
Calc& sub(int value);
Calc& mult(int value);
void print() ;
};
#include "Calc.h"
Calc::Calc(int init_value)
: m_value(init_value)
{}
Calc& Calc::add(int value)
{
m_value += value;
return *this;
}
Calc& Calc::sub(int value)
{
m_value -= value;
return *this;
}
Calc& Calc::mult(int value)
{
m_value *= value;
return *this;
}
void Calc::print()
{
std::cout << m_value << std::endl;
}
class B; // forward declaration
class A
{
private:
int m_value = 1;
friend void doSomething(A& a, B& b);
};
class B
{
private:
int m_value = 2;
friend void doSomething(A& a, B& b);
};
void doSomething(A& a, B& b)
{
// private 변수를 직접 접근
cout << a.m_value << " " << b.m_value <<endl;
}
int main() {
A a;
B b;
doSomething(a, b);
return 0;
}
외부에 있는 함수 말고, 다른 클래스가 내 private 에 접근가능하게 해주는 방법
class B; // forward declaration
class A
{
private:
int m_value = 1;
friend class B;
};
class B
{
private:
int m_value = 2;
public:
void doSomething(A& a)
{
cout << a.m_value << endl;
}
};
int main() {
A a;
B b;
b.doSomething(a);
return 0;
}
클래스 전부 open 하긴 부담스러우니 함수만 주고 싶다
class A; // forward declaration
class B
{
private:
int m_value = 2;
public:
void doSomething(A& a);
};
class A
{
private:
int m_value = 1;
//friend class B;
friend void B::doSomething(A& a);
};
void B::doSomething(A& a)
{
cout << a.m_value << endl;
}
int main() {
A a;
B b;
b.doSomething(a);
return 0;
}
//A a();
//a.print();
A().print();
class Cents
{
private:
int m_cents;
public:
Cents(const int& cents)
: m_cents(cents)
{}
int getCents() const
{
return m_cents;
}
};
Cents add(const Cents& c1, const Cents &c2)
{
return Cents( c1.getCents() + c2.getCents() );
}
int main()
{
cout << add(Cents(6), Cents(8)).getCents() << endl;
return 0;
}
class Fruit
{
public:
enum FruitType
{
APPLE, BANANA, CHERRY,
};
private:
FruitType m_type;
public:
Fruit(FruitType type) : m_type(type)
{}
FruitType getType() { return m_type; }
};
int main()
{
Fruit apple(Fruit::APPLE);
if(apple.getType() == Fruit::APPLE)
std::cout << "Apple" << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
using namespace std;
class Timer
{
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1>>;
std::chrono::time_point<clock_t> start_time = clock_t::now();
public:
void elapsed()
{
std ::chrono::time_point<clock_t> end_time = clock_t::now();
cout << std::chrono::duration_cast<second_t>(end_time - start_time).count() << endl;
}
};
int main()
{
random_device rnd_device;
mt19937 mersenne_engine{ rnd_device() };
vector<int> vec(10);
for (unsigned int i = 0; i <vec.size(); ++i)
vec[i] = i;
// vector 안에 숫자 섞기
std::shuffle(begin(vec), end(vec), mersenne_engine);
Timer timer;
// 정렬
std::sort(begin(vec), end(vec));
// 잰 시간 출력
timer.elapsed();
for (auto &e : vec) cout << e << " ";
cout << endl;
return 0;
}