C++ Programming Basics

Jul 13, 2024

Lecture: C++ Programming Basics

Introduction to C++

  • C++ is a fast and efficient language.
  • Used in advanced graphics applications (e.g., Adobe software, video editing).
  • Commonly used for embedded systems and video game development.
  • Middle-level language: closer to hardware than high-level languages (e.g., Python, Java), but still resembles human language.
  • Steeper learning curve than higher-level languages, but it's worthwhile.

Getting Started with C++

  • Tools Needed:
    • Text Editor: Examples include VS Code, Code Blocks, and Notepad.
      • VS Code and Code Blocks are IDEs with integrated developer tools.
    • Compiler: Converts source code to machine instructions.
      • Windows/Linux: GCC
      • Mac: Clang

Setting Up VS Code

  1. Download VS Code from code.visualstudio.com.
  2. Install and set it up with default settings.
  3. Recommended extensions for C++ development:
    • C/C++ extension
    • Code Runner extension
  4. Create a new folder for C++ projects.
  5. Create a new file (e.g., hello_world.cpp) in VS Code.

Installing a Compiler

  • Linux:
    • Check if GCC is installed: gcc -v
    • Install GNU Compiler Tools: sudo apt-get install build-essential
  • Mac:
    • Install XCode Command Line Tools: xcode-select --install
  • Windows: (More Steps)
    • Download MinGW-w64 and follow installation instructions.

Writing and Running a Basic C++ Program

  1. Include necessary headers and set up the main function:
    #include <iostream>
    
    int main() {
      std::cout << "Hello, world!" << std::endl;
      return 0;
    }
    
  2. Run the program using the Code Runner extension.

Working with Output in C++

  • Using iostream for basic input and output operations.
  • std::cout for output to the console.
  • Use \n or std::endl for new lines.
  • Using comments for notes:
    • Single-line comments with //
    • Multi-line comments with /* */

Variables and Data Types

  • Declaration and assignment:
    • Syntax: datatype variableName = value;
    • Example: int x = 5;
  • Common data types:
    • int: integers
    • double: floating-point numbers
    • char: single characters
    • bool: boolean values (true/false)
    • string: sequences of text (requires #include <string>)

Basic C++ Syntax and Control Structures

  • If Statements: Used for decision-making
  • Switch Statements: Alternative to many else if statements
  • Loops:
    • For loops: Execute a block of code a specific number of times
    • While loops: Repeat code as long as a condition is true
    • Do-While loops: Similar to while loops, but the code block is executed at least once

Functions in C++

  • Definition:
    returnType functionName(parameters) {
      // code
    }
    
  • Example:
    void printHello() {
      std::cout << "Hello!" << std::endl;
    }
    
  • Function Prototypes: Declare functions before the main function.
  • Parameter Passing:
    • Pass by Value: Copies the argument's value (default).
    • Pass by Reference: Passes the argument's address (&).
    • Const Parameters: Use const to make parameters read-only.

C++ Arrays

  • Declaration: datatype arrayName[arraySize];
  • Initialization:
    int numbers[5] = {1, 2, 3, 4, 5};
    
  • Accessing Elements: Use index notation arrayName[index] starting from 0.
  • Multi-dimensional Arrays: Arrays within arrays.
    • Example: int matrix[3][3];

Pointers

  • Definition: Variables that store memory addresses.
  • Syntax: datatype* pointerName;
  • Example:
    int x = 5;
    int* p = &x; // p points to x
    
  • Dereferencing: Access the value at the pointer's address using *.

Dynamic Memory Allocation

  • Using new to allocate memory in the heap:
    int* p = new int;
    *p = 10;
    
  • Deallocating memory with delete:
    delete p;
    
  • Example with arrays:
    int* arr = new int[10];
    delete[] arr;
    

C++ Classes and Object-Oriented Programming

  • Classes: Blueprints for creating objects.
  • Attributes and Methods: Characteristics and actions of the class.
  • Syntax:
    class ClassName {
      public:
        datatype attribute;
        returnType methodName(parameters);
    };
    
  • Example:
    class Dog {
      public:
        int age;
        void bark() {
          std::cout << "Woof!" << std::endl;
        }
    };
    
  • Constructors: Special methods called when an object is instantiated.
  • Inheritance: A class can inherit attributes and methods from another class.
  • Polymorphism: Methods that behave differently based on the object.

Example Programs

  • Simple Calculator
  • Tic-Tac-Toe Game
  • Number Guessing Game

Summary

  • C++ is a versatile and powerful language useful for a variety of applications.
  • Mastering the basics such as variables, control structures, arrays, pointers, and object-oriented programming is crucial.
  • Practice through projects and exercises to solidify understanding.