← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 8 of 11

C++ में Abstraction

Abstraction का मतलब है यह दिखाना कि कोई चीज़ क्या करती है और कैसे करती है यह hide करना, electronics जाने बिना एक TV remote इस्तेमाल करने जैसा। यह code को इस्तेमाल करना simpler बनाता है।
Syntax
cpp
class AbstractClass {
public:
  virtual return_type method_name() = 0;
};

class Concrete : public AbstractClass {
public:
  return_type method_name() override { }
};

Abstraction क्या है?

Abstraction एक object की complex internal implementation hide करने का process है जबकि सिर्फ essential features expose करना जो एक user को actually इसके साथ interact करने के लिए चाहिए।

यह programmers को उन low-level mechanics से distract हुए बिना high-level concepts के साथ काम करने देता है जो उन्हें जानने की ज़रूरत नहीं।

उदाहरण: What is Abstraction?

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:
	void start() {
		// Print to the console with cout
		std::cout << "Car started" << std::endl;
	}
};

// Program execution starts in main()
int main() {
	Car car;
	car.start();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Abstract Classes

एक abstract class specifically दूसरी classes के लिए एक blueprint की तरह serve करने के लिए designed class है: इसमें कम से कम एक pure virtual function होता है और, एक direct consequence की तरह, इसे कभी अपने आप instantiate नहीं किया जा सकता।

उदाहरण: Abstract Classes

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	virtual void draw() = 0;
};

// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
	void draw() override { std::cout << "Circle" << std::endl; }
};

// Program execution starts in main()
int main() {
	Circle c;
	c.draw();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pure Virtual Functions

एक pure virtual function base class में अपने कोई implementation के साथ declared होता है — इसकी body को declaration को 0 के बराबर set करके पूरी तरह replace किया जाता है — उस specific behavior को पूरी तरह उस subclass पर छोड़ते हुए जो आखिरकार इसे implement करे।

उदाहरण: Pure Virtual Functions

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	virtual void draw() = 0;
};

// Define the type Square
class Square : public Shape {
// Members below can be accessed from outside the class
public:
	void draw() override { std::cout << "Square" << std::endl; }
};

// Program execution starts in main()
int main() {
	Square s;
	s.draw();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Abstraction का Implementation

Abstraction actually practice में लाने के लिए, एक abstract parent से derive करने वाली हर concrete class को इसके inherit किए हर pure virtual function के लिए override करना और एक real implementation देना ज़रूरी है, वरना यह खुद abstract रहती है और इसे भी instantiate नहीं किया जा सकता।

उदाहरण: Implementation of Abstraction

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	virtual void draw() = 0;
};

// Define the type Triangle
class Triangle : public Shape {
// Members below can be accessed from outside the class
public:
	void draw() override { std::cout << "Triangle" << std::endl; }
};

// Program execution starts in main()
int main() {
	Triangle t;
	t.draw();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Abstraction के Advantages

Abstraction code duplication कम करके और एक system के design को इसके implementation details से cleanly अलग करके payoff देता है — एक benefit जो बड़े, evolving codebases को copy-pasted logic का एक tangle बनने की बजाय scalable रखता है।

उदाहरण: Advantages of Abstraction

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type PaymentMethod
class PaymentMethod {
// Members below can be accessed from outside the class
public:
	virtual void pay() = 0;
};

// Define the type Cash
class Cash : public PaymentMethod {
// Members below can be accessed from outside the class
public:
	void pay() override { std::cout << "Paid with cash" << std::endl; }
};

// Program execution starts in main()
int main() {
	Cash cash;
	cash.pay();
	// 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. Abstraction (एक चीज़ क्या करती है यह hide करना) को encapsulation (data और methods bundle करना) से confuse करना।
  2. public interface में implementation details expose करना।
  3. एक concrete derived class में हर pure virtual function implement न करना।

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.