Skip to content

ACtE0305 Pure virtual function and file handling

Virtual function and dynamic binding

A virtual function enables runtime dispatch through a base-class interface.

class Base {
public:
    virtual void show();
};

Key fact:

  • when a call is made through a base-class pointer or reference to a virtual function, the override chosen depends on the dynamic type of the object.

This is dynamic binding.

Without virtual, ordinary member calls are statically bound based on the static type used in the call expression.

Pure virtual function and abstract class

Pure virtual syntax:

virtual void draw() = 0;

Recognition facts:

  • a class with at least one pure virtual function is abstract;
  • abstract classes cannot be instantiated directly;
  • derived classes must provide concrete overrides unless they remain abstract.

Stream class hierarchy

Core hierarchy cues:

Class family Role
istream input stream
ostream output stream
iostream input and output
ifstream file input
ofstream file output
fstream file input and output

Console objects:

  • cin is standard input stream object;
  • cout is standard output stream object;
  • cerr is unbuffered standard error output;
  • clog is buffered error/log stream.

File opening, closing, and modes in C++

Mode flag Meaning
ios::in open for input
ios::out open for output
ios::app append on each write
ios::ate seek to end immediately after open
ios::binary binary mode
ios::trunc truncate existing file on open

Opening styles:

ifstream fin("data.txt");
ofstream fout;
fout.open("out.txt", ios::out | ios::app);

Always close explicitly when needed or rely on RAII at scope exit.

Input/output operations and stream states

Check Meaning
good() no error flags set
eof() end-of-file flag set
fail() logical/format or open failure
bad() serious stream error

File and console operations:

  • formatted extraction/insertion: >>, <<;
  • unformatted input: get, getline, read;
  • unformatted output: put, write.

Trap: formatted >> for strings stops at whitespace, whereas getline reads an entire line.

ios flags and manipulators

Formatting tools:

Tool Use
setw(n) width
setprecision(n) floating precision
setfill(ch) fill character
fixed fixed-point float format
scientific scientific notation
hex, oct, dec integer base
left, right justification
endl newline plus flush

Manipulators change stream formatting state or perform immediate output effects.

File-handling traps

  • ios::app always writes at end; ios::ate merely starts at end and can still seek elsewhere.
  • eof() should not be used as a pre-read loop condition.
  • Dynamic binding requires a virtual function call through a base interface, not just inheritance alone.

Virtual-file-handling revision box

  • virtual enables runtime polymorphic dispatch.
  • Pure virtual function makes a class abstract.
  • istream, ostream, iostream are the core stream families.
  • ifstream, ofstream, fstream are file-stream specializations.
  • >> is formatted; getline and read are unformatted-style operations.
  • ios::app and ios::ate are not the same mode.