Unit 4 of 4 · B.Sc IT Sem 2

Unit 4: Polymorphism and file handling

OOPs using C++ notes · PTU syllabus (BSIT203/BSBC203)

3 min read4 topics10 exam questions
On this page
  1. Unit summary
  2. Early and late binding
  3. Virtual and pure virtual functions
  4. Streams, buffers and iostreams
  5. File input and output
  6. Key terms
  7. Quick revision
  8. Important questions

Unit summary

Polymorphism lets one interface work with many types, and streams connect programs to files. This unit covers early and late binding, virtual and pure virtual functions, streams, buffers and iostreams, header files, and file input and output.

After this unit you can

  • Distinguish early and late binding
  • Use virtual and pure virtual functions and abstract classes
  • Explain streams, buffers and iostream classes
  • Perform file input and output

PTU syllabus topics

  • Early and late binding
  • virtual functions and pure virtual functions
  • streams/buffers/iostreams
  • header files
  • file input and output
ComparisonEarly vs late binding
Early (static) binding
Late (dynamic) binding

Decided at

Compile time

Run time

Achieved by

Function and operator overloading

Virtual functions

Speed

Faster

Slightly slower

Flexibility

Less

More

1

Topic 1

Early and late binding

ClassificationTypes of polymorphism
Polymorphism
  • Compile-time (early binding)

    Function overloading, operator overloading

  • Run-time (late binding)

    Virtual functions through base-class pointers

  • Early (static) binding: the function call is linked to its definition at compile time.
  • Late (dynamic) binding: the call is linked at run time, based on the actual object pointed to.
2

Topic 2

Virtual and pure virtual functions

A virtual function is declared with virtual in the base class; when called through a base-class pointer or reference, the derived class's version runs (late binding).

cppclass Shape { public: virtual void draw() { cout << "Shape"; } };
class Circle : public Shape { public: void draw() { cout << "Circle"; } };
Shape *s = new Circle;
s->draw();      // prints "Circle" because draw() is virtual

A pure virtual function has no body: virtual void draw() = 0;. A class with at least one pure virtual function is an abstract class; it cannot be instantiated and serves as an interface that derived classes must complete.

3

Topic 3

Streams, buffers and iostreams

  • Stream: a sequence of bytes flowing between a program and a device — input streams (cin) and output streams (cout, cerr, clog).
  • Buffer: temporary memory holding data before it is written or after it is read — flushed with endl or flush.
ClassificationStream class hierarchy
ios
  • istream

    cin, extraction >>

  • ostream

    cout, insertion <<

  • iostream

    Both input and output

  • ifstream, ofstream, fstream

    File streams

  • Header files: iostream (console I/O), fstream (files), iomanip (setw, setprecision), string, cmath.
4

Topic 4

File input and output

C++ uses stream classes from <fstream>: ofstream (write), ifstream (read) and fstream (both).

ProcessUsing a file stream
  1. 1Include fstream
  2. 2Create a stream object

    ofstream fout;

  3. 3Open

    fout.open("data.txt"); or ofstream fout("data.txt");

  4. 4Read or write

    fout << name; fin >> name; getline(fin, line);

  5. 5Close

    fout.close();

ModeMeaning
ios::inOpen for reading
ios::outOpen for writing
ios::appAppend at the end
ios::binaryBinary mode

Example

Copying a file: open the source with ifstream and the target with ofstream, read character by character with fin.get(ch) and write with fout.put(ch) until the end of file.

Key terms

Early binding
Function call resolved at compile time
Late binding
Function call resolved at run time through virtual functions
Pure virtual function
Virtual function with no body (= 0)
Stream
Flow of bytes between program and device
fstream
Class for reading and writing files

Quick revision

  • Compile-time vs run-time polymorphism.
  • virtual functions, vtable idea; pure virtual and abstract classes.
  • Streams, buffers; ios, istream, ostream, iostream; manipulators.
  • ifstream, ofstream, fstream; open modes; reading and writing.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Distinguish early and late binding.
  2. Q2.What is a pure virtual function?
  3. Q3.What is an abstract class?
  4. Q4.What is a buffer?
  5. Q5.Name the classes used for file handling.
  6. Q6.State three file opening modes in C++.

Long-answer questions

  1. Q1.Explain early and late binding with examples.
  2. Q2.Explain virtual and pure virtual functions and abstract classes.
  3. Q3.Explain streams and the iostream class hierarchy.
  4. Q4.Write a program to write and read data using files in C++.

Stuck on this unit?

Message SBS on WhatsApp for help with OOPs using C++, or to ask about studying B.Sc IT at Synetic.

WhatsApp us