C++ Concepts Gurnoor Singh
14-Mark Master Blueprint: Advanced C++
Concepts
Pointers, Arrays, Memory, File Handling, Templates, Exceptions
1. What are Pointers? Explain their uses with ex-
ample.
Definition
A pointer is a variable that stores the memory address of another variable.
Key Concepts
• Declared using *: int *ptr;
• Use & to get address, * to access value
• Pointer arithmetic: increment, decrement, etc.
Example
int a = 10;
int * p = & a ;
cout << * p ; // Output : 10
Uses of Pointers
• Dynamic memory allocation
• Arrays and strings handling
• Function arguments (pass by address)
• Data structures like linked list, trees
• Efficient memory manipulation
2. Explain Array of Pointers with example.
Definition
An array of pointers is a collection where each element is a pointer.
Use Cases
• Storing addresses of strings or arrays
• Dynamic memory use in 2D arrays
1
C++ Concepts Gurnoor Singh
Example
const char * arr [3] = { " One " , " Two " , " Three " };
for ( int i = 0; i < 3; i ++)
cout << arr [ i ] << " \ n " ;
3. What is Dynamic Memory Allocation in C++?
Explain with examples.
Definition
DMA allows allocation of memory at runtime using new and deallocation using delete.
Operators
• new – allocates memory
• delete – frees memory
Examples
Single Variable
int * p = new int ;
* p = 5;
delete p ;
Array
int * arr = new int [5];
for ( int i =0; i <5; i ++) arr [ i ] = i ;
delete [] arr ;
Advantages
• Efficient memory usage
• Useful for structures with unknown size
• Supports flexible data structures
4. Explain File Handling in C++ with example.
Definition
File handling allows reading/writing data to/from files using file streams. It is useful for
storing data permanently.
2
C++ Concepts Gurnoor Singh
Header File
<fstream> is used, which provides:
• ifstream – input file stream (read from file)
• ofstream – output file stream (write to file)
• fstream – both input and output
Opening Modes
• ios::in, ios::out, ios::app, ios::binary, ios::ate
Example
# include < fstream >
using namespace std ;
ofstream out ( " data . txt " ) ;
out << " Hello " ;
out . close () ;
ifstream in ( " data . txt " ) ;
string str ;
in >> str ;
cout << str ;
in . close () ;
Common File Functions
• open(), close()
• getline(), eof()
• seekg(), seekp(), tellg(), tellp()
Best Practices
• Always check if file opened: if(file.is open())
• Close the file after operation
• Use exception handling for robust file operations
5. What are Templates in C++? Explain Function
and Class Templates.
Definition
Templates allow writing generic code for any data type.
3
C++ Concepts Gurnoor Singh
Function Template
template < typename T >
T add ( T a , T b ) { return a + b ; }
Class Template
template < class T >
class Box {
T value ;
public :
void set ( T v ) { value = v ; }
T get () { return value ; }
};
Advantages
• Reusability
• Type safety
• Less code repetition
6. What is Exception Handling in C++? Explain
try-catch block.
Definition
Exception handling is a mechanism to detect and handle runtime errors without crashing
the program. It separates error-handling code from regular code.
Keywords
• try – wraps code that may cause an error
• throw – raises an exception
• catch – handles the thrown exception
Syntax
try {
// code
throw value ;
}
catch ( type var ) {
// handler
}
4
C++ Concepts Gurnoor Singh
Example
try {
int a = 5 , b = 0;
if ( b == 0) throw " Divide ␣ by ␣ zero " ;
cout << a / b ;
} catch ( const char * msg ) {
cout << msg ;
}
Types of Exceptions
• Synchronous (e.g., divide by zero)
• Asynchronous (e.g., I/O error)
Multiple Catch Blocks
try {
throw 10;
} catch ( int e ) {
cout << " Int ␣ exception " ;
} catch (...) {
cout << " Default ␣ handler " ;
}
Advantages
• Graceful error recovery
• Improves program stability
• Separates error logic from main logic
Exam Tips
• Always define concepts first
• Explain syntax and usage with clean code
• Add output and comments to code
• Underline important terms
• Use diagrams if needed (file streams, pointers)