Вы находитесь на странице: 1из 6

Q91. What is abstract class? A class that contains at least one pure virtual function is considered an abstract class.

Classes derived from the abstract class must implement the pure virtual function or they, too, are abstract classes. A virtual function is declared as "pure" by using the pure-specifier syntax. The intent of class Account is to provide general functionality, but objects of type Account are too general to be useful. Therefore, Account is a good candidate for an abstract class: // deriv_AbstractClasses.cpp // compile with: /LD class Account { public: Account( double d ); // Constructor. virtual double GetBalance(); // Obtain balance. virtual void PrintBalance() = 0; // Pure virtual function. private: double _balance; }; The only difference between this declaration and the previous one is that PrintBalance is declared with the pure specifier (= 0). Q92. What is a stream? In C++, I/O devices are treated as files; files are treated as a sequence or a stream of bytes. In other words, a stream in C++ means a flow of bytes from an input device to the CPU(input stream) and from the CPU to an output device(output stream). Streams used for input and output are Stream Description Meaning cin console input standard input stream cout console output standard output stream Q93. What is stream class and stream object. Describe them in detail with an example. The C++ I/O system supplies an interface to the programmer that is independent of the actual device being accessed. This interface is known as stream. The I/O system contains a hierarchy of the stream classes used to define various streams to deal with the console and disk files. These classes are known as stream classes. The base stream class for handling input and output operations with the console and disk is ios and there are various classes derived from it such as istream, ostream, streambuf etc. The header file <iostream> declares the stream classes. Q94. Explain stream class hierarchy. This chart shows the heirarchical class-subclass relationships between the C++ I/O stream classes. Declarations for these classes are found in header files fstream.h, iostream.h, and strstrea.h, either directly or indirectly.

Q95. What are the three stream classes that are commonly used for disk I/O? C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files. These classes are derived directly or indirectly from the classes istream, and ostream. We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream.

Q96. Describe read() and write() functions. The function write() and read() handle the data in binary form. This means that the values are stored in the disk file in the same format in which they are stored in the binary and character format . The binary format is more accurate for storing the numbers as they are stored in the exact internal representation. There are no conversions while saving data and therefore saving is much faster. Q97. Differentiate between function read and write. write() function writes data in file in binary form and read() function reads blocks of binary data from file. example: file.read((char *)&obj, sizeof(obj)); file.write((char *)&obj, sizeof(obj)); Q97(a). What is the difference between text mode and binary mode while performing file I/O operations? The file stream classes support anumber of member functions for performing the input and output operations of files. One pair of functions i.e. put() and get() are designed for handling a single character at a time. Another pair of functions, write() and read() are designed to write and read blocks of binary data.

An int takes two bytes to store its values in the binary form, irrespective of its size. But a four digit int will take four bytes to store it in the character form. Example: For the integer value 2594: Binary formate 0000101000100010 1 byte Character formate: 2594 1byte

1byte 1byte 1byte 1byte

Q97(b). Describe put( ) and get() functions. The function put() write a single character to the associated stream. Similarly, the function get() reads a single character from the associated stream.

Q98. What is a Manipulator? What is the difference between manipulator and setf() function? Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer's choice of display. There are numerous manipulators available in C++. Some of the more commonly used manipulators are provided here below: endl Manipulator: This manipulator has the same functionality as the 'n' newline character. For example: Sample Code 1. cout << "Exforsys" << endl; 2. cout << "Training"; produces the output: Exforsys Training setw Manipulator: This manipulator sets the minimum field width on output. The syntax is: setw(x) Here setw causes the number or string that follows it to be printed within a field of x characters wide and x is the argument set in setw manipulator. The header file that must be included while using setw manipulator is . Sample Code

#include <iostream> using namespace std; #include <iomanip>

void main( ) { int x1=12345,x2= 23456, x3=7892; cout << setw(8) << "Exforsys" << setw(20) << "Values" << endl << setw(8) << "E1234567" << setw(20)<< x1 << endl << setw(8) << "S1234567" << setw(20)<< x2 << endl << setw(8) << "A1234567" << setw(20)<< x3 << endl; } The output of the above example is: The difference between the manipulator and setf( ) function are as follows: 1. The setf( ) function is used to set the flags of the ios but manipulators directly inserts the formatting instructions into the stream. 2. We can create user-defined manipulators but setf( ) function uses data members of ios class only. The flags put on through the setf( ) function can be put off through unsetf( ) function. Such flexibility is not available with manipulators. Q99. What is manipulator in C++? Why do we need manipulators? Explain the advantage of manipulator? Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for example: cout << boolalpha;

Manipulators are used to change formatting parameters on streams and to insert or extract certain special characters.

Q100. What are different types of file opening mode? A file can be open in two ways. A) Using the constructor function of the class. B) Using the member function open() of the class. Opening files using constructor A constructor is used to initialize object while it is being created. A filename is used to initialize the file stream object. This involves the following steps: 1. Create a file stream object to manage the stream using the appropriate class. The class ofstream is used to create the output stream and the class ifstream to create the input stream. Initialized the file object with the desired file.

2.

For example, the following statement opens a file named results for output : ofstream outfile(results); The following statement declares infile as an ifstream object and attaches it to the file data for reading (input): ifstream infile(data);

Opening files using open() The function open()can be used to open multiple files that use the same stream object. We may want to process of 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, mode);

File mode parameter ios::app ios::ate ios::binary

Meaning

Append to end of file go to end of file on opening file open in binary mode

ios::in open file for reading only ios::out open file for writing only

ios::nocreate open fails if the file does not exist ios::noreplace open fails if the file already exist

ios::trunc

delete the contents of the file if it exist

All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open():

fstream file; file.open ("example.bin", ios::out | ios::app | ios::binary);

Вам также может понравиться