Theory
चार words जो आपका program रोक देते हैं
आप canteen till की classic पहली line type करते हैं:
cout << "Welcome to CanteenTill";
Compiler मना कर देता है: 'cout' was not declared in this scope।
आपकी logic में कुछ ग़लत नहीं है। Compiler ने बस कभी cout के बारे में सुना ही नहीं। किसी को उनका introduction कराना होगा, और वह introduction एक line है: #include <iostream>।
Theory
Supplier catalogue
Canteen अपनी tea leaves ख़ुद नहीं उगाता। यह supplier catalogues रखता है: beverage catalogue से order कीजिए, grain catalogue से, packaging catalogue से।
Header files आपके program की catalogues हैं। iostream input/output catalogue है, cmath mathematics catalogue। #include उस catalogue को आपकी desk पर रखता है ताकि compiler को उससे order किए जाने वाले हर item का पता हो।
Theory
एक header file असल में क्या है
एक header file declarations रखती है: standard library के ready-made functions, objects और classes के नाम और signatures।
#include एक preprocessor directive है: compilation शुरू होने से पहले, preprocessor header का content literally आपकी file में paste कर देता है। इसी वजह से यह # से शुरू होता है और कोई semicolon नहीं लेता।
Declarations compiler को बताते हैं क्या exist करता है; बाद में link हुआ compiled library code बताता है यह कैसे काम करता है।
At a glance
Headers जो BCA304 programs असल में इस्तेमाल करते हैं
| Header | यह क्या देता है | किसके लिए इस्तेमाल |
|---|---|---|
| <iostream> | cout, cin, endl | Screen output, keyboard input |
| <cstring> | strlen, strcpy, strcat, strcmp | C-string handling |
| <cmath> | sqrt, pow, abs, floor | Mathematics |
| <cstdlib> | rand, atoi, exit | Utilities, conversions |
| <iomanip> | setw, setprecision | Output columns format करना |
| <fstream> | ifstream, ofstream | Files पढ़ना, लिखना |
Theory
Angle brackets बनाम quotes, .h बनाम बिना .h
Angle brackets के साथ #include <iostream>: compiler अपनी system directories search करता है। इसे standard library headers के लिए इस्तेमाल कीजिए।
Quotes के साथ #include "myitems.h": compiler पहले आपका current folder search करता है। इसे उन headers के लिए इस्तेमाल कीजिए जो आपने ख़ुद लिखे।
Purane C headers को C++ spelling मिलती है: string.h <cstring> बन जाता है, math.h <cmath> बन जाता है, stdlib.h <cstdlib> बन जाता है। दोनों spellings exams में काम करती हैं, पर पूछे जाने पर modern वाला नाम लीजिए।
Quiz
Till program एक loyalty formula के लिए sqrt(payment) compute करता है और setw(8) से padded amounts print करता है। कौन सी headers की pair इसे include करनी होगी?
- <cmath> और <iomanip>
- <iostream> और <cstring>
- <cmath> और <cstdlib>
- <iomanip> और <fstream>
Show the answer
<cmath> और <iomanip>
sqrt <cmath> में रहता है और setw <iomanip> में रहता है। <iostream> cout के लिए ज़रूर भी मौजूद होगा, पर सवाल पूछता है इन दो functions को क्या चाहिए। <cstring> strings के लिए है, <cstdlib> rand/atoi के लिए, <fstream> files के लिए: असली families, sqrt और setw के लिए ग़लत catalogues।
Think first
Compiler की शिकायत का अंदाज़ा लगाइए
एक program सिर्फ़ <iostream> include करता है, फिर strlen(name) call करता है। tap करने से पहले: क्या यह compile होता है, और अगर नहीं, error क्या कहेगा?
Show the answer
यह कुछ ऐसे fail होता है 'strlen' was not declared in this scope। <iostream> include करना सिर्फ़ input/output family introduce करता है। strlen <cstring> का है, एक अलग catalogue। एक header कभी पूरी library नहीं ख़रीदता: हर #include declarations की exactly एक family खोलता है।
Watch out
Trap: एक include सबको control करे
Students मान लेते हैं <iostream> सब कुछ unlock करता है क्योंकि उनके पहले programs को कभी सिर्फ़ इसी की ज़रूरत थी। फिर sqrt या strcmp रहस्यमय तरीक़े से fail होते हैं।
Rule: हर family को अपना include चाहिए। यह भी याद रखिए using namespace std; आपके programs में चुपचाप काम कर रहा है: इसके बिना आपको std::cout लिखना होगा। Exams पूछते हैं cout कहाँ defined है; जवाब है: std namespace में, <iostream> में declared।
Theory
आप उनसे फिर कहाँ मिलेंगे
हर एक BCA304 program <iostream> से शुरू होता है। आने वाला strings lesson <cstring> पर टिकता है। बाद में आपके course में file handling <fstream> इस्तेमाल करती है। और <cstdlib> का rand() simulations और games को power देता है। ऊपर वाली table एक बार सीखिए और आप किस function को कौन सी header चाहिए इस पर आसान 2-mark सवाल खोना बंद कर देंगे।
Summary
Key takeaways
- एक header file ready-made library names की declarations देती है; #include compilation से पहले उन्हें paste करता है।
- #include एक preprocessor directive है: # से शुरू होता है, semicolon नहीं चाहिए।
- <> system directories search करता है; "" पहले आपका folder search करता है।
- Modern spellings: <cstring>, <cmath>, <cstdlib> string.h, math.h, stdlib.h की जगह लेते हैं।
- एक header = एक family: I/O के लिए iostream, strings के लिए cstring, maths के लिए cmath, formatting के लिए iomanip, files के लिए fstream।
- Memory hook: catalogue से order कीजिए, tea ख़ुद मत उगाइए।