← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 2 of 17

C++ के Interview Questions

Interview questions C++ jobs के लिए apply करते वक्त पूछे जाने वाले classic questions हैं। इन्हें practice करना एक बड़े game से पहले rehearse करने जैसा है।

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?

cpp
// 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;
}

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

cpp
// 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;
}

Constructor क्या है?

एक constructor एक special member function है जिसका नाम इसकी class जैसा है, जो object बनते ही automatically call होता है, जिसका काम उस object को एक valid initial state में डालना है -- typically इसके member variables को arguments या defaults से initialize करके।

उदाहरण: What is a Constructor?

cpp
// 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;
}

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

cpp
// 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;
}

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?

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. answer देते वक्त pointer और reference behavior mix up करना।
  2. यह भूल जाना कि struct के members default से public होते हैं जबकि class के private।
  3. यह भूल जाना कि constructors का कोई return type नहीं होता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.