Draft Academy C++ Course

Jul 18, 2024

Draft Academy C++ Course Notes

Introduction

  • Instructor: Mike
  • Course Overview: Learn core concepts of C++ programming.

Course Outline

  1. Basics

    • Setup C++ on your computer
    • Install a text editor
    • Write your first program
  2. Core Concepts

    • Variables
    • Data types
    • Loops
    • Conditional statements (if statements)
    • Functions
    • Classes and Objects
  3. Intermediate Concepts

    • Arrays
    • Pointers
    • Inheritance
    • Encapsulation
    • Constructors
    • Object Functions

Setup Instructions

Tools Required

  1. Text Editor

    • Recommendations: Notepad, CodeBlocks (IDE)
    • CodeBlocks: Integrated Development Environment (IDE) for C++
  2. C++ Compiler

    • Translates C++ code to machine-readable language
    • Installation tips on Windows, Linux, Mac

Programming Basics

Writing Your First Program

  • Example: Hello World
    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello, World!" << endl;
        return 0;
    }
    ``
    

Variables

  • Variable types: ints, doubles, chars, strings
  • Example:
    string characterName;
    int characterAge;
    

User Input

  • Using cin for basic data types
    int age;
    cout << "Enter your age: ";
    cin >> age;
    
  • Using getline for strings
    string fullName;
    cout << "Enter your full name: ";
    getline(cin, fullName);
    

Basic Arithmetic

  • Example of adding two numbers
    int num1, num2, sum;
    cout << "Enter num1: ";
    cin >> num1;
    cout << "Enter num2: ";
    cin >> num2;
    sum = num1 + num2;
    cout << "Sum is: " << sum << endl;
    

Control Structures

  • If Statements
    if (condition)
       // code to execute
    else
       // alternate code
    
  • Switch Statements
    switch(expression) {
       case x: // code
         break;
       default: // code
    }
    
  • Loops
    • While Loop
      while (condition)
        // code
      
    • For Loop
      for (int i = 0; i < n; i++)
        // code
      
    • Do-While Loop
      do {
       // code
      } while(condition);
      

Functions

  • Function Creation
    returnType functionName(parameters) {
        // code
    }
    
  • Example: Exponent Function
    int power(int baseNum, int powNum) {
        int result = 1;
        for (int i = 0; i < powNum; i++)
            result *= baseNum;
        return result;
    }
    

Arrays

  • Single and Multi-dimensional arrays
    int numbers[5] = {1, 2, 3, 4, 5};
    int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
    
  • Iterating with loops
    for (int i = 0; i < 5; i++)
       cout << numbers[i] << endl;
    

Pointers

Basics

  • Definition: Memory address variables
  • Example:
    int age = 30;
    int *pAge = &age;
    

Dereferencing Pointers

cout << *pAge << endl; // Outputs 30

Pointers in Functions

void swap(int *a, int *b) {
   int temp = *a;
   *a = *b;
   *b = temp;
}

Classes and Objects

Introduction

  • Class Declaration
    class Book {
      public:
        string title;
        string author;
        int pages;
    };
    
  • Creating Objects
    Book book1;
    book1.title = "1984";
    book1.author = "George Orwell";
    book1.pages = 328;
    
  • Constructor Function
    class Book {
       public:
         string title;
         string author;
         int pages;
    
         Book(string aTitle, string anAuthor, int aPages) {
            title = aTitle;
            author = anAuthor;
            pages = aPages;
         }
    };
    Book book1("1984", "George Orwell", 328);
    

Object Functions

class Student {
   public:
     string name;
     double gpa;
     // Constructor
     Student(string aName, double aGPA) {
        name = aName;
        gpa = aGPA;
     }
     // Object function
     bool hasHonors() {
        if (gpa >= 3.5)
           return true;
        return false;
     }
};
Student student1("Mike", 3.7);
cout << student1.hasHonors() << endl; // Outputs 1 (true)

Getters and Setters

  • Control access to class attributes
    class Movie {
       private:
         string rating;
       public:
         string title;
         string director;
    
         // Setter
         void setRating(string aRating) {
            if(aRating == "G" || aRating == "PG" || aRating == "PG-13" || aRating == "R")
               rating = aRating;
            else
               rating = "NR";
         }
         // Getter
         string getRating() {
            return rating;
         }
    };
    Movie avengers;
    avengers.setRating("PG-13");
    cout << avengers.getRating(); // Outputs: PG-13
    

Inheritance

  • Enable one class to inherit attributes and methods of another
    class Chef {
       public:
         void makeChicken() {
            cout << "The chef makes chicken" << endl;
         }
         void makeSalad() {
            cout << "The chef makes salad" << endl;
         }
    };
    
    class ItalianChef : public Chef {
       public:
         void makePasta() {
            cout << "The chef makes pasta" << endl;
         }
    };
    
    ItalianChef ic;
    ic.makeChicken(); // Outputs: The chef makes chicken
    ic.makePasta(); // Outputs: The chef makes pasta
    

Comments

Single-line comments

// This is a single-line comment
cout << "Hello"; // Inline comment

Multi-line comments

/*
This is a multi-line comment
Comments are useful for understanding code
*/