C++91-100

download C++91-100

of 6

Transcript of C++91-100

  • 7/30/2019 C++91-100

    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-specifiersyntax. The intent ofclass Account is to provide general functionality, but objects of type Account are toogeneral to be useful. Therefore, Account is a good candidate for an abstract class:

    // deriv_AbstractClasses.cpp// compile with: /LDclass 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 isdeclared 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 streamcout console output standard output

    stream

    Q93. What is stream class and stream object. Describe them in detail with anexample.

    The C++ I/O system supplies an interface to the programmer that is independent of theactual device being accessed. This interface is known as stream. The I/O system containsa hierarchy of the stream classes used to define various streams to deal with the consoleand disk files. These classes are known as stream classes. The base stream class forhandling 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 declares the stream classes.

    Q94. Explain stream class hierarchy.

    This chart shows the heirarchical class-subclass relationships between the C++ I/Ostream classes. Declarations for these classes are found in header files fstream.h,iostream.h, and strstrea.h, either directly or indirectly.

  • 7/30/2019 C++91-100

    2/6

    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/fromfiles:

    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. Wehave already used

    objects whose types were these classes: cin is an object of class istream and cout is anobject of class ostream.

    Q96. Describe read() and write() functions.The function write() and read() handle the data in binary form. This means that thevalues are stored in the disk file in the same format in which they are stored in thebinary and character format . The binary format is more accurate for storing the numbersas they are stored in the exact internal representation. There are no conversions whilesaving 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 whileperforming file I/O operations?

    The file stream classes support anumber of member functions for performing the inputand output operations of files. One pair of functions i.e. put() and get() are designed forhandling a single character at a time. Another pair of functions, write() and read() aredesigned to write and read blocks of binary data.

  • 7/30/2019 C++91-100

    3/6

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

    1 byte 1byteCharacter formate: 2594

    1byte 1byte 1byte 1byte

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

    Q98. What is a Manipulator? What is the difference between manipulator andsetf() function?

    Manipulators are operators used in C++ for formatting output. The data is manipulatedby the programmer's choice of display.

    There are numerous manipulators available in C++. Some of the more commonly usedmanipulators are provided here below:

    endl Manipulator:

    This manipulator has the same functionality as the 'n' newline character.

    For example:

    Sample Code

    1. cout

  • 7/30/2019 C++91-100

    4/6

    #include

    using namespace std;

    #include

    void main( )

    {

    int x1=12345,x2= 23456, x3=7892;

    cout

  • 7/30/2019 C++91-100

    5/6

    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) Usingthe 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 toinitialize the file stream object. This involves the following steps:

    1. Create a file stream object to manage the stream using the appropriate class. Theclass ofstream is used to create the output stream and the class ifstream to createthe input stream.

    2. Initialized the file object with the desired file.

    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 filedata 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 asingle 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 Meaning

    ios::app Append to end of file

    ios::ate go to end of file on opening

    ios::binary 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

  • 7/30/2019 C++91-100

    6/6

    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 wewant to open the file example.bin in binary mode to add data we could do it by thefollowing call to member function open():

    fstream file;

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