Unit 4: Polymorphism and file handling
OOPs using C++ notes · PTU syllabus (BSIT203/BSBC203)
On this page
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
Decided at
Compile time
Run time
Achieved by
Function and operator overloading
Virtual functions
Speed
Faster
Slightly slower
Flexibility
Less
More
Topic 1
Early and late binding
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.
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 virtualA 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.
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.
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.
Topic 4
File input and output
C++ uses stream classes from <fstream>: ofstream (write), ifstream (read) and fstream (both).
- 1Include fstream
- 2Create a stream object
ofstream fout;
- 3Open
fout.open("data.txt"); or ofstream fout("data.txt");
- 4Read or write
fout << name; fin >> name; getline(fin, line);
- 5Close
fout.close();
| Mode | Meaning |
|---|---|
| ios::in | Open for reading |
| ios::out | Open for writing |
| ios::app | Append at the end |
| ios::binary | Binary 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
- Q1.Distinguish early and late binding.
- Q2.What is a pure virtual function?
- Q3.What is an abstract class?
- Q4.What is a buffer?
- Q5.Name the classes used for file handling.
- Q6.State three file opening modes in C++.
Long-answer questions
- Q1.Explain early and late binding with examples.
- Q2.Explain virtual and pure virtual functions and abstract classes.
- Q3.Explain streams and the iostream class hierarchy.
- 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.
