Unit 4 of 4 · BCA Sem 2

Unit 4: Polymorphism and file handling

Object Oriented Programming using C++ notes · PTU syllabus (UGSEC2503)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Polymorphism: early and late binding
  3. Function overloading
  4. Operator overloading
  5. Virtual functions and abstract classes
  6. File handling in C++
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

Polymorphism lets the same function or operator behave differently depending on the objects involved. This unit covers compile-time polymorphism (function and operator overloading), run-time polymorphism (virtual functions, pure virtual functions and abstract classes) and file handling in C++.

After this unit you can

  • Distinguish early and late binding
  • Overload functions and unary/binary operators following the rules
  • Use virtual and pure virtual functions and abstract classes
  • Open, read, write and close files using file streams

PTU syllabus topics

  • Early and late binding
  • function overloading
  • operator overloading (unary and binary) and its rules
  • virtual functions
  • pure virtual functions
  • abstract classes
  • opening/closing files
  • reading and writing a file
ComparisonOverloading vs overriding
Overloading
Overriding

Where

Same class

Base and derived class

Signature

Different parameters

Same signature

Resolved at

Compile time (early binding)

Run time with virtual functions (late binding)

Example

area(int) and area(float)

draw() redefined in Circle

1

Topic 1

Polymorphism: 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

Function overloading

Function overloading means several functions with the same name but different parameter lists (number or type of arguments). The compiler chooses the right one.

cppdouble area(double r)            { return 3.14159 * r * r; }   // circle
double area(double l, double b)  { return l * b; }             // rectangle
double area(double a, double b, double c) {                    // triangle (Heron)
    double s = (a + b + c) / 2;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}
3

Topic 3

Operator overloading

Operator overloading gives an operator a special meaning for user-defined types using an operator function: ReturnType operator+(Arg);

  • Unary operators (++, −−, −) as member functions take no argument.
  • Binary operators (+, −, ×) as member functions take one argument (the right operand).
cppclass Complex {
    float re, im;
public:
    Complex(float r = 0, float i = 0) { re = r; im = i; }
    Complex operator+(Complex c) { return Complex(re + c.re, im + c.im); }
};
Complex c3 = c1 + c2;   // calls c1.operator+(c2)

Rules: only existing operators can be overloaded; at least one operand must be a user-defined type; precedence and number of operands cannot change; the operators ., ::, ?:, sizeof and .* cannot be overloaded.

4

Topic 4

Virtual functions and abstract classes

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.

5

Topic 5

File handling in C++

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

Polymorphism
One interface, many implementations
Late binding
Linking a function call at run time
Operator overloading
Giving operators meaning for user-defined types
Virtual function
A base-class function overridden for run-time polymorphism
Abstract class
A class with a pure virtual function that cannot be instantiated

Quick revision

  • Compile-time: function and operator overloading; run-time: virtual functions.
  • Member unary operator: no argument; member binary operator: one argument.
  • Cannot overload . :: ?: sizeof .*
  • Pure virtual: = 0; abstract classes cannot have objects.
  • ofstream writes, ifstream reads, fstream does both.

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.Differentiate between early and late binding.
  2. Q2.What is function overloading?
  3. Q3.List four operators that cannot be overloaded.
  4. Q4.What is a virtual function?
  5. Q5.What is an abstract class?
  6. Q6.Name the file stream classes in C++.

Long-answer questions

  1. Q1.Explain operator overloading with programs for a unary and a binary operator.
  2. Q2.Explain virtual functions and run-time polymorphism with an example.
  3. Q3.What are pure virtual functions and abstract classes? Explain with a program.
  4. Q4.Explain file handling in C++ and write a program to copy one file into another.

Stuck on this unit?

Message SBS on WhatsApp for help with Object Oriented Programming using C++, or to ask about studying BCA at Synetic.

WhatsApp us