객체지향의 핵심
is-a relationship
inheritance

자식클래스는 상속받은 부모클래스의 기능을 모두 사용할 수 있다.
상속을 받은 클래스(자식클래스)를 derived class라고도 한다.

여러클래스에 일반화된것들을 뽑아서 부모클래스로 생성한다.
즉, 여러클래스에서 재사용이 매우용이하다.

부모클래스,자식클래스에 이름이 같은 메소드가 있다면 자식클래스의 메소드가 실행된다.
private는 자식에게도 허용이안된다. 이럴때 protected를 사용거나 부모클래스 생성자를 사용한다.

자식클래스의 생성이유는 부모클래스의 기능과 자식클래스의 기능을 합쳐쓰기 위함이다.

자식클래스에서 생성자Constructor를 만들려면 부모클래스를 불러와 정의해준다.

당연히 자식클래스에서 부모클래스를 사용할수 있기 때문에

부모클래스가 먼저생성되고 자식클래스가 생성된다. 

즉, 자식클래스가 생성될때 부모클래스의 생성자를 같이 실행한다.

즉, 기본생성자가 없으면 정의 해주어야한다.
아니면 자식클래스 생성자에서 부모클래스생성자를 선언해준다.

 

#include <iostream>

using namespace std;

class Mother {
private:
	int m_i;

public:
	//Mother(){}

	Mother(const int & i_in) 
	: m_i(i_in) {
		cout << "constructor" << endl;
	}

	void setValue(const int & i) {
		m_i = i;
	}

	int getValue() {
		return m_i;
	}
};

class Child : public Mother
{
private:
	double m_d;

public:
	Child(const double & d_in, const int & i_in) 
	: Mother(i_in),m_d(d_in)
	{}

	void setValue(const double & d_in,const int & i_in) {
		Mother::setValue(i_in);
	}

	void setValue(const double & d) {
		m_d = d;
	}

	double getValue() {
		return m_d;
	}
};

//Mother 클래스 재사용
class Daughter : public Mother
{

};

int main()
{
	Mother mother(11);
	mother.setValue(1234);
	cout << mother.getValue() << endl;

	Child child(12,222);
	cout << child.getValue() << endl;
	cout << child.Mother::getValue() << endl;

	return 0;
}

컨테이너클래스 Container Classes

다른클래스를 담는 역할을하는 클래스를 컨테이너 클래스라고한다.

vertor,array등이 있다.

IntArr 클래스를 한번 만들어보자.

 

#include <iostream>
#include <vector>
#include <array>
#include <cassert>
#include <initializer_list>

using namespace std;

class IntArr
{
private:
	int m_length = 0;
	int *m_data = nullptr;

	int beforeLen = 0;
	int *beforeData = nullptr;

public:
	void ixCheck(const int & ix) {
		assert(ix <= m_length);
		assert(ix >= 0);
	}

	//Constructor
	IntArr() {}

	IntArr(const int & len) 
	{
		assert(len >= 0);
		m_length = len;
		m_data = new int[m_length];
	}

	IntArr(const initializer_list<int> & list) 
	{
		m_length = list.size();
		m_data = new int[m_length];
		int a = 0;
		for (auto & ele : list) {
			m_data[a] = ele;
			a++;
		}
	}
	//Destrurctor
	~IntArr() {
		delete[] this -> m_data;
	}

	//initialize()
	IntArr & operator = (const initializer_list<int> & list) {
		delete[] m_data;
		m_length = list.size();
		m_data = new int[m_length];
		
		if (m_data != nullptr) {
			int a = 0;
			for (auto & ele : list) {
				m_data[a] = ele;
				a++;
			}
		}
		else {
			m_data = nullptr;
		}
		return *this;
	}

	int getSize() {
		return m_length;
	}

	

	void copyArr() {
		beforeLen = m_length;
		beforeData = new int[beforeLen];
		for (int i = 0; i < beforeLen; i++) {
			beforeData[i] = m_data[i];
		}
	}
	//reset()
	void reset() {
		m_length = 0;
		m_data = nullptr;
	}

	//resize()
	void resize(const int & ix) {
		this -> ixCheck(ix);
		this -> copyArr();

		m_length = ix;
		m_data = new int[m_length];

		for (int i = 0; i < m_length; i++) {
			m_data[i] = beforeData[i];
		}

		delete[] beforeData;
	}
	//insertBefore(const int & value, const int & ix);
	void insertBefore(const int & value, const int & ix) {
		this->ixCheck(ix);
		this->m_data[ix] = value;
	}
	//remover(const int & ix);
	void remover(const int & ix) {
		this->ixCheck(ix);
		this->copyArr();

		m_length = beforeLen - 1;
		m_data = new int[m_length];

		int count = 0;
		for (int i = 0; i < beforeLen; i++) {
			if (i != ix) {
				m_data[count] = beforeData[i];
				count++;
			}
		}

		delete[] beforeData;

	}

	IntArr & push_back(const int &val) {
		this->copyArr();

		m_length = m_length + 1;
		m_data = new int[m_length];

		for (int i = 0; i < m_length; i++) {
	
			if (m_length - 1 == i) {
				m_data[i] = val;
			}
			else {
				m_data[i] = beforeData[i];
			}
			
		}
		delete[] beforeData;

		return *this;
	};

	friend ostream & operator << (ostream & out, IntArr &iA) {
		for (int i = 0; i < iA.m_length; i++) {
			
			out << iA.m_data[i] << " ";
		}
		return out;
	}
};

int main()
{
	IntArr arr{ 1,2,3,4 };
	cout << arr << endl;
	cout << arr.getSize() << endl;

	arr = { 1,2,3 };
	cout << arr << endl;
	cout << arr.getSize() << endl;

	arr.resize(2);
	cout << arr << endl;
	cout << arr.getSize() << endl;

	arr.remover(2);
	cout << arr << endl;
	cout << arr.getSize() << endl;
	arr.insertBefore(100, 0);
	cout << arr << endl;
	cout << arr.getSize() << endl;

	arr.push_back(1).push_back(3);
	cout << arr << endl;
	cout << arr.getSize() << endl;

	return 0;
}

의존관계 Dependencies

서로간의 연결 강도가 낮다.

 

ex:자바 스프링에서 여러기능을 추가할때 사용한다.

재사용이 가능한 클래스를 어느 클래스에서 사용할때를 말한다.

다른 클래스를 사용하더라도 그 클래스의 자세한 내용은 몰라도된다.
즉,잠시 빌려 사용한다고 생각하면된다.

main.cpp

#include "Worker.h"

using namespace std;

int main()
{
	Worker w;
	w.doSomething();

	return 0;
}

Timer.h

#pragma once
#include <chrono>
#include <iostream>

using namespace std;

class Timer {

	using clock_t = chrono::high_resolution_clock;
	using second_t = chrono::duration<double, ratio<1>>;

	chrono::time_point<clock_t> start_time = clock_t::now();

public:
	void elapsed() {
		chrono::time_point<clock_t> end_time = clock_t::now();

		cout << chrono::duration_cast<second_t>(end_time - start_time).count() << endl;
	}
};

Worker.h

#pragma once

class Worker {
public:
	//doSomething에서 Timer클래스를 사용한다.
	//하지만 여기서 사용하는지 알 수 없다.
	void doSomething();
};;

Worker.cpp

#pragma once
#include "Worker.h"
#include "Timer.h"


void Worker::doSomething() {
	//잠시 Timer Class의 기능을 사용한다.
	Timer timer;
	timer.elapsed();
}

제휴관계 Association

어느 한쪽이 주 반대쪽이 부가 되는 개념이 아닌 개념이다.
어느쪽이든 주,부가 될수 있다.

다른관계들보단 많이 사용하지 않는다.

서로가 서로를 사용한다. 용도이외에는 무관하다.

의사,환자

의사1이 환자1을 만난다.
환자1이 의사1를 만난다.
의사1이 환자2를 만난다.
환자2가 의사1을 만난다.

즉, 서로 기록은 하지만 터치하지 못한다.
또 양방향으로 기록하는게 아닌 단방향으로 기록할 수 있다.

 

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Doctor;

class Patient {
private:
	string m_name;
	vector<Doctor*> m_doc;

public:
	Patient(string name)
		:m_name(name) {

	}

	void addDoc(Doctor *new_doc) 
	{
		m_doc.push_back(new_doc);
	}

	void meetDot();
	friend class Doctor;
};

class Doctor {
private:
	string m_name;
	vector<Patient*> m_pat;
	
public:
	Doctor(string name)
		:m_name(name) {

	}

	void addPai(Patient *new_pai)
	{
		m_pat.push_back(new_pai);
	}

	void meetPai() {
		for (auto & ele : m_pat) {
			cout << m_name << " - >Meet Pat : " << ele->m_name << endl;
		}
	}
	friend class Patient;
};
void Patient::meetDot() {
	for (auto & ele : m_doc) {
		cout << m_name << " - >Meet Doc : " << ele->m_name << endl;
	}
}
int main()
{
	Patient *p1 = new Patient("choi");
	Patient *p2 = new Patient("lee");
	Patient *p3 = new Patient("kim");

	Doctor *d1 = new Doctor("D_kim");
	Doctor *d2 = new Doctor("D_jung");

	p1->addDoc(d1);
	d1->addPai(p1);
	p2->addDoc(d1);
	d1->addPai(p2);
	p1->meetDot();
	p2->meetDot();
	d1->meetPai();
	delete p1;
	delete p2;
	delete p3;

	delete d1;
	delete d2;
	return 0;
}

집합aggregation
Has-a 어떤 사람이 자동차를 가지고 있다.
사람이 없어도 자동차는 있을 수 있다(느슨한관계)


학교에는 수업,교수,학생이 있다.

한교수가 여러개의 강의를 할 수 있고
한학생이 여러 수업을 들을 수 있다.

구성관계로 개발하면 정보가 공유되지 않는다.
또, 강의 객체가 없어지면 모든 정보가 사라진다.
객체1 : 학교{강의{교수,학생}
객체2 : 학교{강의2{교수,학생}

집합관계로 개발하게되면 
강의,교수,학생을 따로 생성하고 주소를 공유한다.
강의1,강의2,교수1,교수2,학생1,학생2,학생3을 생성한다.
강의1 안에 교수1,학생1,학생2의 메모리 주소를 공유한다.
강의2 안에 교수2,학생1,학생2,학생3의 메모리 주소를 공유한다.

이렇게하면 강의1이 없어지더라도 교수1,학생1,학생2의 정보는 남는다.


다만, 분산처리할땐 메모리가 분리되어있어 사용못한다.

 

#include <iostream>

//강의
#include "Lecture.h"
//교수
#include "Teacher.h"
//학생
#include "Student.h"

int main()
{
	using namespace std;
	//다른곳에서 쓰게되면 동적할당으로 사용함.
	Student std1("Jack", 0);
	Student std2("cho", 0);
	Student std3("ahn", 0);

	Teacher t1("t_lee");
	Teacher t2("t_choi");

	Lecture lec1("test1");
	lec1.assignTeacher(&t1);
	lec1.registerStudent(&std1);
	lec1.registerStudent(&std3);

	Lecture lec2("test2");
	lec2.assignTeacher(&t2);
	lec2.registerStudent(&std1);
	lec2.registerStudent(&std2);
	lec2.registerStudent(&std3);

	{
		cout << lec1 << endl;
		cout << lec2 << endl;

		lec1.study();
		cout << lec1 << endl;
		cout << lec2 << endl;


	}
}

구성(요소)Composition 
Part-of 두뇌는 육체의 일부이다.
육체가 없으면 두뇌는 없다

몬스터 클래스에 위치 클래스가 있다고한다면
몬스터가 없어지면 그몬스터의 위치클래스도 없어져야한다.
또, 위치 클래스는 몬스터의 이름을 알지 못한다.

즉, 몬스터 클래스에서 사용된 위치클래스는 그냥 몬스터의 일부이다.

또한 몬스터클래스가 위치클래스의 동작원리를 알 필요가 없다.

Composition 관계는 전체/일부로 생각하면 된다.

몬스터클래스 {(몬스터전체)
위치 클래스 : 몬스터클래스의 정보안에 위치가 존재한다.(몬스터의 일부)
}

 

main.cpp

#include <iostream>
#include "Monster.h"
using namespace std;



int main() {
	//몬스터 정보안에 위치가 들어있다.
	Monster mon1("name", Position2D(0,0));
	mon1.getLocation();
	cout << mon1 << endl;
	Monster mon2("name2", Position2D(0, 0));
	mon2.getLocation();
	{
		mon1.moveTo(Position2D(1, 1));
		cout << mon1 << endl;
	}
	return 0;
}

Monster.h

#pragma once

#include <string>
#include "Position2D.h"

class Monster
{
private:
	std::string m_name;

	//subClass
	Position2D m_location;//m_x,m_y

	//int m_x;//location
	//int m_y;

public:
	//서브클래스로 위치클래스를 사용한다.
	Monster(const std::string name_in, const Position2D & pos_in)
		:m_name(name_in), m_location(pos_in) {

	}
	//set의 원리를 알 수 없다.
	void moveTo(const Position2D & pos_target) {
		m_location.set(pos_target);
	}

	void getLocation () {
		std::cout << m_location << std::endl;
	}


	friend std::ostream & operator << (std::ostream & out, Monster & monster) {
		out << monster.m_name << " " << monster.m_location;
		return out; 
	}
};

Position2D.h

#pragma once
#include <iostream>

class Position2D
{
private:
	int m_x;
	int m_y;

public:
	Position2D(const int & x_in,const int & y_in)
		: m_x(x_in),m_y(y_in)
	{}

	void set(const Position2D & pos_target) {
		set(pos_target.m_x, pos_target.m_y);
	}

	void set(const int & x_in, const int & y_in) {
		m_x = x_in;
		m_y = y_in;
	}

	friend std::ostream & operator << (std::ostream & out, Position2D & position2D) {
		out << position2D.m_x << " " << position2D.m_y;
		return out;
	}
};

이니셜라이져 리스트
사용자정의자료형에서 생성자,대입연산자를 만들때 편하게 사용하는것

array등에 사용하는 {}를 말한다.

initalizer_list를 사용한다.
파라미터 initalizer_list로 받아 변환해준다고 생각하면된다

#include <iostream>
#include <cassert>
#include <initializer_list>

using namespace std;

class IntArray {
private:
	unsigned m_length = 0;
	int *m_data = nullptr;

public:
	IntArray(unsigned length)
		:m_length(length) {
		m_data = new int[m_length];
	}

	IntArray(const std::initializer_list<int> & list) 
	:IntArray(list.size()){
		cout << "initializer_list" << endl;
		int count = 0;
		for (auto & element : list) {
			m_data[count] = element;
			++count;
		}
	}

	IntArray& operator =(const std::initializer_list<int> & list)
	{
		delete[] m_data;

		m_length = list.size();
		m_data = new int[m_length];
		cout << "= initializer_list" << endl;

		if (m_data != nullptr) {
			int count = 0;
			for (auto & element : list) {
				m_data[count] = element;
				++count;
			}
		}
		else {
			m_data = nullptr;
		}

		return *this;
	}

	~IntArray() {
		delete[] this->m_data;
	}

	friend ostream & operator << (ostream &out, IntArray &iA) {
		for (unsigned i = 0; i < iA.m_length; i++) {
			out << iA.m_data[i] << " ";
		}
		out << endl;
		return out;
	}
};

int main()
{
	auto il = { 1,2,3 };

	IntArray iA{ 12,3,3 };
	cout << &iA << endl;
	cout << iA << endl;
	iA = { 1,2,3,4,5,6 };
	cout << &iA << endl;
	cout << iA << endl;
	return 0;
}

동적할당된 메모리에 대한 포인터변수를 멤버로 가지고 있는 클래스는
복사하거나 대입할때 깊은복사,얕은복사에 따라 복사생성자,대입연산자구현이 까다로워진다.

기본 복사생성자는 주소값을 복사해준다.

동적메모리할당을 할때는 주의해야한다.

주소값만 복사해주는것을 얕은 복사라고한다.
얕은 복사는 주소값만 복사해주기에 동적할당된 메모리를 반납하게되면 이슈가 생길 수 있다.

주소값을 복사하는대신 메모리를 다시할당 받는것은 깊은 복사라고한다.
다시 주소를 할당받기에 문제가 없다.

변수가 만들어지는순간엔 복사생성자가 실행된다.
이미 있는 변수에 대입하는건 대입 연산자가 실행된다.

 

논리를 잘이해하자 자바등 어느 언어든지 동적메모리할당,반납은 있다.

#include <cassert>
#include <iostream>

using namespace std;

class MyString {
public:
	char *m_data = nullptr;
	int m_length = 0;

public:
	MyString(const char *source = "") {
		assert(source);
		m_length = std::strlen(source)+1;
		m_data = new char[m_length];

		for (int i = 0; i < m_length; i++) {
			m_data[i] = source[i];
		}
		m_data[m_length - 1] = '\0';
	}
	
	MyString(const MyString &source) {
		cout << "Copy constructor" << endl;
		m_length = source.m_length;

		if (source.m_data != nullptr) {
			//주소 새로할당
			m_data = new char[m_length];
			for (int i = 0; i < m_length; i++) {
				m_data[i] = source.m_data[i];
			}
		}
		else {
			m_data = nullptr;
		}
	}

	MyString& operator = (const MyString &source) {

		//shallow copy;
		//this->m_length = source.m_length;
		//this->m_data = source.m_data;
		
		cout << "Assignment operator" << endl;

		//자기자신을 대입했을때
		if (this == &source)
			return *this;
	
		//이미 메모리를 가지고 있을 수 있다.
		delete[] m_data;

		m_length = source.m_length;

		if (source.m_data != nullptr) {
			//주소 새로할당
			m_data = new char[m_length];
			for (int i = 0; i < m_length; i++) {
				m_data[i] = source.m_data[i];
			}
		}
		else {
			m_data = nullptr;
		}

	}

	~MyString() {
		delete[] m_data;
	}

	char* getString() { return m_data; }
	int getLength() { return m_length; }
};

int main()
{

	MyString hello("Hello");

	cout << (int*)hello.m_data << endl;
	cout << hello.getString() << endl;

	{
		//복사생성자 주소값을 복사해준다.
		MyString copy;
		copy = hello;
		cout << (int*)copy.m_data << endl;
		cout << copy.getString() << endl;
		//scope를 나갈때 메모리를 반납한다.
	}

	cout << hello.getString() << endl;
	return 0;
}

프로그래머 편의를위해 생성자를 변환시켜주는 Converting construtor

동적할당 메모리할당 해제하는 delete랑은 다르다.

explicit,delete는 엄격하게 제한할때 사용한다.

생성자의 기본값을 정해주면 파라미터를 모두 넣어주지않아도 된다.
또, int로 파라미터를 설정해놓아도 char로 받을 수 있다.

위 두개의 사항을 엄격하게 제어하기위해
explicit와 delete를 사용한다.

 

#include <iostream>
#include <cassert>

using namespace std;
class Fraction {
private:
	int m_num;
	int m_den;

public:
	Fraction(char) = delete;
	Fraction(char,char) = delete;

	explicit Fraction(int num = 0, int den = 1)
		:m_num(num), m_den(den)
	{
		assert(den != 0);
	}

	Fraction(const Fraction &frc)
		:m_num(frc.m_num), m_den(frc.m_den) {
		cout << "copy constructor called" << endl;
	}

	friend std::ostream & operator << (std::ostream & out, const Fraction & f) {
		out << f.m_num << " / " << f.m_den << endl;
		return out;
	}
};

void doSomething(Fraction frac) {
	cout << frac << endl;
}

int main()
{
	//Fraction('a','a');
	doSomething(Fraction(2,3));
	
	return 0;
}

복사생성자 복사초기화 RVO

자신과 똑같은 타입의 데이터가 들어오면 복사하는 것을 말한다. 


컴파일러의 판단
복사할려는 value가 r_value면 복사하지않고 바로 생성한다.
함수로 return받으면 debug 일때면 복사생성자를 사용하고(다른주소를 가진다) release에선 바로 생성(주소값이 같다)한다.

 

복사생성자 막기

copy constructor를 private로 보내면 복사생성자를 사용할 수 없다. 

#include <iostream>
#include <cassert>

using namespace std;
class Fraction {
private:
	int m_num;
	int m_den;

public:
	Fraction(int num, int den) 
		:m_num(num), m_den(den) 
	{
		assert(den != 0);
	}

	Fraction(const Fraction &frc)
		:m_num(frc.m_num), m_den(frc.m_den) {
		cout << "copy constructor called" << endl;
	}

	friend std::ostream & operator << (std::ostream & out, const Fraction & f) {
		out << f.m_num << " / " << f.m_den << endl;
		return out;
	}
};

Fraction doSomething()
{
	Fraction temp(1, 2);
	return temp;
}

int main()
{
	Fraction frac(3, 5);

	Fraction cpFrac(frac);

	cout << cpFrac << endl;
	return 0;
}

+ Recent posts