0% found this document useful (0 votes)
3 views29 pages

Module 5

This document outlines Module V of the ES203 course on Object Oriented Programming using C++, focusing on Strings, Files, and Exception Handling. It covers topics such as manipulating strings in C++ (C-style vs std::string), file handling techniques, formatted and unformatted I/O operations, and exception handling mechanisms. The module includes practical examples and code snippets to illustrate these concepts effectively.

Uploaded by

pbcg.common
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views29 pages

Module 5

This document outlines Module V of the ES203 course on Object Oriented Programming using C++, focusing on Strings, Files, and Exception Handling. It covers topics such as manipulating strings in C++ (C-style vs std::string), file handling techniques, formatted and unformatted I/O operations, and exception handling mechanisms. The module includes practical examples and code snippets to illustrate these concepts effectively.

Uploaded by

pbcg.common
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module V: Strings, Files and Exception Handling

ES203 – Object Oriented Programming Using C++

Dr. Sreemana Datta


Asst. Professor
Department of Computer Science & Engineering
Amity School of Engineering & Technology
Amity University, Jharkhand

February 19, 2026

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 1 / 29


Module V — Roadmap

1 Manipulating Strings

2 Streams & File Handling

3 Formatted & Unformatted I/O

4 Exception Handling

5 Generic Programming — Templates

6 Standard Template Library (STL)

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 2 / 29


Strings in C++ — Two Flavors
1. C-Style Strings (character arrays) 2. C++ std::string (the modern way)
Array of char ending with ’\0’ A class from <string>
Inherited from C Automatic memory management
Manual memory management Rich set of member functions
Use functions from <cstring> Safe and convenient
Error-prone (buffer overflow!) Preferred in C++!
char name[20] = "Hello"; string name = "Hello";

Analogy
C-strings are like riding a bicycle with no brakes — fast, but risky!
std::string is like riding with full safety gear — comfortable, safe, and almost as fast!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 3 / 29


C-Style String Operations (<cstring>)
# include < iostream >
# include < cstring > // For C - style string functions
using namespace std ;

int main () {
char str1 [50] = " Hello " ;
char str2 [50] = " World " ;
char str3 [50];

// strlen : length ( does NOT count ’\0 ’)


cout « " Length of str1 : " « strlen ( str1 ) « endl ; // 5

// strcpy : copy str1 into str3


strcpy ( str3 , str1 ) ;
cout « " str3 after copy : " « str3 « endl ; // Hello

// strcat : concatenate str2 to str1


strcat ( str1 , " " ) ;
strcat ( str1 , str2 ) ;
cout « " str1 after concat : " « str1 « endl ; // Hello World

// strcmp : compare (0 = equal , <0 = first is smaller , >0 = first is larger )


cout « " Compare Hello vs World : " « strcmp ( " Hello " , " World " ) « endl ;

// strstr : find substring


char * found = strstr ( str1 , " World " ) ;
if ( found ) cout « " Found at position : " « ( found - str1 ) « endl ; // 6

return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 4 / 29


std::string — The C++ Way (Much Better!)
# include < iostream >
# include < string >
using namespace std ;

int main () {
string s1 = " Hello " ;
string s2 = " World " ;

// Concatenation with + operator ( overloaded !)


string s3 = s1 + " " + s2 ;
cout « s3 « endl ; // Hello World

// Length
cout « " Length : " « s3 . length () « endl ; // 11

// Access characters
cout « " First char : " « s3 [0] « endl ; // H
cout « " At index 6: " « s3 . at (6) « endl ; // W

// Substring
cout « " Substr : " « s3 . substr (6 , 5) « endl ; // World

// Find
size_t pos = s3 . find ( " World " ) ;
cout « " Found at : " « pos « endl ; // 6

// Replace
s3 . replace (0 , 5 , " Hi " ) ;
cout « " After replace : " « s3 « endl ; // Hi World

// Compare
if ( s1 == " Hello " ) cout « " Equal ! " « endl ; // Overloaded ==

// Insert and Erase


s3 . insert (2 , " ," ) ; // Hi , World
s3 . erase (0 , 3) ; // World
cout « " Final : " « s3 « endl ;
return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 5 / 29


std::string Useful Methods — Quick Reference
Method What it does Example
length() / size() Returns length [Link]()
empty() Checks if empty if ([Link]())
append(str) Appends string [Link](" World")
substr(pos, len) Extracts substring [Link](0, 5)
find(str) Finds first occurrence [Link]("lo")
rfind(str) Finds last occurrence [Link]("l")
replace(pos, len, str) Replaces portion [Link](0, 2, "Hi")
insert(pos, str) Inserts at position [Link](5, "!")
erase(pos, len) Erases portion [Link](0, 3)
compare(str) Compares strings [Link]("test")
c_str() Returns C-string s.c_str()
clear() Empties the string [Link]()

Use getline(cin, str) to read a full line (including spaces). Plain cin » str stops at whitespace!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 6 / 29


What are Streams?
A stream is a sequence of bytes flowing between a program and an I/O C++ Stream Classes:
device.
ios

Analogy: Water Pipeline


istream ostream
Think of a stream as a water pipe:
Input stream: Water flows into your house (data comes in)
ifstream ofstream
Output stream: Water flows out from your house (data goes out)
The pipe doesn’t care if the water comes from a lake (keyboard) or a tank (file) —
fstream
it just flows!
cin = object of istream
cout = object of ostream

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 7 / 29


File Handling — Reading and Writing Files
# include < iostream >
# include < fstream > // For file streams
# include < string >
using namespace std ;

int main () {
// ===== WRITING TO A FILE =====
ofstream outFile ( " students . txt " ) ; // Open for writing
if (! outFile ) {
cerr « " Error opening file ! " « endl ;
return 1;
}
outFile « " Amit 101 85.5 " « endl ;
outFile « " Priya 102 92.0 " « endl ;
outFile « " Rahul 103 78.3 " « endl ;
outFile . close () ;
cout « " Data written successfully ! " « endl ;

// ===== READING FROM A FILE =====


ifstream inFile ( " students . txt " ) ; // Open for reading
string name ;
int roll ;
float marks ;
cout « " \n - - - Student Records ---" « endl ;
while ( inFile » name » roll » marks ) {
cout « name « " ( Roll : " « roll
« " , Marks : " « marks « " ) " « endl ;
}
inFile . close () ;
return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 8 / 29


File Open Modes
Mode Meaning Effect
ios::in Input (read) Opens for reading
ios::out Output (write) Opens for writing (creates/overwrites)
ios::app Append Writes at end, doesn’t erase
ios::ate At end Opens and moves to end
ios::trunc Truncate Erases content if file exists
ios::binary Binary mode Read/write in binary format

Combining modes: Use the | (bitwise OR) operator:


fstream f("[Link]", ios::in | ios::out | ios::app);

Default Modes
ifstream: defaults to ios::in
ofstream: defaults to ios::out | ios::trunc (creates or overwrites!)
Use ios::app if you want to add to an existing file!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 9 / 29


Reading Files: Line by Line with getline
# include < iostream >
# include < fstream >
# include < string >
using namespace std ;

int main () {
// Write a multi - line file first
ofstream out ( " poem . txt " ) ;
out « " Roses are red , " « endl ;
out « " Violets are blue , " « endl ;
out « " C ++ is powerful , " « endl ;
out « " And so are you ! " « endl ;
out . close () ;

// Read line by line


ifstream in ( " poem . txt " ) ;
string line ;
int lineNum = 1;

while ( getline ( in , line ) ) { // getline reads entire line !


cout « lineNum « " : " « line « endl ;
lineNum ++;
}
in . close () ;

// Count words in a file ( practical application !)


ifstream wordFile ( " poem . txt " ) ;
string word ;
int wordCount = 0;
while ( wordFile » word ) { // » reads word by word
wordCount ++;
}
cout « " \ nTotal words : " « wordCount « endl ;
wordFile . close () ;

return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 10 / 29


Formatted I/O — Pretty Printing
Formatted I/O uses manipulators from <iomanip> to control output appearance.
# include < iostream >
# include < iomanip > // For setw , setprecision , fixed , etc .
using namespace std ;

int main () {
double pi = 3 .1 4 1 5 9 2 6 53 5 89 7 93 ;

// setprecision : control decimal places


cout « fixed « setprecision (2) « " Pi : " « pi « endl ; // Pi : 3.14

// setw : set field width ( right - aligned by default )


cout « setw (10) « " Name " « setw (10) « " Marks " « endl ;
cout « setw (10) « " Amit " « setw (10) « 85 « endl ;
cout « setw (10) « " Priya " « setw (10) « 92 « endl ;

// left / right alignment


cout « left « setw (15) « " Left - aligned " « " | " « endl ;
cout « right « setw (15) « " Right - aligned " « " | " « endl ;

// setfill : fill empty space with character


cout « setfill ( ’* ’) « setw (20) « " Stars " « endl ; // * ** * ** ** * ** ** * * Stars

// showpos : show + sign for positive numbers


cout « showpos « 42 « endl ; // +42

// hex , oct , dec : number base


cout « noshowpos ;
cout « " Decimal : " « dec « 255 « endl ; // 255
cout « " Hex : " « hex « 255 « endl ; // ff
cout « " Octal : " « oct « 255 « endl ; // 377

return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 11 / 29


Unformatted I/O — Raw Character Operations
Unformatted I/O reads/writes raw characters without any conversion or formatting.
# include < iostream >
using namespace std ;

int main () {
char ch ;
char buffer [100];

// get () : reads a single character ( including whitespace )


cout « " Enter a character : " ;
cin . get ( ch ) ;
cout « " You entered : " « ch « endl ;

cin . ignore (100 , ’\ n ’) ; // Clear remaining input buffer

// getline () : reads a full line


cout « " Enter a sentence : " ;
cin . getline ( buffer , 100) ; // C - style : reads into char array
cout « " Sentence : " « buffer « endl ;

// put () : writes a single character


cout . put ( ’A ’) ;
cout . put ( ’\ n ’) ;

// write () : writes a block of characters


char msg [] = " Hello , C ++! " ;
cout . write ( msg , 7) ; // Writes first 7 characters : " Hello , "
cout « endl ;

// peek () : looks at next character WITHOUT removing it


// putback () : puts a character back into the stream
return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 12 / 29


Formatted vs Unformatted I/O — Summary
Formatted I/O Unformatted I/O
Uses « and » operators Uses get(), put(), read(),
write()
Data is converted (e.g., int → text) Data is raw bytes, no conversion
Human-readable output Binary/raw output
Uses manipulators for control No manipulators
Slower (conversion overhead) Faster (no conversion)
Example: cout « 42; Example: [Link](’A’);

When to use which?


Formatted: When displaying to humans (console output, reports, logs).
Unformatted: When working with raw data (binary files, network protocols, low-level I/O).

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 13 / 29


Exception Handling — Graceful Error Management
An exception is an unexpected event that disrupts normal program flow C++ Exception Handling Keywords:
(e.g., divide by zero, file not found, out of memory).
try — Block of code to monitor
Analogy: Fire Safety Plan throw — Signal an exception
A building has a normal daily routine (normal code execution). But there’s also a catch — Handle the exception
fire escape plan (exception handling):
try = the building (normal operations) Without exceptions:
throw = fire alarm (something went wrong!) Use error codes (messy)
catch = fire exit (handle the emergency) Use if checks everywhere
Without a plan, the building burns down (program crashes)! Easy to forget checking
With exceptions:
Clean separation of normal and error code
Cannot be silently ignored

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 14 / 29


Exception Handling — Basic Syntax
# include < iostream >
using namespace std ;

double divide ( double a , double b ) {


if ( b == 0) {
throw " Division by zero ! " ; // THROW an exception ( a C - string here )
}
return a / b ;
}

int main () {
try { // TRY block : monitor this code
cout « divide (10 , 3) « endl ; // 3.33333 ( OK )
cout « divide (10 , 0) « endl ; // This throws !
cout « " This never executes " « endl ; // Skipped !
}
catch ( const char * msg ) { // CATCH block : handle the error
cerr « " Error : " « msg « endl ;
}

cout « " Program continues normally after catch ! " « endl ;


return 0;
}
// Output :
// 3.33333
// Error : Division by zero !
// Program continues normally after catch !

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 15 / 29


Multiple Catch Blocks & Standard Exceptions
# include < iostream >
# include < stdexcept > // For standard exception classes
using namespace std ;

int processAge ( int age ) {


if ( age < 0)
throw invalid _a rg um en t ( " Age cannot be negative ! " ) ;
if ( age > 150)
throw out_of_range ( " Age seems unrealistic ! " ) ;
return age ;
}

int main () {
try {
processAge ( -5) ;
}
catch ( const in va lid _a rg um en t & e ) {
cerr « " Invalid : " « e . what () « endl ;
}
catch ( const out_of_range & e ) {
cerr « " Range : " « e . what () « endl ;
}
catch (...) { // Catch - all ( any exception )
cerr « " Unknown error occurred ! " « endl ;
}

cout « " Program still running ! " « endl ;


return 0;
}

Standard exception classes: runtime_error, invalid_argument, out_of_range, overflow_error, bad_alloc. All have .what() method!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 16 / 29


Custom Exception Classes — Professional Approach
# include < iostream >
# include < exception >
using namespace std ;

// Custom exception class inheriting from std :: exception


class I n s u f f i c i e n t F u n d s E x c e p t i o n : public exception {
double deficit ;
public :
I n s u f f i c i e n t F u n d s E x c e p t i o n ( double d ) : deficit ( d ) {}
const char * what () const noexcept override {
return " Insufficient funds in account ! " ;
}
double getDeficit () const { return deficit ; }
};

class BankAccount {
double balance ;
public :
BankAccount ( double b ) : balance ( b ) {}
void withdraw ( double amount ) {
if ( amount > balance )
throw I n s u f f i c i e n t F u n d s E x c e p t i o n ( amount - balance ) ;
balance -= amount ;
cout « " Withdrawn : " « amount « " , Balance : " « balance « endl ;
}
};

int main () {
try {
BankAccount acc (1000) ;
acc . withdraw (500) ; // OK
acc . withdraw (700) ; // Throws !
}
catch ( const I n s u f f i c i e n t F u n d s E x c e p t i o n & e ) {
cerr « e . what () « " Short by : Rs . " « e . getDeficit () « endl ;
}
return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 17 / 29


Templates — Write Once, Use for Any Type!

The Problem
Want to write a max() function? Without templates, you need:
int max(int a, int b)
double max(double a, double b)
string max(string a, string b)
Same logic, different types — code duplication!

Templates let you write code that works with any data type. The compiler generates the specific version when you use it.

Analogy
A template is like a form with blanks:
“I would like ____ for dinner.”
You fill in: pizza, biryani, pasta. Same form, different content!
Templates: “Sort an array of _____.” Fill in: int, double, string. Same algorithm, different types!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 18 / 29


Function Templates
# include < iostream >
using namespace std ;

// Function Template : T is a placeholder for ANY type


template < typename T >
T findMax ( T a , T b ) {
return ( a > b ) ? a : b ;
}

// Template with multiple type parameters


template < typename T1 , typename T2 >
void display ( T1 a , T2 b ) {
cout « a « " and " « b « endl ;
}

int main () {
// Compiler generates int version automatically
cout « findMax (10 , 20) « endl ; // 20

// Compiler generates double version


cout « findMax (3.14 , 2.72) « endl ; // 3.14

// Compiler generates string version


cout « findMax ( string ( " Amit " ) , string ( " Zara " ) ) « endl ; // Zara

// Multiple types
display (42 , " hello " ) ; // 42 and hello
display (3.14 , 100) ; // 3.14 and 100
return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 19 / 29


Class Templates
# include < iostream >
using namespace std ;

// Class Template : Stack that works with ANY type


template < typename T >
class Stack {
T arr [100];
int top ;
public :
Stack () : top ( -1) {}

void push ( T val ) {


if ( top >= 99) { cout « " Stack Overflow ! " « endl ; return ; }
arr [++ top ] = val ;
}

T pop () {
if ( top < 0) { cout « " Stack Underflow ! " « endl ; return T () ; }
return arr [ top - -];
}

T peek () const { return arr [ top ]; }


bool isEmpty () const { return top < 0; }
};

int main () {
Stack < int > intStack ; // Stack of integers
intStack . push (10) ;
intStack . push (20) ;
cout « intStack . pop () « endl ; // 20

Stack < string > strStack ; // Stack of strings !


strStack . push ( " Hello " ) ;
strStack . push ( " World " ) ;
cout « strStack . pop () « endl ; // World
return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 20 / 29


Templates — Key Points
Function Template Class Template
Parameterizes a function Parameterizes a class
Type deduced from arguments Type explicitly specified:
Stack<int>
Used for generic algorithms Used for generic containers
template<typename T> template<typename T>
T max(T a, T b) class Stack {...}

Templates Power the STL!


The entire Standard Template Library (our next topic!) is built using templates. vector<int>, map<string, int>, set<double> — all are class templates!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 21 / 29


The Standard Template Library (STL) — Overview
The STL is a powerful library of generic, reusable components built
using templates. Why STL?
Without STL, you’d write your own linked list, sort algorithm,
Four Main Components: binary search tree, hash map...
1 Containers — Store collections of data (vector, list, map)
STL gives you all of this tested, optimized, and ready to use!
2 Algorithms — Operate on containers (sort, find, count) It’s like getting a fully equipped workshop instead of making
3 Iterators — Navigate through containers (like smart pointers) your own tools.
4 Function Objects (Functors) — Objects that act like functions

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 22 / 29


STL Containers — Types
Type Container Description Header
vector Dynamic array <vector>
Sequence list Doubly-linked list <list>
deque Double-ended queue <deque>
set Unique sorted elements <set>
Associative map Key-value pairs (sorted) <map>
multiset / multimap Allow duplicates <set>/<map>
stack LIFO <stack>
Adapter queue FIFO <queue>
priority_queue Sorted queue <queue>

vector is the most commonly used container. When in doubt, use a vector!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 23 / 29


The vector Container — Your New Best Friend
# include < iostream >
# include < vector >
using namespace std ;

int main () {
// Creating vectors
vector < int > marks ; // Empty vector
vector < string > names = { " Amit " , " Priya " , " Rahul " }; // Initialized

// Adding elements
marks . push_back (85) ;
marks . push_back (92) ;
marks . push_back (78) ;

// Accessing elements
cout « " First : " « marks [0] « endl ; // 85
cout « " Safe : " « marks . at (1) « endl ; // 92 ( bounds - checked !)

// Size and capacity


cout « " Size : " « marks . size () « endl ; // 3
cout « " Empty ? " « marks . empty () « endl ; // 0 ( false )

// Iterating with range - based for loop ( C ++11)


cout « " All marks : " ;
for ( int m : marks ) {
cout « m « " " ;
}
cout « endl ;

// Remove last element


marks . pop_back () ;
cout « " After pop : size = " « marks . size () « endl ; // 2

return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 24 / 29


The map Container — Key-Value Storage
# include < iostream >
# include <map >
using namespace std ;

int main () {
// map < KeyType , ValueType > - automatically sorted by key
map < string , int > studentMarks ;

// Inserting key - value pairs


studentMarks [ " Amit " ] = 85;
studentMarks [ " Priya " ] = 92;
studentMarks [ " Rahul " ] = 78;
studentMarks . insert ({ " Neha " , 88}) ;

// Accessing
cout « " Amit ’s marks : " « studentMarks [ " Amit " ] « endl ; // 85

// Iterating
cout « " \n - - - All Records ( sorted by name !) ---" « endl ;
for ( auto & pair : studentMarks ) {
cout « pair . first « " : " « pair . second « endl ;
}

// Finding
auto it = studentMarks . find ( " Priya " ) ;
if ( it != studentMarks . end () )
cout « " \ nFound : " « it - > first « " = " « it - > second « endl ;

// Size and erase


studentMarks . erase ( " Rahul " ) ;
cout « " Size after erase : " « studentMarks . size () « endl ; // 3

return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 25 / 29


Iterators — Navigating Containers
An iterator is like a pointer that points to elements inside a # include < iostream >
container. # include < vector >
using namespace std ;

int main () {
Analogy vector < int > v = {10 , 20 , 30 , 40};

A container is a train with compartments. // Using iterator


vector < int >:: iterator it ;
An iterator is a person walking through the train, visiting each compart- for ( it = v . begin () ;
ment one by one. it != v . end () ; ++ it ) {
cout « * it « " " ;
Types of Iterators: }
// Output : 10 20 30 40
begin() — points to first element // Auto keyword ( C ++11)
for ( auto it = v . begin () ;
end() — points past the last element it != v . end () ; ++ it ) {
rbegin() — reverse begin }
* it *= 2; // Modify via iterator

rend() — reverse end


// Range - based for ( easiest !)
for ( int x : v ) {
cout « x « " " ;
}
// Output : 20 40 60 80

return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 26 / 29


STL Algorithms — Powerful Operations
# include < iostream >
# include < vector >
# include < algorithm > // For STL algorithms
# include < numeric > // For accumulate
using namespace std ;

int main () {
vector < int > v = {64 , 25 , 12 , 22 , 11 , 90 , 45};

// SORT
sort ( v . begin () , v . end () ) ;
// v = {11 , 12 , 22 , 25 , 45 , 64 , 90}

// FIND
auto it = find ( v . begin () , v . end () , 25) ;
if ( it != v . end () ) cout « " Found 25 at index : " « ( it - v . begin () ) « endl ;

// COUNT
vector < int > grades = {85 , 90 , 85 , 78 , 92 , 85};
cout « " Count of 85: " « count ( grades . begin () , grades . end () , 85) « endl ; // 3

// MIN and MAX


cout « " Min : " « * min_element ( v . begin () , v . end () ) « endl ; // 11
cout « " Max : " « * max_element ( v . begin () , v . end () ) « endl ; // 90

// REVERSE
reverse ( v . begin () , v . end () ) ;

// ACCUMULATE ( sum )
int total = accumulate ( v . begin () , v . end () , 0) ;
cout « " Sum : " « total « endl ;

// FOR_EACH ( apply a function to every element )


for_each ( v . begin () , v . end () , []( int x ) { cout « x « " " ; }) ;

return 0;
}

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 27 / 29


When to Use Which Container?
Container Use When...
vector Default choice. Fast random access. Elements added at the
end.
list Frequent insertions/deletions in the middle. No random access
needed.
deque Need to add/remove from both ends efficiently.
set Need unique, automatically sorted elements.
map Need key-value pairs with fast lookup by key.
stack Need LIFO behavior (undo operations, expression evaluation).
queue Need FIFO behavior (task scheduling, BFS).
priority_queue Need to always access the largest/smallest element quickly.

Rule of Thumb
Start with vector. Switch to another container only if you have a specific reason!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 28 / 29


Module V — Recap ✓
Topics Covered: 8 Function Templates
1 Strings: C-style vs std::string 9 Class Templates
2 File Handling: ifstream, ofstream, fstream 10 STL Overview
3 File Modes: ios::in, ios::out, ios::app 11 Containers: vector, map, set, stack, queue
4 Formatted I/O: setw, setprecision, manipulators 12 Algorithms: sort, find, count, reverse
5 Unformatted I/O: get(), put(), getline() 13 Iterators: begin, end, traversal
6 Exception Handling: try/catch/throw
7 Standard Exceptions and Custom Exceptions
Congratulations!
You’ve covered the entire ES203 syllabus! From basic OOP concepts
to advanced STL — you’re now equipped to write powerful, professional
C++ programs!

ES203 – OOP Using C++ Module V: Strings, Files & Exceptions 29 / 29

You might also like