의존관계 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;
}

객체 Objects
- 객체지향 프로그래밍란 말에서 사용되는 객체(Object)
- c++,java에서 사용
- 물리적으로 공간을 차지하고 있는 물체를 존재한다고 본다.
- 컴퓨터안에선 메모리에 저장이 돼있는 정보가 객체로서 존재한다고 본다.

변수 variables
- 객체를 다루고 싶을때 즉, 메모리에 올려놓고 사용하고 싶을 때 사용한다.
- ex)아파트 104동 1014호 1041014로 저장하면 보기 힘드므로 OO 네 집처럼 이름 저장한다.

Left-values와 Right-values

- Left-values는 알기 쉬운 주소 값을 가지고 있다.

Right-values는 주소값을 알기 어렵다.


초기화 initializaion와 대입 assignment

- int a = 123; int a(123);등이 initializaion초기화이다.

- 초기화는 메모리에 적재될 때 같이 집어 값을 넣어준다.

- int b; 

- b = 123;은 assignment 대입의 개념 생성된 메모리 안에 값을 대입해준다.


초기화를 안 했을 때의 문제점

- 메모리 주소 안에 garbage값이 들어 있을 수 있다.

 

#include <iostream>

int main()
{
	//x라는 정수형(interger)변수를 선언했다.
	//x도 객체이다. x는 숫자를 담을수있는공간을 메모리에 차지하고 있다.
	//x 메모리의 이름이다.
	int x;
	//x = 123 <- assignment 대입해준다.
	//프로그래밍에서 = 는 보통 오른쪽값을 왼쪽에 대입해준다.
	//즉, x란 변수가 가르키고있는 메모리 공간에 123을 대입해준다.
	x = 123;

	//x의 주소의 대입되어있는 값 출력
	std::cout << x << std::endl;
	//x의 주소 출력(메모리주소)
	std::cout << &x << std::endl;

	//Left-values int x 는 메모리의 주소를 가지고있다.
	//Right-values 123는 프로그래머가 임시로 저장된 주소를 가져오기가 어렵다.
	int a = 123;
	//왼쪽 a는 Left-values이나 오른쪽 a는 a의 값을 임시로 복사해온 right-values로 본다.
	a = a + 123;

	//int z = 123;
	//z라는 변수가 메모리에 할당받을때 바로 집어넣어준다.
	//메모리에 garbage 값이 있을수 있기에 초기화 해준다.
	//initialization
	int z = 123;
	
	//x = 123 <- assignment 대입해준다.
	z = 123;

	//initialization와 assignment같아보이지만 다르다.
	//ex)int z(123);으로 사용할 수 있다.
	//initialization의 개념은 생성자,소멸자,오버로딩,오버라이딩에 사용된다.
	
	return 0;
}

'개발 소발 > 개발 C++(기초)' 카테고리의 다른 글

c++ 함수란?  (0) 2019.06.25
c++ 입력,출력  (0) 2019.06.25
C언어 파일입출력  (0) 2019.01.18
C언어 코딩 함수포인터,구조체  (0) 2019.01.08
C언어 코딩 동적메모리 할당  (0) 2019.01.07

+ Recent posts