# C++ Programming Basics: Part 2 - Building on Your Foundation

Great job mastering the basics! Now that you understand variables, functions, control flow, and basic input/output operations, let's dive deeper into more advanced concepts that will make your C++ skills much stronger.

## Functions (Continued)

### Function Parameters and Return Values

We already saw simple functions, but here are more advanced examples:

```cpp
// Function with multiple parameters
int multiply(int x, int y) {
    return x * y;
}

// Function with default parameter values
void greet(string name, string greeting = "Hello") {
    cout << greeting << ", " << name << "!" << endl;
}

// Using the functions
int result = multiply(5, 3);        // Returns 15
greet("Alice");                     // Prints: Hello, Alice!
greet("Bob", "Hi");                 // Prints: Hi, Bob!
```

### Function Overloading

You can have multiple functions with the same name but different parameters:

```cpp
int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

// Both work:
int sum1 = add(5, 3);       // Uses first version
double sum2 = add(5.5, 3.2); // Uses second version
```

## Arrays and Vectors

### Traditional Arrays (Fixed Size)

```cpp
// Declare array with specific size
int numbers[10];        // Creates array of 10 integers
numbers[0] = 5;         // Assign first element
numbers[9] = 100;       // Assign last element

// Initialize at declaration
int scores[] = {85, 92, 78, 96, 88};     // Size automatically determined
```

### Vectors (Dynamic Arrays)

Vectors are more flexible than traditional arrays:

```cpp
#include <vector>
using namespace std;

vector<int> numbers;        // Empty vector
numbers.push_back(5);       // Add element to end
numbers.push_back(10);      // Add another

// Access elements
cout << numbers[0] << endl;  // First element: 5

// Size of vector
int size = numbers.size();   // Returns 2
```

## Working with Strings More Effectively

```cpp
#include <string>
using namespace std;

string text = "Hello World";

// String methods
int length = text.length();     // Get string length
string upper = text.substr(0, 5); // Extract substring: "Hello"
bool found = text.find("World") != string::npos; // Find substring

// Modify strings
text += "!!!";                  // Concatenate
text[0] = 'h';                  // Change first character to lowercase
```

## Introduction to Classes and Objects

Classes are the foundation of object-oriented programming in C++:

```cpp
#include <string>
using namespace std;

class Student {
private:
    string name;
    int age;
    
public:
    // Constructor - function that creates objects
    Student(string studentName, int studentAge) {
        name = studentName;
        age = studentAge;
    }
    
    // Member functions (methods)
    void displayInfo() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
    
    string getName() {
        return name;
    }
};

// Using the class
int main() {
    Student s1("Alice", 20);     // Create object
    Student s2("Bob", 22);
    
    s1.displayInfo();            // Output: Name: Alice, Age: 20
    
    return 0;
}
```

## File Input/Output

Working with files is a common task:

```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    // Writing to file
    ofstream outFile("example.txt");
    outFile << "Hello, World!" << endl;
    outFile << "This is line 2" << endl;
    outFile.close();
    
    // Reading from file
    ifstream inFile("example.txt");
    string line;
    while (getline(inFile, line)) {
        cout << line << endl;   // Prints each line
    }
    inFile.close();
    
    return 0;
}
```

## More Control Flow Features

### Switch Statements

```cpp
int day = 3;

switch (day) {
    case 1:
        cout << "Monday" << endl;
        break;
    case 2:
        cout << "Tuesday" << endl;
        break;
    case 3:
        cout << "Wednesday" << endl;
        break;
    default:
        cout << "Other day" << endl;
}
```

### Using Break and Continue

```cpp
// Break - exits the loop completely
for (int i = 1; i <= 10; i++) {
    if (i == 5) break;     // Stop when reaching 5
    cout << i << " ";
}   // Output: 1 2 3 4

// Continue - skips to next iteration
for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) continue;   // Skip even numbers
    cout << i << " ";           // Only prints odd numbers
}   // Output: 1 3 5 7 9
```

## Memory Management

Understanding how variables work in memory:

```cpp
int a = 10;
int b = a;          // Copy value to b
a = 20;             // Change a

cout << "a: " << a << endl;     // Output: a: 20
cout << "b: " << b << endl;     // Output: b: 10 (unchanged)

// Reference variables - both refer to same memory location
int c = 10;
int& ref = c;       // ref refers to c
c = 30;
cout << ref << endl;    // Output: 30 (same value as c)
```

## Practice Exercise

Let's build a simple program that uses multiple concepts:

Create a Grade Tracker that:
1. Allows user to enter student names and grades
2. Stores them in arrays/vectors
3. Calculates average grade
4. Finds highest/lowest grades

```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
    vector<string> names;
    vector<int> grades;
    int numStudents, total = 0;
    
    cout << "How many students? ";
    cin >> numStudents;
    
    // Input student data
    for (int i = 0; i < numStudents; i++) {
        string name;
        int grade;
        cout << "Enter name for student " << (i+1) << ": ";
        cin >> name;
        cout << "Enter grade: ";
        cin >> grade;
        
        names.push_back(name);
        grades.push_back(grade);
        total += grade;
    }
    
    // Calculate and display results
    double average = static_cast<double>(total) / numStudents;
    cout << "\nAverage grade: " << average << endl;
    
    return 0;
}
```

## Key Takeaways

You're now ready to:
- Work with more complex data structures (vectors, strings)
- Create reusable code using functions
- Organize your code into classes and objects
- Handle file operations
- Use advanced control flow features

Keep practicing these concepts by building small programs that combine multiple elements. The more you code, the more natural these patterns will become.

What would you like to explore next?