C++ Programming Labs

Master move semantics, multithreading, and modern C++ resource management.

Move Semantics, Threads & RAII - Module 8

Write efficient, concurrent, and resource-safe C++ code.

Lab 22: Move Semantics
Expert
Coding Challenge
Your Task: Understand move semantics - a C++11 feature that enables efficient transfer of resources without copying. This is crucial for performance optimization.

Detailed Requirements:
1. Understand lvalues vs rvalues:
int x = 10; // x is lvalue (has address) int y = x + 5; // x+5 is rvalue (temporary) int&& rref = 20; // rvalue reference to temporary

2. Use std::move to cast to rvalue:
string s1 = "Hello"; string s2 = std::move(s1); // s1's data moved to s2 // s1 is now in valid but unspecified state

3. Create a class with move constructor:
class Buffer { int* data; size_t size; public: // Move constructor Buffer(Buffer&& other) noexcept : data(other.data), size(other.size) { other.data = nullptr; other.size = 0; } // Move assignment Buffer& operator=(Buffer&& other) noexcept { if (this != &other) { delete[] data; data = other.data; size = other.size; other.data = nullptr; other.size = 0; } return *this; } };

Expected Output:
Original: Hello World After move, s2: Hello World s1 is now empty Move constructor called Move assignment called

Requirements Checklist

Use std::move to transfer ownership
Create rvalue reference with &&
Implement move constructor
Use noexcept specifier
Implement move assignment operator
Set source to valid empty state after move
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Move: T(T&& other) noexcept
• Transfer: std::move(obj)
• Always use noexcept for move operations
• Set source to valid empty state after move
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 23: Multithreading Basics
Expert
Coding Challenge
Your Task: Learn C++11 threading basics. Create and manage threads to run code concurrently.

Detailed Requirements:
1. Include thread header:
#include <thread>

2. Create and run a thread:
void task() { cout << "Running in thread" << endl; } thread t(task); // Start thread t.join(); // Wait for completion

3. Thread with lambda:
thread t([]() { cout << "Lambda thread" << endl; }); t.join();

4. Thread with parameters:
void greet(string name) { cout << "Hello, " << name << endl; } thread t(greet, "World"); t.join();

5. Using mutex for synchronization:
#include mutex mtx; void safe_print(int id) { lock_guard lock(mtx); cout << "Thread " << id << endl; }

Expected Output:
Main thread starting Worker thread running Thread ID: 12345 Counter value: 1000 All threads complete

Requirements Checklist

Include thread header
Create a thread object
Use join() to wait for thread
Pass function to thread
Include mutex for synchronization
Use lock_guard for safe locking
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Create: thread t(function, args...);
• Wait: t.join();
• Detach: t.detach();
• Lock: lock_guard<mutex> lock(mtx);
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 24: RAII Pattern
Expert
Coding Challenge
Your Task: Implement RAII (Resource Acquisition Is Initialization) - a fundamental C++ idiom for managing resources safely.

Detailed Requirements:
1. RAII Principle:
   • Acquire resources in constructor
   • Release resources in destructor
   • Resources are automatically freed when object goes out of scope

2. Create a FileHandle RAII class:
class FileHandle { FILE* file; public: FileHandle(const char* name, const char* mode) { file = fopen(name, mode); if (!file) throw runtime_error("Cannot open"); } ~FileHandle() { if (file) fclose(file); cout << "File closed automatically" << endl; } // Disable copying FileHandle(const FileHandle&) = delete; FileHandle& operator=(const FileHandle&) = delete; };

3. Create a Timer RAII class:
class Timer { chrono::time_point start; string name; public: Timer(string n) : name(n), start(chrono::high_resolution_clock::now()) {} ~Timer() { auto end = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast(end - start); cout << name << " took " << duration.count() << "ms" << endl; } };

Expected Output:
Resource acquired Using resource... Resource released automatically Timer: Operation took 100ms

Requirements Checklist

Acquire resource in constructor
Release resource in destructor
Delete copy constructor (= delete)
Delete copy assignment (= delete)
Use scope to trigger automatic cleanup
Print message showing automatic release
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Acquire in constructor, release in destructor
• Delete copy: T(const T&) = delete;
• Use scopes { } to control lifetime
• Destructors called in reverse order of construction
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Congratulations!

You've completed all 24 C++ Programming Labs! You're now a C++ expert ready for real-world development.