C++ के Interview Questions
In this page:
Virtual Function क्या है?
एक virtual function virtual keyword इस्तेमाल करते हुए एक base class में declare होता है और derived classes से override हो सकता है; इसे एक base-class pointer या reference के जरिए call करना जो भी derived-class version actually runtime पर object के real type से match करे उसे invoke करता है -- यही mechanism runtime polymorphism के पीछे है।
उदाहरण: What is a Virtual Function?
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
virtual void speak() { std::cout << "..." << std::endl; }
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
void speak() override { std::cout << "Woof" << std::endl; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Animal *a = new Dog();
a->speak();
// Free the heap memory with delete
delete a;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer बनाम Reference
एक pointer एक memory address store करता है और कहीं और point करने के लिए reassign हो सकता है, या "no target" represent करने के लिए nullptr set हो सकता है। एक reference एक existing variable का alias है -- यह declare होने के moment पर bind होना चाहिए और बाद में कभी किसी और चीज़ की ओर refer करने के लिए reseat नहीं हो सकता।
उदाहरण: Pointer vs. Reference
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 5, b = 10;
int *ptr = &a;
ptr = &b;
int &ref = a;
// Print to the console with cout
std::cout << *ptr << " " << ref << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor क्या है?
एक constructor एक special member function है जिसका नाम इसकी class जैसा है, जो object बनते ही automatically call होता है, जिसका काम उस object को एक valid initial state में डालना है -- typically इसके member variables को arguments या defaults से initialize करके।
उदाहरण: What is a Constructor?
// Include std::cout and std::cin
#include <iostream>
// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
Car() { std::cout << "Car constructed" << std::endl; }
};
// Program execution starts in main()
int main() {
Car car;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Struct बनाम Class
C++ में, struct और class एक default को छोड़कर functionally identical हैं: struct के members अन्यथा बताए जाने तक public हैं, जबकि class के members अन्यथा बताए जाने तक private हैं।
Convention अभी भी simple data-holding types के लिए struct और real behavior वाले types के लिए class इस्तेमाल करता है।
उदाहरण: Struct vs. Class
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; };
// Define the type Box
class Box { int size; };
// Program execution starts in main()
int main() {
Point p;
p.x = 5;
// Print to the console with cout
std::cout << p.x << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
nullptr क्या है?
nullptr, जो C++11 में introduce हुआ, एक type-safe null pointer literal है जो "no pointer" represent करने के लिए C के integer 0 (या NULL macro) इस्तेमाल करने से आई एक लंबे समय से चली आ रही ambiguity fix करता है, खासकर overload resolution के आसपास जहाँ एक int और एक pointer parameter दोनों accidentally plain 0 से match हो सकते थे।
उदाहरण: What is nullptr?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int *ptr = nullptr;
// Print to the console with cout
std::cout << (ptr == nullptr) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- answer देते वक्त pointer और reference behavior mix up करना।
- यह भूल जाना कि struct के members default से public होते हैं जबकि class के private।
- यह भूल जाना कि constructors का कोई return type नहीं होता।
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