Variables & cin/cout
C++ mein variables C jaise hi hain (int, float, double, char, bool naya hai — true/false ke liye), lekin input/output ke liye cin/cout stream-based approach use karta hai, printf/scanf ki jagah — type-safe hai (format specifiers %d/%f yaad nahi rakhne padte).
cout << value se print hota hai, cin >> variable se input milta hai. Multiple values ko chain kar sakte ho: cout << a << " " << b; — bahut readable lagta hai comparison mein.
#include <iostream>
using namespace std;
int main() {
int age;
bool isStudent = true; // bool naya hai C se — true/false
cout << "Apni age batao: ";
cin >> age;
cout << "Tum " << age << " saal ke ho" << endl;
cout << "Student ho? " << isStudent << endl; // 1 (true)
return 0;
}- cin >> = input lo (extract from stream)
- cout << = output do (insert into stream)
- bool = naya type, true/false ke liye
C++11 se auto keyword compiler ko batata hai "khud type figure out karo" initialization value se — code likhna aasan ho jaata hai, especially complex types (jaise iterators) ke liye, lekin readability ka khayal rakhte hue use karo.
auto x = 10; // compiler samajhta hai: int
auto y = 3.14; // double
auto name = "Aarav"; // const char*
auto z = x + y; // double (int + double = double)
cout << z << endl;C++ ka std::string (header <string>) C ke char array se bahut behtar hai — dynamic size (khud grow hota hai), built-in operators (+, ==), aur bahut saare helper methods (.length(), .substr(), .find()).
#include <string>
using namespace std;
string name = "Aarav";
name += " Kumar"; // + operator se concatenation!
cout << name << endl; // "Aarav Kumar"
cout << name.length() << endl; // 11
if (name == "Aarav Kumar") { // == operator seedha compare karta hai
cout << "Match!" << endl;
}