C++/Class
2021. 3. 19. 19:40
의존 관계 (Dependency)
Worker
클래스에서 잠깐 사용하는Timer
클래스main.cpp
#include "Timer.h" #include "Worker.h" int main() { Worker().doSomething(); } /* stdout 4e-07 */
Timer.h
#pragma once #include <iostream> #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; } };
Worker.h
#pragma once class Worker { public: void doSomething(); };
Worker.cpp
#include "Worker.h" #include "Timer.h" void Worker::doSomething() { Timer timer; // do some work here timer.elapsed(); }
'C++ > Class' 카테고리의 다른 글
C++ 상속 기본 예제 (0) | 2021.03.19 |
---|---|
C++ 상속 (Inheritance) (0) | 2021.03.19 |
C++ 연계, 제휴 관계 (Association) (0) | 2021.03.19 |
C++ 구성 관계 (Composition Relationship) (0) | 2021.03.19 |
C++ 객체들의 관계 (Object Relationship) (0) | 2021.03.19 |