3.5 Pure Virtual Functions and File Handling
1. Pure Virtual Functions
virtual return_type function_name() = 0;
#include <iostream>
using namespace std;
// Abstract base class
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle" << endl;
}
};
class Rectangle : public Shape {
public:
void draw() override {
cout << "Drawing a Rectangle" << endl;
}
};
int main() {
Shape* shape1 = new Circle();
Shape* shape2 = new Rectangle();
shape1->draw(); // Output: Drawing a Circle (Dynamic Binding)
shape2->draw(); // Output: Drawing a Rectangle (Dynamic Binding)
delete shape1;
delete shape2;
return 0;
}2. Dynamic Binding
3. File Input/Output Operations
4. Stream Class Hierarchy
Conclusion
Previous3.4 Features of Object-Oriented ProgrammingNext3.6 Generic Programming and Exception Handling
Last updated