💻

Introduction to C++ Programming Basics

Aug 21, 2024

Lecture Notes: Computer Programming 1 - Introduction to C++

Introduction

  • Good morning students of IDE 61.2
  • Instructor: Vida
  • Welcome to the virtual class

Previous Modules Recap

Essential Components of Programming Environment

  1. Operating System
    • System software that manages computer hardware and software resources.
    • Provides common services for computer programs.
  2. Integrated Development Environment (IDE)
    • Software application providing comprehensive facilities for software development.
    • Comprises:
      • Source code editor
      • Build customization tools
      • Debugger
  3. Compiler
    • Translates code written in a programming language into machine language.
  4. Revision Control (Version Control)
    • Manages changes made over time in source code.

Configuring Work Environment

  • Create a GitHub account
  • Download Visual Studio Code (includes C++ extension)
  • Install MinGW Installation Manager and other components

Introduction to C++

Overview

  • C++ is a popular, cross-platform programming language.
  • Developed by Bjarne Stroustrup as an extension of C language.
  • C was developed by Dennis Ritchie and Brian Kernighan in 1972.
  • C++ provides high-level control over system resources and memory.
  • Updated versions: C++11, C++14, C++17

Why Study C++?

  • One of the world's most popular programming languages.
  • Used in operating systems, GUIs, and embedded systems.
  • Object-oriented, allowing code reuse and lower development costs.
  • Portable across multiple platforms.
  • Easy to learn and similar to C# and Java.

C++ Environment Setup

  • Need a text editor and a compiler to start coding.
  • Example C++ Program: #include <iostream> using namespace std; int main() { cout << "Hello World"; return 0; }

Code Explanation

  • Line 1: #include <iostream> - includes header file for input/output.
  • Line 2: using namespace std; - allows usage of standard library objects.
  • Line 4: int main() - signifies the main function.
  • Line 5: cout << "Hello World"; - outputs "Hello World".
  • Line 6: return 0; - ends the main function.

Notes for Executing C++ Programs

  • Every statement ends with a semicolon.
  • White spaces and comments are ignored by the compiler.
  • Use multiple lines for readability.
  • The cout object with << does not insert a new line unless specified with .

Comments in C++

  • Single Line Comments: Start with //.
  • Multi-line Comments: Start with /* and end with */.

Variables in C++

  • Containers for storing data values.

Types of Variables

  1. int - stores integers.
  2. double - stores floating-point numbers.
  3. char - stores single characters.
  4. string - stores text.
  5. bool - stores true/false values.

Variable Declaration

  • Syntax: type variable = value;
  • Example: int myNum = 15; cout << myNum;

Identifiers in C++

  • Must be unique names for variables.
  • Can include letters, digits, and underscores.
  • Case-sensitive (e.g., myVar vs myvar).

Constants in C++

  • Values that cannot be changed during execution.
  • Use the const keyword.
  • Example: const int MINUTES_PER_HOUR = 60;

User Input in C++

  • Use cin to get user inputs.
  • Example: int x; cout << "Type a number:"; cin >> x;

  • This class covered essential basics of C++ programming, including syntax, variables, and user input methods.