C++ OOP का परिचय
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Encapsulation
Encapsulation एक class के data variables को इस पर operate करने वाले methods के साथ एक single unit में bundle करता है, और उस internal state तक direct outside access restrict करता है।
यह एक object के data को ऐसे तरीकों से बदलने से protect करता है जो इसे एक invalid या inconsistent condition में छोड़ देंगे।
उदाहरण: Encapsulation
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Abstraction
Abstraction एक object के complex inner implementation को hide करता है और सिर्फ essential features expose करता है जो एक user को actually चाहिए।
यह simplify करता है कि दूसरा code आपकी classes के साथ कैसे interact करता है, callers को internally यह focus करने देते हुए कि एक object क्या करता है, यह नहीं कि कैसे करता है।
उदाहरण: Abstraction
#include <iostream>
class Car {
public:
void start() { std::cout << "Car started" << std::endl; } // hides engine details
};
int main() {
Car car;
car.start();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Inheritance
Inheritance एक नई class को एक existing class की properties और methods acquire करने देता है, उनके बीच एक parent-child relationship establish करते हुए।
यह related classes को scratch से same behavior reimplement करने की बजाय एक common base share करने देकर redundant code कम करता है।
उदाहरण: Inheritance
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Polymorphism
Polymorphism का literally मतलब है 'many forms' — यह आपको अलग classes के objects पर same function नाम call करने देता है और हर एक को अपने specific तरीके से respond कराता है।
यही है जो एक base type के against लिखे generic code को किसी भी derived type के साथ सही काम करने देता है, present या future।
उदाहरण: Polymorphism
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- हर data member को public बनाना, जो encapsulation तोड़ता है और किसी भी code को object की state corrupt करने देता है।
- एक class (blueprint) को एक object (इससे बनाया गया एक real instance) से confuse करना।
- Inheritance को सिर्फ code reuse करने के लिए इस्तेमाल करना, जब इसे एक is-a relationship model करना चाहिए।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: