Download presentation
Presentation is loading. Please wait.
Published byJoan Reeves Modified over 9 years ago
1
Chapter 13
2
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.
3
#include ifstream::pos_type size; char * memblock; int main () { ifstream file ("example.bin", ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [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"; return 0; }
4
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
5
Organization and conceptualization Large programs can be broken into multiple files Many programmers are working with same project Theirs.h Theirs.objMine.obj Mine.cppMine.h Theirs.lib Mine.exe Compiler Linker
6
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
7
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 int globalvar //File B globalvar = 3 // illegal, unknown to file B extern int globalvar // 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
8
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
9
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
10
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 int globar; int gloFunc(int); //FileA.cpp #include “fileH.h” int glovar; int gloFunc(int n){ return n; } //FileB.cpp #include “fileH.h” glovar= 5; Int glovarB = gloFunc(glovar);
11
Example //fileH.h class someClass{ Private: int memvar: Public: int memFunc(int,int); }; //fileA.cpp #include “fileH.h” Int someClass::memFunc(int n1, int n2){return n1+n2;} //fileB.cpp #include “fileH.h” someClass anObj; Int answer = anObj.memFunc(6,7);
12
Multiple Includes Hazard Including the same header file twice in source file causes multiple-definition errors //file headtwo.h Int globalVar; //file headone.h #include headtwo.h //file aap.cpp #include “headone.h” #include “headtwo.h” int globalvar; // from headtwo.h via headone.h int globalvar; // from headtwo.h To avoid from such hazard use #if and #define directives #if !define(HEADCOM) #define HEADCOM …… #endif
13
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
14
Example #include namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using namespace first; cout << x << endl; cout << y << endl; cout << second::x << endl; cout << second::y << endl; return 0; }
15
Chapter 14
16
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 function_declaration;
17
Example #include template T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); } int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax (i,j); n=GetMax (l,m); cout << k << endl; cout << n << endl; return 0; }
18
Cont’d We can also define function templates that accept more than one type parameter template T GetMin (T a, U b) { return (a<b?a:b); } We can use this function as int i,j; long l; i = GetMin (j,l); OR i = GetMin (j,l);
19
Class Template Class templates are generally used for data storage(container) classes template class mypair { T values [2]; public: mypair (T first, T second) { values[0]=first; values[1]=second; } }; The object of class template is declared as mypair myobject (115, 36); Mypair floatobject(12.3,35.4);
20
Example #include template class mypair { T a, b; public: mypair (T first, T second) { a=first; b=second; } T getmax (); }; template T mypair ::getmax () { T retval; retval = a>b? a : b; return retval; } int main () { mypair myobject (100, 75); cout << myobject.getmax(); return 0; }
21
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
22
Cont’d #include int main () { try { throw 20; } catch (int e) { cout << "An exception occurred. Exception Nr. "; } return 0; } Throw expression accepts one parameter which is passed as an argument to the exception handler
23
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
24
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"; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.