लोड हो रहा है...

क्लासेस और ऑब्जेक्ट्स

C++ में क्लासेस और ऑब्जेक्ट्स ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग (OOP) के मूल तत्व हैं। क्लास एक ब्लूप्रिंट होती है जो किसी ऑब्जेक्ट की विशेषताओं (variables) और व्यवहार (methods) को परिभाषित करती है, जबकि ऑब्जेक्ट क्लास की एक वास्तविक इंस्टेंस होती है। क्लासेस और ऑब्जेक्ट्स का उपयोग करने से हम कोड को मॉड्यूलर, पुन: प्रयोज्य और मेंटेन करने योग्य बना सकते हैं।
C++ में क्लासेस encapsulation, inheritance और polymorphism जैसी OOP प्रिंसिपल्स को लागू करने में मदद करती हैं। इसके माध्यम से हम memory management, error handling और data protection को नियंत्रित कर सकते हैं। advanced C++ development में क्लासेस का प्रयोग complex data structures, algorithm implementation और system architecture के लिए किया जाता है।
इस ट्यूटोरियल में, आप सीखेंगे कि कैसे क्लासेस को define किया जाता है, ऑब्जेक्ट्स को instantiate किया जाता है, methods को implement किया जाता है और algorithms को क्लासेस में लागू किया जाता है। साथ ही, best practices जैसे const correctness, memory management, STL integration और performance optimization को समझाया जाएगा। यह ज्ञान software development और scalable C++ applications के निर्माण में उपयोगी होगा।

मूल उदाहरण

text
TEXT Code
\#include <iostream>
\#include <string>

class Employee {
private:
std::string name;
int id;
double salary;

public:
// Constructor
Employee(const std::string& empName, int empId, double empSalary)
: name(empName), id(empId), salary(empSalary) {}

// Setter methods
void setName(const std::string& empName) { name = empName; }
void setId(int empId) { id = empId; }
void setSalary(double empSalary) { salary = empSalary; }

// Getter methods
std::string getName() const { return name; }
int getId() const { return id; }
double getSalary() const { return salary; }

// Display method
void display() const {
std::cout << "Employee ID: " << id << "\n"
<< "Name: " << name << "\n"
<< "Salary: $" << salary << "\n";
}

};

int main() {
Employee e1("Ravi Kumar", 101, 75000.50);
e1.display();

e1.setSalary(80000.75);
std::cout << "Updated Salary: $" << e1.getSalary() << "\n";

return 0;

}

इस उदाहरण में Employee क्लास में तीन private attributes हैं: name, id और salary। encapsulation के माध्यम से डेटा सुरक्षित रहता है और public setter/getter methods के जरिए controlled access मिलता है। Constructor object के निर्माण के समय attributes initialize करता है। Getter methods को const घोषित किया गया है ताकि object का state बदल न सके।
display() method object के state और behavior को दर्शाता है। setSalary() और getSalary() के उदाहरण से private डेटा को सुरक्षित तरीके से modify करना दिखाया गया है। यह संरचना real-world applications जैसे employee management system में आसानी से लागू की जा सकती है। Constructor और stack-based object allocation के उपयोग से memory leaks को रोका जा सकता है।

व्यावहारिक उदाहरण

text
TEXT Code
\#include <iostream>
\#include <vector>
\#include <algorithm>

class Product {
private:
std::string name;
int id;
double price;

public:
Product(const std::string& prodName, int prodId, double prodPrice)
: name(prodName), id(prodId), price(prodPrice) {}

void setPrice(double prodPrice) { price = prodPrice; }
double getPrice() const { return price; }
std::string getName() const { return name; }

void display() const {
std::cout << "Product ID: " << id << ", Name: " << name
<< ", Price: $" << price << "\n";
}

};

class Inventory {
private:
std::vector<Product> products;

public:
void addProduct(const Product& prod) { products.push_back(prod); }

void displayAll() const {
std::cout << "Inventory:\n";
for (const auto& prod : products)
prod.display();
}

void applyDiscount(double percent) {
for (auto& prod : products) {
double discounted = prod.getPrice() * (1.0 - percent / 100.0);
prod.setPrice(discounted);
}
}

void sortByPrice() {
std::sort(products.begin(), products.end(),
[](const Product& a, const Product& b) { return a.getPrice() < b.getPrice(); });
}

};

int main() {
Inventory shop;
shop.addProduct(Product("Laptop", 201, 1200.00));
shop.addProduct(Product("Smartphone", 202, 800.50));
shop.addProduct(Product("Headphones", 203, 150.75));

shop.displayAll();
std::cout << "\nApplying 10% discount...\n";
shop.applyDiscount(10);
shop.sortByPrice();
shop.displayAll();

return 0;

}

यह उदाहरण Product और Inventory क्लासेस का उपयोग कर real-world scenario दिखाता है। Product क्लास individual products को represent करती है, जबकि Inventory क्लास उनके collection को manage करती है। applyDiscount() और sortByPrice() methods algorithms को क्लास में लागू करना दिखाती हैं। STL container और lambda expressions का उपयोग करके memory leaks से बचा गया है।
यह OOP principles जैसे encapsulation, aggregation और safe operations को स्पष्ट करता है। const correctness, reference passing और STL integration robustness और maintainability बढ़ाते हैं। Single-Responsibility principle के अनुसार responsibility को अलग करना software extensibility और testability में मदद करता है।

📊 संदर्भ तालिका

C++ Element/Concept Description Usage Example
Class Object blueprint defining variables and methods class Employee { private: std::string name; public: void setName(std::string n) { name=n; } };
Object Instance of a class with state and behavior Employee e1("Ravi",101,75000);
Constructor Initializes objects at creation Employee(const std::string& n,int i,double s):name(n),id(i),salary(s){}
Destructor Manages resources when object is destroyed \~Employee() { /* cleanup */ }
Encapsulation Protects data and controls access private: int id; public: void setId(int i){id=i;}
Member Function Method inside a class to manipulate data void display() const { std::cout<\<name; }

क्लासेस और ऑब्जेक्ट्स का mastery complex systems को model करने, efficient algorithms implement करने और maintainable software architectures बनाने में मदद करता है। मुख्य सीख यह है कि क्लासेस encapsulation और controlled behavior प्रदान करती हैं, जिससे बड़े projects manageable और extensible बनते हैं। अगले कदम में inheritance, polymorphism और design patterns सीखकर modular और scalable applications विकसित किए जा सकते हैं। C++ documentation, STL tutorials और advanced books आगे सीखने में सहायक हैं।

🧠 अपने ज्ञान की परीक्षा करें

शुरू करने के लिए तैयार

Test Your Knowledge

Test your understanding of this topic with practical questions.

4
प्रश्न
🎯
70%
पास करने के लिए
♾️
समय
🔄
प्रयास

📝 निर्देश

  • हर प्रश्न को ध्यान से पढ़ें
  • हर प्रश्न के लिए सबसे अच्छा उत्तर चुनें
  • आप जितनी बार चाहें क्विज़ दोबारा दे सकते हैं
  • आपकी प्रगति शीर्ष पर दिखाई जाएगी