C++ Programming Labs

Master object-oriented programming, file handling, and the STL in C++.

Classes, File I/O & Vectors - Module 5

Build real-world applications with OOP, file handling, and dynamic containers.

Lab 13: Classes & Objects
Advanced
Coding Challenge
Your Task: Create a class with private data members, a constructor, and public methods. Classes are the foundation of Object-Oriented Programming (OOP) in C++.

Detailed Requirements:
1. Define a BankAccount class:
class BankAccount { private: string owner; double balance; public: // Constructor and methods go here };
   • private: members can only be accessed within the class
   • public: members can be accessed from outside
   • This is called encapsulation - hiding internal data

2. Create a constructor:
BankAccount(string n, double b) : owner(n), balance(b) {}
   • Constructors initialize objects when created
   • Use initializer list : owner(n), balance(b) for efficiency

3. Add getter methods:
string getOwner() { return owner; }
double getBalance() { return balance; }
   • Getters provide read access to private data

4. Add deposit and withdraw methods:
void deposit(double amount) { if (amount > 0) balance += amount; } void withdraw(double amount) { if (amount > 0 && amount <= balance) balance -= amount; }
   • Methods can validate data before modifying it

Expected Output:
Owner: John Doe Balance: $1000 After deposit of $500: $1500 After withdrawal of $200: $1300

Requirements Checklist

Define a class with private data members
Create a constructor with parameters
Add getter methods for private data
Add a deposit method that modifies balance
Add a withdraw method with validation
Create an object and call all methods
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Class syntax: class Name { private: ... public: ... };
• Constructor: ClassName(params) : member(val) {}
• Create object: ClassName obj(args);
• Call method: obj.methodName();
• Don't forget semicolon after class definition!
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 14: File Input/Output
Advanced
Coding Challenge
Your Task: Learn to read from and write to files using C++ file streams. File I/O is essential for persistent data storage.

Detailed Requirements:
1. Include the fstream header:
#include <fstream>
   • ofstream - output file stream (write)
   • ifstream - input file stream (read)
   • fstream - both read and write

2. Write to a file:
ofstream outFile("data.txt"); if (outFile.is_open()) { outFile << "Hello, File!" << endl; outFile << "Line 2" << endl; outFile.close(); }
   • Always check if file opened with is_open()
   • Use << just like with cout
   • Always close() when done

3. Read from a file:
ifstream inFile("data.txt"); string line; while (getline(inFile, line)) { cout << line << endl; } inFile.close();
   • getline() reads one line at a time
   • Loop continues until end of file

File Modes:
ios::app - append to end of file
ios::trunc - clear file before writing (default)
ios::binary - binary mode

Expected Output:
Writing to file... File written successfully! Reading from file: Hello, File! Line 2 C++ is awesome!

Requirements Checklist

Include the fstream header
Create an ofstream object and open a file
Write multiple lines to the file
Close the output file
Create an ifstream and read the file
Use getline() to read and print each line
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Write: ofstream file("name.txt"); file << data;
• Read: ifstream file("name.txt"); getline(file, str);
• Always check: if (file.is_open())
• Always close: file.close();
• Append mode: ofstream file("name.txt", ios::app);
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 15: Vectors (Dynamic Arrays)
Advanced
Coding Challenge
Your Task: Use vectors - dynamic arrays from the C++ Standard Template Library (STL). Unlike regular arrays, vectors can grow and shrink automatically.

Detailed Requirements:
1. Include the vector header and create a vector:
#include <vector>
vector<int> numbers;
   • Vectors are template classes - specify type in <>
   • Can also initialize: vector<int> nums = {1, 2, 3};

2. Add elements with push_back:
numbers.push_back(10);
numbers.push_back(20);
   • Elements added to the end
   • Vector grows automatically

3. Access elements and get size:
cout << numbers[0]; // Access by index
cout << numbers.at(0); // Bounds-checked access
cout << numbers.size(); // Number of elements

4. Iterate with range-based for loop:
for (int num : numbers) { cout << num << " "; }

5. Useful vector methods:
pop_back() - remove last element
clear() - remove all elements
empty() - check if empty
front() / back() - first/last element

Expected Output:
Vector size: 5 Elements: 10 20 30 40 50 First: 10, Last: 50 After pop_back: 10 20 30 40 Using iterator: 10 20 30 40

Requirements Checklist

Include the vector header
Create a vector and add elements with push_back
Print the vector size using size()
Access elements using [] or at()
Use a range-based for loop to iterate
Use pop_back() to remove an element
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Create: vector<type> name;
• Add: vec.push_back(value);
• Size: vec.size();
• Loop: for (type item : vec) { ... }
• Remove last: vec.pop_back();
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Congratulations!

You've completed all 15 C++ Programming Labs! You now have a solid foundation in C++ programming.