← Back to C++ Course | Chapter 8: OOP Core | Lesson 1 of 14

C++ OOP का परिचय

Object-oriented programming code को real-world चीज़ों की तरह organize करता है, हर एक के facts और actions होते हैं, जैसे एक नाम वाला एक dog जो bark कर सकता है। यह बड़े programs को neat रखता है।
Syntax
cpp
class ClassName {
private:
  data_type member_variable;
public:
  return_type method_name() { }
};

OOP क्या है?

Object-Oriented Programming (OOP) code को actions के एक sequence की बजाय objects — data और behavior के bundles — के around organize करता है।

Real-world entities को classes और objects की तरह model करके, OOP आपको ऐसे programs लिखने के लिए encourage करता है जो modular, reusable, और बढ़ने के साथ reason करना आसान हों।

उदाहरण: What is OOP?

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 drive() { std::cout << "Driving" << std::endl; }
};

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

Encapsulation

Encapsulation एक class के data variables को इस पर operate करने वाले methods के साथ एक single unit में bundle करता है, और उस internal state तक direct outside access restrict करता है।

यह एक object के data को ऐसे तरीकों से बदलने से protect करता है जो इसे एक invalid या inconsistent condition में छोड़ देंगे।

उदाहरण: Encapsulation

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

// Define the type Account
class Account {
// Members below are only accessible inside the class
private:
	double balance = 0;
// Members below can be accessed from outside the class
public:
	void deposit(double amount) { balance += amount; }
	double getBalance() { return balance; }
};

// Program execution starts in main()
int main() {
	Account acc;
	acc.deposit(100);
	// Print to the console with cout
	std::cout << acc.getBalance() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Abstraction

Abstraction एक object के complex inner implementation को hide करता है और सिर्फ essential features expose करता है जो एक user को actually चाहिए।

यह simplify करता है कि दूसरा code आपकी classes के साथ कैसे interact करता है, callers को internally यह focus करने देते हुए कि एक object क्या करता है, यह नहीं कि कैसे करता है।

उदाहरण: Abstraction

cpp
#include <iostream>

class Car {
public:
	void start() { std::cout << "Car started" << std::endl; } // hides engine details
};

int main() {
	Car car;
	car.start();
	return 0;
}

Inheritance

Inheritance एक नई class को एक existing class की properties और methods acquire करने देता है, उनके बीच एक parent-child relationship establish करते हुए।

यह related classes को scratch से same behavior reimplement करने की बजाय एक common base share करने देकर redundant code कम करता है।

उदाहरण: Inheritance

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:
	void eat() { std::cout << "Eating" << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void bark() { std::cout << "Barking" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.eat();
	d.bark();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Polymorphism

Polymorphism का literally मतलब है 'many forms' — यह आपको अलग classes के objects पर same function नाम call करने देता है और हर एक को अपने specific तरीके से respond कराता है।

यही है जो एक base type के against लिखे generic code को किसी भी derived type के साथ सही काम करने देता है, present या future।

उदाहरण: Polymorphism

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 << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Meow" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Animal *a = new Cat();
	a->speak();
	// Free the heap memory with delete
	delete a;
	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. हर data member को public बनाना, जो encapsulation तोड़ता है और किसी भी code को object की state corrupt करने देता है।
  2. एक class (blueprint) को एक object (इससे बनाया गया एक real instance) से confuse करना।
  3. Inheritance को सिर्फ code reuse करने के लिए इस्तेमाल करना, जब इसे एक is-a relationship model करना चाहिए।

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.