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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Functions and scope Confidence In scope Reference: K&K textbook, Chapter 4.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
True or false A variable of type char can hold the value 301. ( F )
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Guide To UNIX Using Linux Third Edition
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
1 CSC241: Object Oriented Programming Lecture No 27.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
1 Chapter 9 Scope, Lifetime, and More on Functions.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Exception Handling How to handle the runtime errors.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
C++ Lesson 1.
Hightlights C++ Template C++ Number Systems.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1.2 Introduction to C++ Programming
Exceptions, Templates, and the Standard Template Library (STL)
Chapter 2 part #1 C++ Program Structure
Student Book An Introduction
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Topics Introduction to File Input and Output
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Chapter 3: Input/Output
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Topics Introduction to File Input and Output
SPL – PS1 Introduction to C++.
Functions Chapter No. 5.
Presentation transcript:

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 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; }

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 Theirs.objMine.obj Mine.cppMine.h Theirs.lib Mine.exe Compiler Linker

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 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

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 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);

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);

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

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 namespace first { int x = 5; int y = 10; } namespace second { double x = ; double y = ; } int main () { using namespace first; cout << x << endl; cout << y << endl; cout << second::x << endl; cout << second::y << endl; return 0; }

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 function_declaration;

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; }

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);

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);

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; }

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 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

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"; }