Understanding C++ Programming Basics

Sep 12, 2024

C++ Programming Lecture Notes

Introduction to C++

  • Fast language, commonly used in:
    • Advanced graphics applications (Adobe, video editing software)
    • Embedded systems
    • Video game development

Levels of Programming Languages

  • Middle-level language: C++
  • Higher-level languages (Python, Java, C#): Easier to write and understand, but slower
  • Lower-level languages (C, C++): Closer to hardware instructions, faster but harder to write

Getting Started with C++

Tools Needed

  1. Text Editor/IDE:
    • Options: VS Code, Code::Blocks, Notepad
  2. Compiler:
    • GCC for Windows/Linux
    • Clang for Mac

Installing VS Code

  • Visit: code.visualstudio.com
  • Download appropriate version
  • Install extensions for C++ and Code Runner

Writing Your First Program

  • Create a new file (e.g., hello_world.cpp)
  • Include header file: #include <iostream>
  • Write main function:
    int main() {
        std::cout << "Hello, World!" << std::endl;
        return 0;
    }
    

Variables in C++

Declaring Variables

  • Data Types:
    • int for integers
    • double for decimals
    • char for characters
    • bool for boolean values
    • string for sequences of characters

Variable Scope

  • Local Variables: Declared inside functions
  • Global Variables: Declared outside all functions

Control Structures

If Statements

  • Basic syntax:
    if (condition) {
        // code to execute if condition is true
    }
    

Switch Statements

  • Alternative to multiple if statements
  • Example:
    switch (variable) {
        case 1:
            // code
            break;
        default:
            // code
    }
    

Functions in C++

Basic Function Structure

  • Return type, name, parameters
  • Example:
    int add(int a, int b) {
        return a + b;
    }
    

Function Overloading

  • Multiple functions with the same name but different parameters.

Object-Oriented Programming

Classes and Objects

  • Class: Blueprint for creating objects
  • Object: Instance of a class

Constructors

  • Special method automatically called when an object is instantiated
  • Can set default values for attributes

Inheritance

  • Child class inherits attributes and methods from a parent class
  • Promotes code reusability

Structs

  • Group related variables under one name
  • Supports multiple data types

Pointers

  • Variable that stores memory address of another variable
  • Use & to get address, * to dereference

Dynamic Memory

  • Memory allocated during runtime
  • Uses new and delete

Templates

  • Allow designing functions that work with any data type
  • Syntax:
    template <typename T>
    T max(T a, T b) {
        return (a > b) ? a : b;
    }
    

Summary

  • C++ is a powerful language that allows for various programming paradigms including procedural, object-oriented, and generic programming. Understanding structures, functions, and classes will enable effective coding practices.