290 likes | 505 Views
Multifile Program. Chapter 13. Binary Files. In binary files we do not need to format data File streams include two member functions specifically designed to input and output binary data sequentially Write A member function of ostream inherited by ofstream Read
E N D
Multifile Program Chapter 13
Binary Files • In binary files we do not need to format data • File streams include two member functions specifically designed to input and output binary data sequentially • Write • A member function of ostream inherited by ofstream • Read • A member function of istream that is inherited by ifstream write ( memory_block, size );read ( memory_block, size ); • Where memory_block is of type "pointer to char" (char*), and represents the address of an array of bytes where data elements are read and write • The size parameter is an integer value that specifies the number of characters to be read or written from/to the memory block.
#include <iostream> #include <fstream> ifstream::pos_type size; char* memblock; intmain () { ifstream file ("example.bin", ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); memblock = newchar [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close(); cout << "the complete file content is in memory"; delete[] memblock; } else cout << "Unable to open file"; return0; }
Class libraries • Libraries provide ready-made functions • A class library can take over a greater programming burden • Class developer vs programmer • Class library consists of various public declarations • Class library comes under header file with .H extension • Class library is used in source code with #include • The declarations are interface to the programmer • Implementation details (source code) are hidden from programmer. • Source code is distributed in OBJ files or library(.LIB) files
Organization and conceptualization • Large programs can be broken into multiple files • Many programmers are working with same project Theirs.h Mine.cpp Mine.h Compiler Theirs.obj Mine.obj Theirs.lib Linker Mine.exe
Cont’d • Header file can be included in source file like #include “theirs.h” • Quotes tell the compiler to look for the file in current directory • Each complier keeps its own library files in INCLUDE directory • A project contains all the files necessary for application
Interfile Communication - variables • A variable is declared by giving it a name and type • A variable is defined when it is given a place in memory • To access a variable in different files, it must be declared in every file //File A intglobalvar //File B globalvar = 3 // illegal, unknown to file B externintglobalvar // OK globalvar = 3; • Use extern keyword to declare it in other file. • extern causes globalvar in file A to be visible in file B • The linker will take care of connecting reference to a variable
Inter-file functions • Function declaration vs function definition? • Compiler only needs to know function name, its return type and the types of its arguments • To use function defined in one file we only need to declare the function in second file //File A int add(int a, int b){return a+b;} //File B int add(int,int); . . . int answer = add(3,2); //call to function • No need to use keyword extern
Inter-file classes • Definition of class does not set aside memory unless an object is declared • To access a class in multi files, it must be declared in each file in which its object will be used • The compiler needs to know the data type of everything its compiling like variables, functions • Class definition holds these thing
Header files • Common information needs for many files • Header file holds variable or function declarations • Definitions of functions and variable in header file will generate “multiply define” error • A class definition does not create multiply define error //FileH.h extern intglobar; intgloFunc(int); //FileA.cpp #include “fileH.h” intglovar; intgloFunc(int n){ return n; } //FileB.cpp #include “fileH.h” glovar= 5; IntglovarB = gloFunc(glovar);
Example //fileH.h class someClass{ Private: intmemvar: Public: intmemFunc(int,int); }; //fileA.cpp #include “fileH.h” IntsomeClass::memFunc(int n1, int n2){return n1+n2;} //fileB.cpp #include “fileH.h” someClassanObj; Int answer = anObj.memFunc(6,7);
Multiple Includes Hazard • Including the same header file twice in source file causes multiple-definition errors //file headtwo.h IntglobalVar; //file headone.h #include headtwo.h //file aap.cpp #include “headone.h” #include “headtwo.h” intglobalvar; // from headtwo.h via headone.h intglobalvar; // from headtwo.h • To avoid from such hazard use #if and #define directives #if !define(HEADCOM) #define HEADCOM …… …… #endif
Namespaces • Namespaces allow to group entities like classes, objects and functions under a name namespace identifier{entities} • Identifier is any name given to name space and entities are group of classes, functions, variables • Its useful when global object of function uses same variable. • Using keyword is used to introduce namespace in program
Example #include <iostream> namespace first { int x = 5; int y = 10; } namespacesecond { double x = 3.1416; double y = 2.7183; } int main () { usingnamespace first; cout << x << endl; cout << y << endl; cout << second::x << endl; cout << second::y << endl; return0; }
Template and Exceptions Chapter 14
Function template • Function templates are special functions that can operate with generic types • Function template can be adapted for more than one type of data instead of repeating entire code • In C++ this can be achieved using template parameters • A template parameter is use to pass type as function arguments like values are passed in simple function • The format for declaring function templates with type parameters is:template <class identifier> function_declaration;
Example #include <iostream> template<class T> T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); } intmain () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax<int>(i,j); n=GetMax<long>(l,m); cout << k << endl; cout << n << endl; return0; }
Cont’d • We can also define function templates that accept more than one type parameter template <class T, class U> T GetMin (T a, U b) { return (a<b?a:b); } • We can use this function as int i,j; longl; i = GetMin<int,long> (j,l); OR i = GetMin (j,l);
Class Template • Class templates are generally used for data storage(container) classes template <class T> classmypair { T values [2]; public: mypair (T first, T second) { values[0]=first; values[1]=second; } }; • The object of class template is declared as mypair<int> myobject (115, 36); Mypair<float> floatobject(12.3,35.4);
Example #include <iostream> template<class T> classmypair { T a, b; public: mypair (T first, T second) { a=first; b=second; } T getmax (); }; template <class T> T mypair<T>::getmax () { T retval; retval = a>b? a : b; returnretval; } intmain () { mypair <int> myobject (100, 75); cout << myobject.getmax(); return 0; }
Exceptions • Exceptions provide a way to react to runtime errors in a program by transferring control to special functions called handlers • To catch exceptions code is placed under exception inspection i.e. try block • When exception is occurred control is transferred to exception handlers otherwise code run normally • The exception handlers are declared with keyword catch
Cont’d #include <iostream> int main () { try{ throw20; } catch (int e) { cout<< "An exception occurred. Exception Nr. "; } return0; } • Throw expression accepts one parameter which is passed as an argument to the exception handler
Sequence of events • Code is executing normally outside a try block • Control enters the try block • A statement in the try block causes an error in a member function • The member function throws an exception • Control transfers to the exception handler (catch block) following the try block
Multiple catch() • Multiple catch statements can be included each have different parameter type • By using ellipsis (…) as parameter of catch the handler can catch any exception regardless of type try { // code here} catch (int param) { cout << "int exception"; } catch(char param) { cout << "char exception"; } catch(...) { cout << "default exception"; }