C++ 28

[C++/Inheritance] Accessing Inherited Functions

문제 Accssing Inherited Functions | HackerRank Accessing Inherited Functions | HackerRank Access inherited functions with the same name. www.hackerrank.com A, B, C 클래스의 함수들을 이용하여 입력된 값을 만들 때, 클래스들이 각각 몇 번 호출되었는지 출력하는 문제입니다. 단, 입력된 값은 오직 2, 3, 5를 소인수로 갖습니다. 풀이 A, B, C 클래스는 각각 전달받은 인수에 2, 3, 5 를 곱하고, 클래스가 호출된 횟수의 값을 저장하고 반환하는 동작을 합니다. void D::check(int new_val) { update_val(new_val); cout

[C++/Debugging] Messages Order

문제 Messages Order | HackerRank Messages Order | HackerRank Implement a software layer over the top of a network, such that sent messages are printed by the recipient in the order they were sent. www.hackerrank.com 메세지를 생성하고, 순서가 보존되지 않는 네트워크에서 송신자가 메세지를 보낸 순서대로 수신자에게 출력하도록 Message 클래스와 MessageFactory 클래스의 구현을 마무리하는 문제입니다. 풀이 class Network { public: static void send_messages(vector messages, Reci..

[C++/STL] Deque-STL

문제 Deque-STL | HackerRank Deque-STL | HackerRank Learn to use deque container. Find the maximum number in each and every contiguous sub array of size K in the given array. www.hackerrank.com 배열을 보다 작은 크기의 부분 배열로 나누고, 각 부분 배열에서의 최댓값을 찾아 출력하는 문제입니다. 코드 설명 모든 부분 배열마다 방문하여 최댓값을 찾는 것은 비효율적이기 때문에 방문을 최소화해야 합니다. 핵심은 부분 배열의 원소들이 어떻게 바뀌는지 파악해야 합니다. 그림을 통해 쉽게 이해해보도록 하겠습니다. 위 그림과 같이 배열이 주어졌고, 부분 배열의 크기는 4 ..

[C++/Class] Abstract Classes - Polymorphism

문제 Abstract Classes - Polymorphism | HackerRank Abstract Classes - Polymorphism | HackerRank Given an abstract class Cache, write a class LRUCache which extends the class Cache and implement an LRU cache. www.hackerrank.com 페이지 교체 알고리즘 (Least Recently Used Algorithm, LRU)을 구현하는 문제입니다. 풀이 처음에 연결 리스트(Linked List)를 사용하여 캐시 저장소를 관리하려고 해서 코드가 복잡해지고, 구현도 힘들었습니다. 문제를 다시 읽고 주어진 코드를 살펴보다가 Cache 클래스의 Map ..

[C++/Classes] Virtual Functions

문제 Virtual Functions | HackerRank Virtual Functions | HackerRank Learn how to use virtual functions and solve the given problem. www.hackerrank.com 기초 클래스인 Person 클래스와 파생 클래스인 Professor 클래스와 Student 클래스를 조건에 맞게 구현하는 문제입니다. 또한, 기초 클래스의 멤버 함수를 가상 함수로 구현하고, 파생 클래스에서 오버로드(Overload)를 거쳐 객체의 타입에 따라 다르게 동작하도록 구현하는 문제입니다. 풀이 int main(){ int n, val; cin>>n; //The number of objects that is going to be cre..

[C++/Classes] Exceptional Server

문제 Link: Exeptional Server | HackerRank Exceptional Server | HackerRank Handle server errors. www.hackerrank.com 주어진 Server 클래스의 멤버 함수인 compute 의 동작에서 발생하는 예외를 처리하는 문제입니다. 풀이 ... // When you use multiple catch blocks, have to consider order. // In this problem, 'exception' is parent exception object of 'bad_alloc'. // If exception be first, catch block for bac_alloc do not run. try { cout

[C++/Classes] Inherited Code

문제 Inherited Code | HackerRank Inherited Code | HackerRank Inherited Code | HackerRank We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies. www.hackerrank.com exception 클래스를 상속받는 BadLengthException 클래스를 구현하여 사용자명을 입력받고 조건에 부합할 때 발생하는 예외를 처리하는 문제입니다. 풀이 class BadLengthException : public exceptio..

[C++/Classes] Box it!

문제 Box It! | HackerRank Box It! | HackerRank Design a class named Box with overloaded operators. www.hackerrank.com 육면체의 길이, 너비, 높이 값을 관리하고, 부피를 구하는 Box 클래스를 구현하는 문제입니다. 또한, 연산자 오버로드를 통해 클래스에 맞는 연산 결과를 출력하는 문제입니다. 풀이 조건에 맞는 클래스(Class)를 구현하고, 오버로드(Overloading)를 활용할 수 있는지에 관해 물어보는 문제입니다. Box() { l = 0; b = 0; h = 0; } Box(int length, int breadth, int height) { l = length; b = breadth; h = height; ..

[C++] export와 클래스 템플릿, 헤더 파일 나누기

export와 클래스 템플릿템플릿들은 클래스와 멤버 함수 정의가 아니다 클래스와 멤버 함수 정의를 생성하는 방법을 컴파일러에게 알려 주는 지시문이다 템플릿들은 함수가 아니기 때문에 개별적으로 컴파일할 수 없다 따라서 특별한 구체화 요구들과 함께 결함하여 사용해야 한다 보통 모든 템플릿 관련 정보를 헤더 파일에 집어 넣고, 그 템플릿들을 사용하는 파일에 그 헤터 파일을 포함시키는 것이다 하지만 위와 같은 방법은 코드가 깔끔해보이지 않을 수 있다 템플릿이 아닌 클래스의 경우 cpp 파일을 따로 나누어 함수 정의를 구현한다 템플릿 클래스의 경우는 export 키워드를 이용하여 그것이 가능하다 ... export template class nameclass { ... }; ... 주의할 점은 export 키워드..

[C/C++] char[] 문자열 초기화시 주의할 점

문자열을 동적 메모리를 할당하여 초기화 할 때 주의해야할 점이 있다 char str = new char[1]; delete[] str; //new[]를 사용하여 초기화 시킨 //포인터 및 널 포인터와 호환 char words[15] = L"bad idea"; char *p1 = words; char *p2 = new char; char *p3; delete []p1; //결과를 알 수 없다, 사용하지 말자 delete []p2; //결과를 알 수 없다, 사용하지 말자 delete []p3; //결과를 알 수 없다, 사용하지 말자 new []를 사용하여 초기화한 포인터나 널 포인터가 delete []와 호환되기 때문에 주의할 필요가 있다 p1, p2, p3와 같은 경우에는 delete []와 호환되지 않기..

728x90