100% found this document useful (1 vote)
85 views26 pages

C++: From Darkness To Dawn

C++: From Darkness to Dawn A Beginner’s Journey Through Code, Logic, and Light C++: From Darkness to Dawn is your ultimate guide to transforming from a complete beginner into a confident C++ programmer. Whether you're starting with zero knowledge or looking to strengthen your foundation, this book walks you step by step out of the confusion (“darkness”) and into clarity and skill (“dawn”). By Souhail Laghchim.

Uploaded by

Souhail Laghchim
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
85 views26 pages

C++: From Darkness To Dawn

C++: From Darkness to Dawn A Beginner’s Journey Through Code, Logic, and Light C++: From Darkness to Dawn is your ultimate guide to transforming from a complete beginner into a confident C++ programmer. Whether you're starting with zero knowledge or looking to strengthen your foundation, this book walks you step by step out of the confusion (“darkness”) and into clarity and skill (“dawn”). By Souhail Laghchim.

Uploaded by

Souhail Laghchim
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction:

What is C++?
C++ is a powerful general-purpose programming language. It is an extension of the C
language and supports both procedural and object-oriented programming.

Why Learn C++?


 It’s fast and efficient.
 Used in game development, system software, embedded systems, and more.
 Teaches you how computers work at a deeper level (memory management, pointers).
 Used in many competitive programming contests.

Basic Structure of a C++ Program:


Here’s a simple "Hello, World!" example:

C++:

 #include <iostream> // Includes input-output library

 using namespace std;

 int main() {
 cout << "Hello, World!" << endl; // Prints to the console
 return 0;
 }
Explanation:
 #include <iostream>: Includes the standard input/output library.
 using namespace std;: So we don’t have to write std:: every time.
 int main(): The main function where the program starts.
 cout: Used to display output.
 return 0;: Ends the program.

Key Features of C++:


 Object-Oriented (Classes, Inheritance, etc.)
 Low-level memory manipulation (pointers)
 Rich Standard Library (STL: vectors, maps, etc.)
 Portable and cross-platform

Run C++ Code Online (Easy & Quick)

Use an Online Compiler:


No installation needed. Just write and run your code in the browser.

Popular sites:

 https://www.programiz.com/cpp-programming/online-compiler
 https://www.onlinegdb.com/online_c++_compiler
 https://www.tutorialspoint.com/compile_cpp_online.php

How to Use:
1. Open the link.
2. Paste or write your C++ code.
3. Click Run or Execute.
4. See the output below the editor.
Run C++ Locally (More Control & Power)

For Windows:
1. Install a Compiler:
o Download Code::Blocks (with MinGW):
https://www.codeblocks.org/downloads
o Or use Dev-C++ or Visual Studio (Community Edition).

2. Write Code:
o Open the IDE, create a new project or file.
o Write your code, then Build and Run.

For macOS:
1. Install Xcode Command Line Tools:

Bash:

 xcode-select --install

2. Compile your code using Terminal:

Bash:

 g++ yourfile.cpp -o yourprogram


 ./yourprogram
For Linux (Ubuntu):
1. Install g++ if not already installed:

Bash:

 sudo apt update


 sudo apt install g++

2. Compile and Run:

Bash:

 g++ yourfile.cpp -o yourprogram


 ./yourprogram
What is a Variable?
A variable is a container used to store data like numbers, characters, or text.

In C++, you must:

1. Declare the variable (specify its type).


2. Initialize it (assign a value).

Example 1: Integer Variable


C++:

 #include <iostream>
 using namespace std;
 int main() {
 int age = 25; // integer variable
 cout << "Age: " << age << endl;
 return 0;
 }
Example 2: Float (Decimal) Variable
C++:

 #include <iostream>
 using namespace std;

 int main() {

 float price = 99.99; // float variable


 cout << "Price: " << price << endl;
 return 0;
 }

Example 3: Character Variable


C++:

 #include <iostream>
 using namespace std;

 int main() {

 char grade = 'A'; // character variable


 cout << "Grade: " << grade << endl;
 return 0;
 }
Example 4: String (Text)
C++:

 #include <iostream>
 #include <string> // Required for string
 using namespace std;

 int main() {

 string name = "Souhail"; // string variable


 cout << "Name: " << name << endl;
 return 0;
 }

Example 5: Change Variable Value


C++:

 #include <iostream>
 using namespace std;

 int main() {
 int score = 10;
 score = 20; // update value
 cout << "Score: " << score << endl;
 return 0;
 }
What are Data Types?
Data types tell the compiler what kind of data a variable will store.

Each variable must be declared with a data type like int, float, char, etc.

Basic Data Types in C++

Data Type Example Value Description

int 42 Stores whole numbers

float 3.14 Stores decimal numbers (single precision)

double 3.141592 Stores decimal numbers (more precision)

char 'A' Stores a single character

bool true / false Stores Boolean values

string "Hello" Stores text (requires #include <string>)

Integer (int)
C++:

 int age = 25;

Stores whole numbers like 10, -5, 0.

Floating-point (float, double)


C++:

 float price = 99.99;


 double pi = 3.1415926535;

 float is for smaller decimal numbers.


 double is for higher precision.
Character (char)
C++:

 char grade = 'A';

Stores a single character inside single quotes ' '.

String (string)
C++:

 #include <string>

 string name = "Souhail";

Used to store text. Requires #include <string>.

Boolean (bool)
C++:

 bool isOnline = true;

Can be true or false.


Example: Using All Data Types
C++:

 #include <iostream>
 #include <string>
 using namespace std;

 int main() {

 int age = 30;


 float height = 5.9;
 double pi = 3.14159;
 char grade = 'A';
 bool isStudent = true;
 string name = "Souhail";

 cout << "Name: " << name << endl;


 cout << "Age: " << age << endl;
 cout << "Height: " << height << endl;
 cout << "PI: " << pi << endl;
 cout << "Grade: " << grade << endl;
 cout << "Is Student: " << isStudent << endl;

 return 0;
 }
What Are Conditional Statements?
Conditional statements are used to make decisions in a program.

They help you run specific code only when certain conditions are true.

Types of Conditional Statements in C++


1. if statement
2. if...else statement
3. else if ladder
4. switch statement

if Statement
C++:

 #include <iostream>

 int main() {

 int age = 20;


 if (age >= 18) {
 std::cout << "You are an adult." << std::endl;
 }
 return 0;
 }

If the condition is true, the block runs.


if...else Statement
C++:

 #include <iostream>

 int main() {
 int age = 15;
 if (age >= 18) {
 std::cout << "You are an adult." << std::endl;
 } else {
 std::cout << "You are a minor." << std::endl;
 }
 return 0;
 }

Runs one block if true, another if false.


else if Ladder
C++:

 #include <iostream>

 int main() {
 int score = 75;

 if (score >= 90) {
 std::cout << "Grade: A" << std::endl;
 } else if (score >= 80) {
 std::cout << "Grade: B" << std::endl;
 } else if (score >= 70) {
 std::cout << "Grade: C" << std::endl;
 } else {
 std::cout << "Grade: F" << std::endl;
 }
 return 0;
 }

Checks multiple conditions in order.


switch Statement
C++:

 int #include <iostream>

 int main() {
 int day = 3;
 switch (day) {
 case 1:
 std::cout << "Monday";
 break;
 case 2:
 std::cout << "Tuesday";
 break;
 case 3:
 std::cout << "Wednesday";
 break;
 default:
 std::cout << "Invalid day";
 }
 return 0;
 }

Best for checking exact values, like menu options or days.


Comparison Operators
Symbol Meaning Example (a = 5, b = 3)
== Equal to a == b → false
!= Not equal to a != b → true
> Greater than a > b → true
< Less than a < b → false
>= Greater or equal a >= b → true
<= Less or equal a <= b → false

Logical Operators

Operator Meaning Example

&& AND (both true) a > 0 && b > 0

` `

! NOT (reverse result) !(a > b)


What Are Loops?
Loops let you run a block of code multiple times until a condition is false.

Types of Loops in C++

Loop Type Description

for loop Repeats a known number of times

while loop Repeats while a condition is true

do...while Same as while, but runs at least once

for Loop
Used when you know how many times to repeat.

C++:

 #include <iostream>
 int main() {
 for (int i = 1; i <= 5; i++) {
 std::cout << "Count: " << i << std::endl;
 }
 return 0;
 }
while Loop
Used when you don’t know exactly how many times to loop in advance.

C++:

 #include <iostream>
 int main() {
 int i = 1;
 while (i <= 5) {
 std::cout << "Count: " << i << std::endl;
 i++;
 }
 return 0;
 }
do...while Loop
Runs the code at least once, then checks the condition.

C++:

 #include <iostream>

 int main() {
 int i = 1;
 do {
 std::cout << "Count: " << i << std::endl;
 i++;

 } while (i <= 5);


 return 0;
 }

Loop Control Statements


Statement Purpose

break Stops the loop immediately

continue Skips current iteration, continues next one


Example: break
C++:

 #include <iostream>
 int main() {
 for (int i = 1; i <= 10; i++) {
 if (i == 5) break;
 std::cout << i << " ";
 }
 return 0;
 }
Example: continue
C++:

 #include <iostream>
 int main() {
 for (int i = 1; i <= 5; i++) {
 if (i == 3) continue;
 std::cout << i << " ";
 }
 return 0;
 }

Summary
Loop Type Best Use Case
for Fixed number of repetitions
while Unknown repetitions (check before run)
do...while Must run at least once
What Is a Function?
A function is a block of code that performs a specific task.
Instead of writing the same code again and again, you write it once in a function and call it
when needed.

Function Structure in C++


C++:

 return_type function_name(parameters) {
 // code to execute
 }

Then, you call the function like this:

C++:

 function_name(arguments);
Example 1: Simple Function (No Parameters)
C++:

 #include <iostream>
 using namespace std;

 // Function definition
 void greet() {
 cout << "Hello, Souhail!" << endl;

 }
 int main() {
 greet(); // Function call
 return 0;
 }
Example 2: Function with Parameters
C++:

 #include <iostream>
 using namespace std;

 void greet(string name) {


 cout << "Hello, " << name << "!" << endl;
 }
 int main() {
 greet("Souhail");
 greet("Ali");
 return 0;
 }
Example 3: Function with Return Value
C++:

 #include <iostream>
 using namespace std;

 // Adds two numbers and returns the result

 int add(int a, int b) {


 return a + b;
 }
 int main() {
 int sum = add(5, 3);
 cout << "Sum: " << sum << endl;
 return 0;
 }
Why Use Functions?
 Avoid code repetition
 Make your code easier to read and maintain
 Break problems into smaller pieces (modular code)

Summary of Function Components


Part Example Description

Return type int, void, float, etc. What the function gives back

Function name add, greet, etc. Your function's identifier

Parameters (int a, int b) Inputs to the function

Function body { ... } Code that runs

Return statement return a + b; Sends value back to caller

Email:
[email protected]

Blogger:
https://souhaillaghchimdev.blogspot.com/

You might also like