Ict

Page 1: Introduction to C++ Programming

Overview

  • C++ is a popular programming language.

  • Widely used in game development.

  • Developed as an extension of C with similar syntax.

Differences Between C and C++

  • C++ supports classes and objects.

  • C does not have these features.


Page 2: Getting Started with C++

Requirements

  • Text Editor: e.g., Notepad to write code.

  • Compiler: e.g., GCC to translate the code.

Example C++ Syntax

  • Line 1: #include <iostream>

  • Line 2: using namespace std;

  • Line 3: int main()

  • Line 4: cout << "Hello world!";

  • Line 5: return 0;


Page 3: Data Types and Variables in C++

Common Data Types

  • Integer: whole numbers.

  • Float: decimal numbers (e.g., 5.99).

  • Double: larger decimal values (e.g., 9.988).

  • Char: single character (e.g., 'A').

  • String: sequence of characters.

  • Boolean: True/False values.

Declaring Variables

  • Syntax: DataType variableName = value;

  • Example: int gradeLevel = 8;

Historical Context

  • Developed by Bjarne Stroustrup as an extension to C.

  • Updated frequently in 2011, 2014, 2017, 2020, and 2023.


Page 4: Advantages of Using C++

Key Benefits

  • Fun and easy to learn.

  • Highly popular programming language.

  • Used in graphical user interfaces and embedded systems.

Syntax Example

  • Example: #include <iostream>

  • Line to replace CH syntax with std for using standard objects.


Page 5: Using an IDE for C++

Definition

  • IDE: Integrated Development Environment for editing and compiling code.

  • Common Syntax:

    • #include <iostream>: header for input/output objects.

    • using namespace std;: allows use of standard library names.

Code Structure

  • White spaces for readability are ignored by C++.

  • int main() defines the main function that executes the code inside.

  • cout: object to output text.

  • return 0;: ends the main function.


Page 6: C++ Statements

Definition

  • A program is a list of instructions to be executed by the computer.

  • These instructions are called statements in programming languages.

Outputting Text

  • Use cout with the << operator to output text enclosed in quotes.

    • Example: cout << "Hello world!";

Comments in C++

  • Single Line: // This is a comment.

  • Multi-line: /* This is a multi-line comment. */


Page 7: Constants and Identifiers in C++

Constants

  • Declare constant variables using const.

    • Example: const int myNum = 15;

Identifiers

  • Variable names must be unique.

Naming Rules

  • Can contain letters, digits, and underscores.

  • Must begin with a letter or underscore.

  • Case-sensitive.

  • Cannot contain white spaces or special characters.

  • Reserved words (keywords) cannot be used as identifiers.


Page 8: User Input in C++

Input/Output Objects

  • cout: outputs/prints values.

  • cin: gets user input.

  • cout uses the extraction operator (<<).

  • cin uses the input operator (>>).

Example Usage

  • Complete example showing usage of both cout and cin.