배움 기록/Programming

C++ 인터뷰 준비하기 - 2. OOP

Spezi 2024. 12. 25. 01:53
반응형

1편 basics에 이어서 OOP에 대한 인터뷰 예상질문이다. ☺️

2024.12.25 - [배움 기록/Programming] - C++ 인터뷰 준비하기 - 1. Basic Concepts

 

C++ 인터뷰 준비하기 - 1. Basic Concepts

이번에 C++ 인터뷰를 준비하면서 예상 질문 리스트를 만들어봤다. 이걸 블로그에 공유해볼까 한다. 영어 인터뷰였기 때문에 질문과 답변도 영어로 작성되어 있다. 내가 본 인터뷰

jedemanfangwohnteinzauberinne.tistory.com

 

2. Object-Oriented Programming (OOP)

What is object-oriented programming? How is it implemented in C++?

Object-Oriented Programming in C++ revolves around creating and managing classes and objects. It uses principles like encapsulation, inheritance, polymorphism, and abstraction to create flexible and reusable code. C++ provides the tools needed to implement these principles effectively, allowing for robust and maintainable software design.


What are the four pillars of OOP?

  1. Encapsulation: Bundles data and methods, restricting access to the internal state.
  2. Inheritance: Allows for code reuse and establishes a hierarchy between classes.
  3. Polymorphism: Enables objects to be treated as instances of their base class, allowing for different behaviors based on the object’s type.
  4. Abstraction: Defines a general interface for classes, focusing on what an object does rather than how it does it.

What is the difference between public, private, and protected inheritance?

  • Private:
    • (Default) Only objects of the same class can access these members. For example, point0._x in main() would not work because _x is private.
    • Other objects of the same class can still access private members of other objects of the same class.
  • Protected:
    • Only objects of the same class or derived classes can access these members.
  • Public:
    • Anyone can access these members. For instance, point0.x() is always accessible.

Why shouldn’t we make all member variables public?

  • Only expose what is meant to be modified.
  • Ensure that the object is modified consistently.
  • Hide the implementation details to simplify usage for the end-user.

What is friend in C++?

The friend keyword in C++ allows a function or another class to access the private and protected members of the class in which it is declared. Although friend functions are not members of the class, they can still access its private and protected data.


Explain polymorphism and how it is achieved in C++.

  • Compile-Time Polymorphism: Achieved through method and operator overloading. The correct function is chosen based on the function signature at compile time.
  • Runtime Polymorphism: Achieved through inheritance and virtual functions. The correct function is determined at runtime based on the object's actual type.

What is a virtual function, and why is it used?

  • Virtual Function: A member function in a base class that can be overridden in derived classes.
  • Purpose: Enables runtime polymorphism, allowing methods to be dynamically dispatched based on the object's actual type.
  • Usage: Defines a common interface in a base class while allowing specific implementations in derived classes. Ensures proper resource cleanup with virtual destructors when dealing with inheritance.

What is the role of the this pointer in C++?

  • Pointer to the Object: this is a pointer to the object for which the member function is called.
  • Usage:
    • Provides context for member functions.
    • Helps distinguish member variables from parameters when there is a naming conflict.
    • Supports method chaining.

Explain constructor overloading and operator overloading.

Overloading refers to defining multiple functions or operators with the same name but different parameters (in number, type, or both).

  • Constructor Overloading: Allows you to define multiple constructors for a class, each with a different parameter list. This enables you to create objects in different ways using different initializations.
  • Operator Overloading: Allows you to define custom behavior for operators (like +, -, ==, etc.) when they are used with objects of user-defined classes. This makes objects of your classes work in a natural and intuitive way with operators.

 


배움을 기록하기 위한 공간입니다. 

수정이 필요한 내용이나 공유하고 싶은 것이 있다면 언제든 댓글로 남겨주시면 환영입니다 :D

반응형