C++ Unit5
C++ Unit5
Introduction
Many real-life problems handle large volumes of data and, in such situations, we need to use some devices
such as floppy disk or hard disk to store the data. The data is stored in these devices using the concept of
files. A file is a collection of related data stored in a particular area on the disk. Programs can be designed
to perform the read and write operations on these files.
Console-program-file interaction
Similarly, the following statement declares infile as an ifstream object and attaches it to the file data for
reading (input).
Ifstream infile("data"); // input only
Refer Program 11.1 from the textbook, page no. 329.
B. Opening Files Using open()
As stated earlier, the function open() can be used to open multiple files that use the same stream object.
For example, we may want to process a set of files sequentially. In such cases, we may create a single
stream object and use it to open each file in turn. This is done as follows:
file-stream-class stream-object;
stream-object.open ("filename");
‘Seek' functions seekg() and seekp() can also be used with two arguments as follows:
seekg(offset, refposition)
seekp(offset, refposition)
The parameter offset represents the number of bytes the file pointer is to be moved from the location
specified by the parameter refposition. The refposition takes one of the following three constants defined
in the ios class:
ios::beg Start of the file
ios::cur current position of the pointer
ios::end End of the file
The seekg() function moves the associated file’s ‘get’ pointer while the seek() function moves the associated
file's ‘put’ pointer.
What Is a Window?
Windows are central to Windows. They are so important that they named the operating system after them.
But what is a window? When you think of a window, you probably think of something like this:
This type of window is called an application window or main window. It typically has a frame with a title
bar, Minimize and Maximize buttons, and other standard UI elements. The frame is called the non-client
area of the window, so called because the operating system manages that portion of the window. The area
within the frame is the client area. This is the part of the window that your program manages.
Here is another type of window:
If you are new to Windows programming, it may surprise you that UI(User Interface) controls, such as
buttons and edit boxes, are themselves windows. The major difference between a UI control and an
application window is that a control does not exist by itself. Instead, the control is positioned relative to the
application window. When you drag the application window, the control moves with it, as you would expect.
Also, the control and the application window can communicate with each other. (For example, the
application window receives click notifications from a button.)
Therefore, when you think window, do not simply think application window. Instead, think of a window as
a programming construct that:
• Occupies a certain portion of the screen.
• May or may not be visible at a given moment.
• Knows how to draw itself.
• Responds to events from the user or the operating system.
• Parent Windows and Owner Windows
In the case of a UI control, the control window is said to be the child of the application window. The
application window is the parent of the control window. The parent window provides the coordinate system
used for positioning a child window. Having a parent window affects aspects of a window's appearance; for
example, a child window is clipped so that no part of the child window can appear outside the borders of
its parent window.
Another relationship is the relation between an application window and a modal dialog window. When an
application displays a modal dialog, the application window is the owner window, and the dialog is an
owned window. An owned window always appears in front of its owner window. It is hidden when the
owner is minimized, and is destroyed at the same time as the owner.
The following image shows an application that displays a dialog box with two buttons:
The application window owns the dialog window, and the dialog window is the parent of both button
windows. The following diagram shows these relations:
MFC
The Microsoft Foundation Class Library (MFC) is an "application framework" for programming in
Microsoft Windows. MFC provides much of the code, which are required for the following −
• Managing Windows.
• Menus and dialog boxes.
• Performing basic input/output.
• Storing collections of data objects, etc.
You can easily extend or override the basic functionality the MFC framework in you C++ applications by
adding your application-specific code into MFC framework.
MFC Framework
• The MFC framework provides a set of reusable classes designed to simplify Windows
programming.
• MFC provides classes for many basic objects, such as strings, files, and collections that are used in
everyday programming.
• It also provides classes for common Windows APIs and data structures, such as windows, controls,
and device contexts.
• The framework also provides a solid foundation for more advanced features, such as ActiveX and
document view processing.
• In addition, MFC provides an application framework, including the classes that make up the
application architecture hierarchy.
Why MFC?
• The MFC framework is a powerful approach that lets you build upon the work of expert
programmers for Windows. MFC framework has the following advantages.
• It shortens development time.
• It makes code more portable.
• It also provides tremendous support without reducing programming freedom and flexibility.
• It gives easy access to "hard to program" user-interface elements and technologies.
• MFC simplifies database programming through Data Access Objects (DAO) and Open Database
Connectivity (ODBC), and network programming through Windows Sockets.
MFC is loosely organized into several major categories:
The library's classes are presented here in the following categories:
• Root Class: CObject
• MFC Application Architecture Classes
• Application and Thread Support Classes
• Command Routing Classes
• Document Classes
• View Classes (Architecture)
• Frame Window Classes (Architecture)
• Document-Template Classes
• Window, Dialog, and Control Classes
• Frame Window Classes (Windows)
• View Classes (Windows)
• Dialog Box Classes
• Control Classes
• Control Bar Classes
• Drawing and Printing Classes
• Output (Device Context) Classes
• Drawing Tool Classes
• Simple Data Type Classes
• Array, List, and Map Classes
• Template Classes for Arrays, Lists, and Maps
• Ready-to-Use Array Classes
• Ready-to-Use List Classes
• Ready-to-Use Map Classes
• File and Database Classes
• File I/O Classes
• DAO Classes
• ODBC Classes
• OLE DB Classes
• Internet and Networking Classes
• Windows Sockets Classes
• Win32 Internet Classes
• OLE Classes
• OLE Container Classes
• OLE Server Classes
• OLE Drag-and-Drop and Data Transfer Classes
• OLE Common Dialog Classes
• OLE Automation Classes
• OLE Control Classes
• Active Document Classes
• OLE-Related Classes
• Debugging and Exception Classes
• Debugging Support Classes
• Exception Classes