Theory
ચાર શબ્દ જે તમારો program અટકાવે છે
તમે canteen ના till ની પેલી જાણીતી પહેલી લીટી લખો છો:
cout << "Welcome to CanteenTill";
Compiler ના પાડે છે: 'cout' was not declared in this scope.
તમારા તર્કમાં કશું ખોટું નથી. Compiler એ ફક્ત cout વિશે ક્યારેય સાંભળ્યું જ નથી. કોઈએ એમની ઓળખાણ કરાવવી પડે, અને એ ઓળખાણ એક લીટી છે: #include <iostream>.
Theory
પુરવઠાની સૂચિ
Canteen પોતાની ચાની પત્તી ઉગાડતી નથી. એ પુરવઠાની સૂચિઓ રાખે છે: પીણાંની સૂચિમાંથી મંગાવો, અનાજની સૂચિમાંથી, વેષ્ટનની સૂચિમાંથી.
Header files એ તમારા program ની સૂચિઓ છે. iostream એ input/output ની સૂચિ છે, cmath એ ગણિતની સૂચિ. #include એ સૂચિને તમારા મેજ પર મૂકે છે જેથી compiler ને એમાંથી તમે મંગાવી શકો એવી દરેક વસ્તુની ખબર પડે.
Theory
Header file ખરેખર શું છે
Header file માં declarations હોય છે: standard library ના તૈયાર functions, objects અને classes નાં નામ અને એમનાં સ્વરૂપ.
#include એ preprocessor directive છે: compilation શરૂ થાય એ પહેલાં preprocessor header ની સામગ્રીને શબ્દશઃ તમારી file માં ચોંટાડી દે છે. એટલે જ એ # થી શરૂ થાય છે અને એને અર્ધવિરામ લેતું નથી.
Declarations compiler ને કહે છે કે શું અસ્તિત્વમાં છે; પછીથી જોડાતો compile થયેલો library નો code એ કેવી રીતે કામ કરે છે એ પૂરું પાડે છે.
At a glance
BCA304 ના programs ખરેખર જે headers વાપરે છે
| Header | શું આપે છે | શા માટે વપરાય છે |
|---|---|---|
| <iostream> | cout, cin, endl | પડદા પર output, keyboard થી input |
| <cstring> | strlen, strcpy, strcat, strcmp | C-string ની હેરફેર |
| <cmath> | sqrt, pow, abs, floor | ગણિત |
| <cstdlib> | rand, atoi, exit | ઉપયોગિતાઓ, રૂપાંતરો |
| <iomanip> | setw, setprecision | Output ના સ્તંભ ગોઠવવા |
| <fstream> | ifstream, ofstream | Files વાંચવી, લખવી |
Theory
ખૂણિયા કૌંસ સામે અવતરણચિહ્ન, .h સામે .h વગર
ખૂણિયા કૌંસ સાથે #include <iostream>: compiler પોતાની system directories માં શોધે છે. Standard library નાં headers માટે આ વાપરો.
અવતરણચિહ્ન સાથે #include "myitems.h": compiler પહેલાં તમારા હાલના folder માં શોધે છે. તમે જાતે લખેલાં headers માટે આ વાપરો.
જૂનાં C નાં headers ને C++ ની જોડણી મળે છે: string.h એ <cstring> બને છે, math.h એ <cmath>, stdlib.h એ <cstdlib>. પરીક્ષામાં બંને જોડણી ચાલે છે, પણ પુછાય ત્યારે આધુનિક જોડણી લખો.
Quiz
Till નો program વફાદારીના સૂત્ર માટે sqrt(payment) ગણે છે અને રકમો setw(8) થી ગોઠવીને છાપે છે. એણે કઈ headers ની જોડ include કરવી જ પડે?
- <cmath> અને <iomanip>
- <iostream> અને <cstring>
- <cmath> અને <cstdlib>
- <iomanip> અને <fstream>
Show the answer
<cmath> અને <iomanip>
sqrt એ <cmath> માં વસે છે અને setw એ <iomanip> માં. cout માટે <iostream> પણ ચોક્કસ હાજર હશે જ, પણ પ્રશ્ન પૂછે છે કે આ બે functions ને શું જોઈએ. <cstring> એ strings માટે છે, <cstdlib> એ rand/atoi માટે, <fstream> એ files માટે: ખરેખરાં કુટુંબ, પણ sqrt અને setw માટે ખોટી સૂચિઓ.
Think first
Compiler ની ફરિયાદ ધારો
એક program ફક્ત <iostream> include કરે છે, પછી strlen(name) બોલાવે છે. Tap કરતાં પહેલાં: એ compile થાય છે, અને જો ન થાય તો ભૂલ શું કહેશે?
Show the answer
એ 'strlen' was not declared in this scope જેવા સંદેશા સાથે નિષ્ફળ જાય છે. <iostream> include કરવાથી ફક્ત input/output નું કુટુંબ આવે છે. strlen એ <cstring> નું છે, જુદી સૂચિ. એક header તમને ક્યારેય આખી library ખરીદી આપતું નથી: દરેક #include બરાબર એક કુટુંબનાં declarations ખોલે છે.
Watch out
ફાંદો: એક include બધા પર રાજ કરે
Students માની લે છે કે <iostream> બધું ખોલી નાખે છે, કારણ કે એમના પહેલા programs ને ફક્ત એની જ જરૂર પડી હતી. પછી sqrt કે strcmp રહસ્યમય રીતે નિષ્ફળ જાય છે.
નિયમ: દરેક કુટુંબને પોતાનું include જોઈએ. અને યાદ રાખો કે using namespace std; તમારા programs માં ચૂપચાપ કામ કરી રહ્યું છે: એના વગર તમારે std::cout લખવું પડે. પરીક્ષાઓ પૂછે છે કે cout ક્યાં વ્યાખ્યાયિત છે; જવાબ છે: std namespace માં, <iostream> માં declare થયેલું.
Theory
તમે એમને ફરી ક્યાં મળશો
BCA304 નો દરેકે દરેક program <iostream> થી શરૂ થાય છે. હવે પછીનો strings નો પાઠ <cstring> પર ટેકે છે. તમારા અભ્યાસક્રમમાં આગળ file handling <fstream> વાપરે છે. અને <cstdlib> નું rand() simulations અને games ચલાવે છે. ઉપરનું કોષ્ટક એક વાર શીખી લો અને કયું function કયું header માંગે છે એના સહેલા 2 marks ના પ્રશ્નો ગુમાવવાનું બંધ કરો.
Summary
Key takeaways
- Header file તૈયાર library નાં નામોનાં declarations પૂરાં પાડે છે; #include એમને compilation પહેલાં ચોંટાડી દે છે.
- #include એ preprocessor directive છે: # થી શરૂ થાય, અર્ધવિરામ ન જોઈએ.
- <> એ system directories માં શોધે છે; "" પહેલાં તમારા folder માં શોધે છે.
- આધુનિક જોડણી: <cstring>, <cmath>, <cstdlib> એ string.h, math.h, stdlib.h ની જગ્યા લે છે.
- એક header = એક કુટુંબ: I/O માટે iostream, strings માટે cstring, ગણિત માટે cmath, ગોઠવણી માટે iomanip, files માટે fstream.
- Memory hook: સૂચિમાંથી મંગાવો, ચા ઉગાડો નહીં.