محاضرة 1
Introduction to C++ and Basic Structure
البرنامج الأول (Hello World)، المتغيرات، والطباعة (cout)
ملخص المحاضرة
📜 Lecture 1: Programming Essentials in C++
This lecture introduces the C++ language, its setup, and the basic structure of a program.
Key Concepts
- What is C++?: It's a high-level, general-purpose programming language. It was developed as an enhancement of the C language to include Object-Oriented Programming (OOP).
- Why C++?: It's powerful, supports OOP, and allows for low-level memory access.
- Compilation: The process of converting Source Code (human-readable, e.g.,
main.cpp) into Machine Code (computer-readable, e.g., 1s and 0s) using a compiler. - Basic Program Structure:
#include <iostream>: A preprocessor directive that includes the standard library for input/output operations (likecout).using namespace std;: Tells the compiler to use the "standard" namespace, which avoids having to writestd::cout.int main(): The main function. Program execution always begins here.cout << "Hello world!";: The standard output command.cout(see-out) prints the string to the console.return 0;: Indicates that the program finished successfully.;(Semicolon): The statement terminator. It ends a command.
- Comments: Notes for humans, ignored by the compiler.
- Single-line:
// This is a comment. - Multi-line:
/* This is a comment */.
- Single-line:
- New Lines: To format output.
\n: The "newline" escape character, used inside a string.endl: A command (end-line) used withcout.
- Variables & Data Types:
int: Integers (e.g., 15).float/double: Fractional numbers (e.g., 2.3).doublehas more precision.string: Text (e.g., "Mona").char: A single character (e.g., 'A').bool:trueorfalse(outputs as 1 or 0).
#include <iostream> // Required for std::cout and std::endl #include <string> // Required for std::string int main() { // Printing a simple string literal std::cout << "Hello, C++!" << std::endl; // Printing the value of an integer variable int age = 30; std::cout << "My age is: " << age << std::endl; // Printing the value of a string variable std::string name = "Alice"; std::cout << "My name is: " << name << std::endl; // Chaining multiple outputs in a single statement double temperature = 25.5; std::cout << "Today's temperature is " << temperature << " degrees Celsius." << std::endl; return 0; // Indicates successful program execution }