Comprehensive study material – click a topic below to jump
What is C++? C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C language. It adds object-oriented programming (OOP) features to C. C++ supports procedural, object-oriented, and generic programming.
Key Features:
C++ क्या है? C++ एक सामान्य प्रयोग की प्रोग्रामिंग भाषा है जिसे ब्योर्न स्ट्रॉस्ट्रुप ने C भाषा के विस्तार के रूप में बनाया था। इसमें ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) की सुविधाएँ जोड़ी गई हैं। C++ प्रोसीजरल, OOP और जेनेरिक प्रोग्रामिंग को सपोर्ट करती है।
मुख्य विशेषताएँ:
// First C++ program
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Explanation: #include <iostream> includes input/output stream. using namespace std; allows use of standard names. cout outputs text.
व्याख्या: #include <iostream> इनपुट/आउटपुट स्ट्रीम लाइब्रेरी शामिल करता है। using namespace std; से हम स्टैंडर्ड नेमस्पेस का उपयोग कर सकते हैं। cout आउटपुट के लिए है।
// single line, /* multi-line */.main and Main are different.// एक लाइन, /* एक से ज्यादा लाइनें */।// Example
#include <iostream>
using namespace std;
int main() {
int a = 10; // semicolon
int b = 20;
int sum = a + b;
cout << "Sum = " << sum << endl;
return 0;
}
Primary: int, float, double, char, bool, void
Derived: arrays, pointers, references, functions
User-defined: classes, structures, unions, enums
मूल: int, float, double, char, bool, void
डिराइव्ड: ऐरे, पॉइंटर्स, रेफरेंसेस, फंक्शन
यूजर-डिफाइंड: क्लास, स्ट्रक्चर, यूनियन, एनम
#include <iostream>
using namespace std;
int main() {
int age = 21;
float price = 99.99;
double pi = 3.1415926535;
char grade = 'A';
bool isPass = true;
cout << "Age: " << age << endl;
cout << "Price: " << price << endl;
cout << "Pi: " << pi << endl;
cout << "Grade: " << grade << endl;
cout << "Pass: " << isPass << endl;
return 0;
}
Variables: named memory locations. Declaration: data_type var_name;
Constants: values that cannot change. Use const keyword: const int DAYS = 7;
वेरिएबल: नाम दिया गया मेमोरी लोकेशन। डिक्लेरेशन: डेटा_टाइप वेरिएबल_नाम;
कॉन्स्टैंट्स: वैल्यू जो बदली नहीं जा सकती। const कीवर्ड का प्रयोग: const int DAYS = 7;
int a = 10; // variable const int B = 20; // constant a = 15; // ok // B = 25; // error
cout for output, cin for input, endl for newline.
cout आउटपुट के लिए, cin इनपुट के लिए, endl नई लाइन के लिए।
#include <iostream>
using namespace std;
int main() {
int age;
string name;
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
cout << "Hello " << name << ", age " << age << endl;
return 0;
}
Arithmetic: +, -, *, /, %
Relational: ==, !=, <, >, <=, >=
Logical: &&, ||, !
Assignment: =, +=, -=, *=, /=, %=
Increment/Decrement: ++, --
Bitwise: &, |, ^, ~, <<, >>
Conditional: ? :
अरिथमेटिक: +, -, *, /, %
रिलेशनल: ==, !=, <, >, <=, >=
लॉजिकल: &&, ||, !
असाइनमेंट: =, +=, -=, *=, /=, %=
इन्क्रीमेंट/डिक्रीमेंट: ++, --
बिटवाइज: &, |, ^, ~, <<, >>
कंडीशनल: ? :
int a=10, b=3; cout << a+b; // 13 cout << (a>b); // 1 (true) a++; // a=11
if, else if, else, switch.
if, else if, else, switch.
int num;
cin >> num;
if (num > 0) cout << "Positive";
else if (num == 0) cout << "Zero";
else cout << "Negative";
switch(day) {
case 1: cout << "Mon"; break;
case 2: cout << "Tue"; break;
default: cout << "Other";
}
for, while, do-while.
for, while, do-while.
for (int i=1; i<=5; i++) cout << i;
int j=1; while(j<=5) { cout << j; j++; }
int k=1; do { cout << k; k++; } while(k<=5);
Collection of same-type elements. Declaration: int arr[5];
एक ही टाइप के एलिमेंट्स का समूह। डिक्लेरेशन: int arr[5];
int arr[5] = {10,20,30,40,50};
for(int i=0; i<5; i++) cout << arr[i] << " ";
Use #include <string>. C++ string class.
#include <string> का उपयोग करें। C++ string क्लास।
#include <string> string s = "Hello"; cout << s.length() << s[0]; string t = "World"; string u = s + " " + t;
Function overloading, default arguments, pass by value/reference.
फंक्शन ओवरलोडिंग, डिफॉल्ट आर्ग्यूमेंट्स, पास बाय वैल्यू/रेफरेंस।
int add(int a, int b) { return a+b; }
double add(double a, double b) { return a+b; } // overload
void swap(int &x, int &y) { int t=x; x=y; y=t; } // reference
Pointer stores address of another variable. Use & to get address, * to dereference.
पॉइंटर दूसरे वेरिएबल का एड्रेस स्टोर करता है। एड्रेस के लिए &, वैल्यू के लिए *।
int x = 10; int *ptr = &x; cout << *ptr; // 10 *ptr = 20; // x becomes 20
Reference is an alias to an existing variable. Must be initialized.
रेफरेंस किसी मौजूदा वेरिएबल का उपनाम होता है। इनिशियलाइज करना जरूरी है।
int a = 5; int &ref = a; // ref is alias of a ref = 10; // a becomes 10
Use new and delete for dynamic allocation.
डायनामिक एलोकेशन के लिए new और delete का उपयोग करें।
int *p = new int(5); *p = 10; delete p; int *arr = new int[10]; arr[0] = 1; delete[] arr;
Class is blueprint for objects. Access specifiers: public, private, protected.
क्लास ऑब्जेक्ट का खाका है। एक्सेस स्पेसिफायर: public, private, protected।
class Student {
private:
string name;
int age;
public:
void setData(string n, int a) { name=n; age=a; }
void display() { cout << name << " " << age; }
};
Student s;
s.setData("Raj",20);
s.display();
Constructor called when object created. Destructor called when object destroyed. Can be overloaded. Copy constructor, this pointer.
कंस्ट्रक्टर ऑब्जेक्ट बनने पर कॉल होता है। डिस्ट्रक्टर ऑब्जेक्ट नष्ट होने पर कॉल होता है। इन्हें ओवरलोड किया जा सकता है। कॉपी कंस्ट्रक्टर, this पॉइंटर।
class Rectangle {
int w,h;
public:
Rectangle() { w=0; h=0; } // default
Rectangle(int a, int b) { w=a; h=b; } // parameterized
Rectangle(const Rectangle &r) { w=r.w; h=r.h; } // copy
~Rectangle() { cout << "destroyed"; }
};
Deriving new classes from existing ones. Types: single, multiple, multilevel, hierarchical, hybrid.
मौजूदा क्लास से नई क्लास बनाना। प्रकार: single, multiple, multilevel, hierarchical, hybrid।
class Shape {
protected:
int width, height;
public:
void setDim(int w, int h) { width=w; height=h; }
};
class Rectangle : public Shape {
public:
int area() { return width*height; }
};
Runtime polymorphism via virtual functions. Abstract classes (pure virtual).
रनटाइम पॉलीमॉर्फिज्म वर्चुअल फंक्शन के जरिए। एब्सट्रैक्ट क्लास (pure virtual)।
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
Base *b = new Derived();
b->show(); // Derived
Define custom behavior for operators (+, -, etc.) for user-defined types.
यूजर-डिफाइंड टाइप के लिए ऑपरेटरों का व्यवहार परिभाषित करना।
class Complex {
int real, imag;
public:
Complex operator+(const Complex &c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
};
Friend functions can access private members. Static members belong to class, not object.
फ्रेंड फंक्शन प्राइवेट मेंबर्स एक्सेस कर सकते हैं। स्टैटिक मेंबर क्लास के होते हैं, ऑब्जेक्ट के नहीं।
class A {
private:
int x;
public:
friend void show(A &a);
static int count;
};
int A::count = 0;
void show(A &a) { cout << a.x; }
Function templates and class templates for generic programming.
फंक्शन टेम्पलेट और क्लास टेम्पलेट जेनेरिक प्रोग्रामिंग के लिए।
template <typename T>
T add(T a, T b) { return a+b; }
template <class T>
class Box {
T data;
public:
void set(T d) { data = d; }
T get() { return data; }
};
Containers: vector, list, map, set. Algorithms: sort, find, etc. Iterators.
कंटेनर: vector, list, map, set। एल्गोरिदम: sort, find, आदि। इटरेटर।
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v = {5,2,8,1};
sort(v.begin(), v.end());
for(int x : v) cout << x << " ";
map<string,int> m;
m["apple"] = 10;
cout << m["apple"];
Use try, catch, throw to handle runtime errors.
रनटाइम एरर हैंडल करने के लिए try, catch, throw का उपयोग।
try {
int age;
cin >> age;
if (age < 18) throw age;
cout << "Welcome";
} catch(int e) {
cout << "Underage: " << e;
}
Use fstream, ifstream, ofstream.
fstream, ifstream, ofstream का उपयोग करें।
#include <fstream>
ofstream out("file.txt");
out << "Hello";
out.close();
ifstream in("file.txt");
string line;
getline(in, line);
in.close();
auto i = 10; // type deduced
vector<int> v = {1,2,3};
for (auto x : v) cout << x; // range-based
auto lambda = [](int a, int b) { return a+b; };
cout << lambda(5,3);
unique_ptr<int> p(new int(5)); // smart pointer