C++ Programming Labs

Master lambda expressions, STL algorithms, and object-oriented inheritance.

Lambdas, STL Algorithms & Inheritance - Module 7

Write modern, functional C++ and leverage OOP principles.

Lab 19: Lambda Expressions
Expert
Coding Challenge
Your Task: Learn to write lambda expressions - anonymous inline functions. Lambdas are powerful for callbacks, algorithms, and functional programming.

Detailed Requirements:
1. Basic lambda syntax:
[capture](parameters) -> return_type { // body }
   • [] - capture clause (variables from outer scope)
   • () - parameters (like regular functions)
   • -> type - return type (optional, usually inferred)

2. Simple lambda examples:
auto greet = []() { cout << "Hello!"; }; greet(); // Prints: Hello! auto add = [](int a, int b) { return a + b; }; cout << add(3, 4); // Prints: 7

3. Capture modes:
int x = 10; auto byValue = [x]() { return x; }; // Copy auto byRef = [&x]() { x++; }; // Reference auto allByValue = [=]() { return x; }; // All by copy auto allByRef = [&]() { x++; }; // All by ref

4. Use with STL algorithms:
vector nums = {1, 2, 3, 4, 5}; for_each(nums.begin(), nums.end(), [](int n) { cout << n * 2 << " "; });

Expected Output:
Simple lambda: 7 With capture: 15 Modified by ref: 11 Doubled: 2 4 6 8 10

Requirements Checklist

Create a basic lambda with [] syntax
Create a lambda with parameters
Use capture by value [x]
Use capture by reference [&x]
Store lambda in auto variable
Use lambda with for_each algorithm
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Basic: auto f = []() { };
• With params: auto f = [](int x) { return x; };
• Capture value: [x], reference: [&x]
• Capture all: [=] by value, [&] by ref
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 20: STL Algorithms
Expert
Coding Challenge
Your Task: Master the C++ Standard Template Library algorithms. These powerful functions work with iterators and simplify common operations.

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

2. Sorting algorithms:
vector v = {5, 2, 8, 1, 9}; sort(v.begin(), v.end()); // Ascending sort(v.begin(), v.end(), greater()); // Descending

3. Searching algorithms:
auto it = find(v.begin(), v.end(), 5); if (it != v.end()) cout << "Found!"; bool exists = binary_search(v.begin(), v.end(), 5);

4. Transformation algorithms:
// Transform each element transform(v.begin(), v.end(), v.begin(), [](int x) { return x * 2; }); // Copy elements matching condition vector evens; copy_if(v.begin(), v.end(), back_inserter(evens), [](int x) { return x % 2 == 0; });

5. Numeric algorithms:
#include int sum = accumulate(v.begin(), v.end(), 0); int count = count_if(v.begin(), v.end(), [](int x) { return x > 5; });

Expected Output:
Sorted: 1 2 3 4 5 Found 3 at position 2 Sum: 15 Count > 2: 3

Requirements Checklist

Include algorithm header
Use sort() to sort a vector
Use find() to search for element
Use transform() or for_each()
Use accumulate() for sum
Use count_if() with predicate
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Sort: sort(v.begin(), v.end());
• Find: find(v.begin(), v.end(), value);
• Sum: accumulate(v.begin(), v.end(), 0);
• Count: count_if(begin, end, predicate);
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 21: Inheritance & Polymorphism
Expert
Coding Challenge
Your Task: Implement inheritance and polymorphism - core OOP concepts. Create a class hierarchy with virtual functions.

Detailed Requirements:
1. Create a base class with virtual function:
class Animal { public: virtual void speak() { cout << "Animal sound" << endl; } virtual ~Animal() {} // Virtual destructor };
   • virtual enables runtime polymorphism
   • Always use virtual destructor in base classes

2. Create derived classes:
class Dog : public Animal { public: void speak() override { cout << "Woof!" << endl; } }; class Cat : public Animal { public: void speak() override { cout << "Meow!" << endl; } };
   • : public Animal inherits from Animal
   • override ensures we're overriding a virtual function

3. Use polymorphism:
Animal* pet = new Dog(); pet->speak(); // Prints "Woof!" (dynamic dispatch) delete pet;

Expected Output:
Dog says: Woof! Cat says: Meow! Using base pointer: Woof! Meow!

Requirements Checklist

Create base class with virtual function
Add virtual destructor to base class
Create derived class using : public
Override virtual function with override keyword
Create second derived class
Demonstrate polymorphism with base pointer
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Virtual: virtual void func() { }
• Inherit: class Derived : public Base { }
• Override: void func() override { }
• Always use virtual ~Base() {}
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below