Unit 4: Polymorphism and file handling
Object Oriented Programming using C++ notes · PTU syllabus (UGSEC2503)
On this page
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
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
Topic 1
Polymorphism: 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
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));
}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.
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 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 5
File handling in C++
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
- 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
- Q1.Differentiate between early and late binding.
- Q2.What is function overloading?
- Q3.List four operators that cannot be overloaded.
- Q4.What is a virtual function?
- Q5.What is an abstract class?
- Q6.Name the file stream classes in C++.
Long-answer questions
- Q1.Explain operator overloading with programs for a unary and a binary operator.
- Q2.Explain virtual functions and run-time polymorphism with an example.
- Q3.What are pure virtual functions and abstract classes? Explain with a program.
- 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.
