Notes on C++ Course

Jul 24, 2024

C++ Course Notes

Introduction to C++

  • Instructor: Mike
  • C++ is a popular programming language, related to C.
  • Great for beginners and carries over knowledge for other languages.

Course Overview

  • Starting with basics:
    • Setup (installation of C++ and text editor).
    • Writing the first program.
  • Intermediate:
    • Variables, data manipulation, simple games.
  • Advanced:
    • Control structures: if statements, loops, classes, objects.

Setting Up for C++ Programming

  • Requirements:
    1. A text editor (preferably an IDE).
    2. A C++ compiler.
    • Recommended IDE: Code::Blocks (install via www.codeblocks.org).

Installation Steps:

  1. Visit www.codeblocks.org.
  2. Click on downloads and then binary release.
  3. Download the appropriate version (e.g., Code::Blocks 16.01 MinGW setup).
  4. Install following the default options.

Writing Your First C++ Program

  • Basic program structure.
  • Using cout and cin for printing and getting input.

Basic Terminology

  • Variables: Containers for storing data (e.g., int, double, string).
  • Functions: Blocks of code that perform specific tasks.
  • Classes and Objects: Templates for creating objects in OOP.

Variables and Data types

  • String: Text.
  • Integer: Whole number.
  • Double: Decimal number.
  • Char: Single character.
  • Boolean: True or false.

Example: Creating and Modifying Variables

int age = 19;
double GPA = 2.7;
string name = "Mike";

Pointers

  • Store memory addresses of variables.
  • Use of & to get address and * for dereferencing.

Example

int* pAge = &age;
cout << *pAge; // prints 19

Classes and Objects

  • Classes specify what objects can do (attributes and methods).
  • Objects are instances of classes.

Example of a Class

class Movie {
    public:
        string title;
        string director;
        int rating;
};

Constructors

  • Special function that initializes objects.

Example

Movie(string title, string director, int rating) {
    this->title = title;
    this->director = director;
    this->rating = rating;
}

Getters and Setters

  • Control access to attributes.
  • Use public methods for interacting with private attributes.

Inheritance

  • Create new classes based on existing ones.
  • Example: ItalianChef inherits from Chef.
class ItalianChef : public Chef {
    void makePasta() {
        cout << "Making pasta";
    }
};

Object Functions

  • Methods that define actions and behaviors of objects.

Switch Statements

  • Allow easy comparison of a variable against many values.

Example

switch(day) {
    case 0: cout << "Sunday";
    case 1: cout << "Monday";
    default: cout << "Invalid day";
}

Loops

  • While Loop: Executes code while a condition is true.
    • Be cautious of infinite loops.
  • For Loop: Executes code a specific number of times.
for(int i = 0; i < 5; i++) {
    cout << i;
}

Guessing Game

  • Allow user to guess a number, use loops and conditional statements to validate guesses.

Example of Game Logic

while (secretNum != guess && !outOfGuesses) {
    // Prompt user
}
if (outOfGuesses) {
    cout << "You lose!";
} else {
    cout << "You win!";
}

Two-Dimensional Arrays

  • An array of arrays.

Example

int numberGrid[3][2] = {{1, 2}, {3, 4}, {5, 6}};

Nested Loops

  • For loops inside other loops to traverse multi-dimensional arrays.

Example

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        cout << numberGrid[i][j];
    }
}

Comments

  • Use // for single line comments
  • Use /* */ for multi-line comments.

Summary

  • Pointers: Store memory locations.
  • Classes: Blueprints for objects.
  • Inheritance: Allow classes to inherit attributes and behaviors from others.
  • Getters and Setters: Control access within classes.
  • Loops and Conditional Statements: Structure program flow based on conditions.