C++ के Advanced Interview Questions
In this page:
Smart Pointers और RAII
Smart pointers dynamic memory पर RAII (Resource Acquisition Is Initialization) apply करते हैं: std::unique_ptr single ownership enforce करता है और scope से बाहर जाने पर automatically अपना object free करता है, जबकि std::shared_ptr reference counting के जरिए कई owners allow करता है -- दोनों एक manual delete याद रखने की ज़रूरत खत्म करते हैं।
उदाहरण: Smart Pointers and RAII
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
std::unique_ptr<int> ptr = std::make_unique<int>(5);
// Print to the console with cout
std::cout << *ptr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Move Semantics और Rvalue References
Move semantics एक object के internal resources -- जैसे एक heap buffer -- को एक temporary से एक नए object में pointer reassignment के जरिए transfer होने देता है, एक पूरी deep copy perform करने की बजाय।
यही चीज़ modern C++ में functions से बड़े objects return करना सस्ता बनाती है।
उदाहरण: Move Semantics and rvalue references
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named source (a resizable array)
std::vector<int> source = {1, 2, 3};
// Create a vector named target (a resizable array)
std::vector<int> target = std::move(source);
// Print to the console with cout
std::cout << target.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Virtual Destructors
एक base class जिसे एक base-class pointer के जरिए delete किया जाना है उसे अपना destructor virtual declare करना चाहिए; नहीं तो, एक base pointer के जरिए एक derived object delete करना सिर्फ base class का destructor चलाता है, derived class का cleanup skip करते हुए और इसके owned कोई भी resources leak करते हुए।
उदाहरण: Virtual Destructors
// Include std::cout and std::cin
#include <iostream>
// Define the type Base
class Base {
// Members below can be accessed from outside the class
public:
virtual ~Base() { std::cout << "Base destroyed" << std::endl; }
};
// Define the type Derived
class Derived : public Base {
// Members below can be accessed from outside the class
public:
~Derived() { std::cout << "Derived destroyed" << std::endl; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Base *b = new Derived();
// Free the heap memory with delete
delete b;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
SFINAE और Concepts
SFINAE ("Substitution Failure Is Not An Error") एक template-metaprogramming technique है जहाँ एक invalid template substitution एक compile error cause करने की बजाय silently overload resolution से discard हो जाता है, templates को यह constrain करने देते हुए कि वे कौन-से types accept करते हैं।
C++20 Concepts इसे readable, explicit syntax में formalize करते हैं।
उदाहरण: SFINAE and Concepts
// Include std::cout and std::cin
#include <iostream>
#include <type_traits>
// Declare a template so this works with any suitable type
template <typename T>
typename std::enable_if<std::is_integral<T>::value, void>::type
show(T value) {
// Print to the console with cout
std::cout << "Integral: " << value << std::endl;
}
// Program execution starts in main()
int main() {
show(5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Perfect Forwarding और std::forward
Perfect forwarding, universal references (T&&) लेने वाले एक template function के अंदर std::forward इस्तेमाल करते हुए, यह preserve करता है कि हर argument originally एक lvalue था या rvalue जब यह किसी दूसरे function को pass होता है -- generic wrapper functions के लिए essential जिन्हें accidentally एक unnecessary copy force नहीं करना चाहिए।
उदाहरण: Perfect Forwarding and std::forward
// Include std::cout and std::cin
#include <iostream>
#include <utility>
void process(int &x) { std::cout << "lvalue" << std::endl; }
void process(int &&x) { std::cout << "rvalue" << std::endl; }
// Declare a template so this works with any suitable type
template <typename T>
void wrapper(T &&arg) {
process(std::forward<T>(arg));
}
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
wrapper(a);
wrapper(10);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
shared_ptrऔरunique_ptrownership interchangeable हैं, जबunique_ptrसिर्फ एक owner allow करता है। - इस पर
std::movecall करने के बाद एक object इस्तेमाल करना। - polymorphically इस्तेमाल होने वाली एक base class में एक
virtualdestructor भूल जाना।
- Namespaces, header files, और multi-file programming बड़े projects organize करते हैं।
- चार cast operators,
static_cast,dynamic_cast,const_cast, औरreinterpret_cast, अलग-अलग तरीकों से types convert करते हैं। - Threads, mutexes और locks,
asyncऔरfuture, debugging, input validation, mini projects, और interview questions C++ को real work में apply करते हैं।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced