Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

cplusplus2

This document is a part of Mosh Hamedani's Ultimate C++ course, covering fundamental concepts of the C++ programming language. It introduces essential topics such as data types, decision making, loops, and functions, along with their definitions and usage. The document serves as a foundational guide for beginners to understand and start programming in C++.

Uploaded by

gritunicb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

cplusplus2

This document is a part of Mosh Hamedani's Ultimate C++ course, covering fundamental concepts of the C++ programming language. It introduces essential topics such as data types, decision making, loops, and functions, along with their definitions and usage. The document serves as a foundational guide for beginners to understand and start programming in C++.

Uploaded by

gritunicb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1

The Ultimate

Part 1: The Fundamentals

Mosh Hamedani

Copyright 2022 Code with Mosh codewithmo


codewithmosh.c sh.com
om
2
Hi! I am Mosh Hamedani. I’m a software engineer with over 20 years of
experience and I’ve taught millions of people how to code and become
professional software engineers through my YouTube channel and coding
school (Code with Mosh).

This PDF is part of my Ultimate C++ course where you will learn everything
you need to know from the absolute basics to more advanced concepts. You
can find the full course on my website.

https://codewithmosh.com

https://www.youtube.com/c/programmingwithmosh

https://twitter.com/moshhamedani

https://www.facebook.com/programmingwithmosh/

Table of Content

Getting Started..................................................................................................................................... 4

The Basics............................................................................................................................................ 6

Data Types............................................................................................................................................8

Decision Making.................................................................................................................................11

Loops.................................................................................................................................................. 15

Functions........................................................................................................................................... 17

Copyright 2022 Code with Mosh codewithmosh.com


3

Getting Started
Terms
C++ Standard Library Integrated Development Environment
Compiling Machine code
Console application Statement
Function Syntax
Graphical User Interface (GUI) Terminal

Summary
• C++ is one of the oldest yet most popular programming languages in the world due to its
performance and efficiency.

• It’s often used in building performance-critical applications, video games (especially with Unreal
Engine), servers, operating systems, etc.

• To learn C++, you need to learn the syntax (grammar) of the language as well as C++ Standard
Library, which is a collection of pre-written C++ code for solving common problems.

• To write C++ applications, we often use an Integrated Development Environment (IDE). The
most popular IDEs are MS Visual Studio, XCode, and CLion.

• To run C++ applications, first we have to compile our C++ code to machine code.

• The main() function is the starting point of a C++ program.

Copyright 2022 Code with Mosh codewithmosh.com


4
Your First C++ Program

Copyright 2022 Code with Mosh codewithmosh.com


5
The Basics

The Basics
Terms
Camel case Operator
Comment Pascal case
Constant Snake case
Directive Standard input stream
Expression Standard output stream
Hungarian notation Stream extraction operator
Mathematical expression Stream insertion operator
Operand Variable
Summary
• We use variables to temporarily store data in the computer’s memory.

• To declare a variable, we should specify its type and give it a meaningful name.

• We should initialize variables before using them. Using an uninitialized variable can lead to
unexpected behavior in our programs since these variables hold garbage values.

• Unlike variables, the value of constants don’t change.

• The common naming conventions used in C++ applications are: PascalCase, camelCase, and
snake_case.

• An expression is a piece of code that produces a value. A mathematical (arithmetic) expression


consists of an operator (+, -, *, /, %) and two operands.

Copyright 2022 Code with Mosh codewithmosh.com


6
• Multiplication and division operators have a higher priority than addition and subtraction
operators. So, they’re applied first.

The Basics

• We can use parentheses to change the order of operators.

• We use cout (pronounced sea-out) to write characters to the Standard Output Stream which
represents the terminal or console window.

• We use cin (pronounced sea-in) to read data from the Standard Input Stream which represents
the keyboard.

• We use the Stream Insertion Operator (<<) to write data to a stream.

• We use the Stream Extraction operator (>>) to read data from a stream.

• The Standard Template Library (STL) consists of several files each containing functions for
different purposes.

• To use functions in the Standard Library, we should include the corresponding files using the
#include directive.

• Using comments we can explain what cannot be expressed in code. This includes why’s, how’s,
and any assumptions we made while writing code.

Copyright 2022 Code with Mosh codewithmosh.com


7

Copyright 2022 Code with Mosh codewithmosh.com


Fundamental Data Types 8

Data Types
Terms
Array Floating-point number
Binary system Hexadecimal system
Boolean values Index
Casting Overflow
Characters Run-time error
Compile-time error Stream manipulator
Data type String
Decimal system Underflow
Summary
• C++ has several built-in data types for storing integers (whole numbers), floating-point numbers
(numbers with a decimal point), characters, and Boolean values (true/false).

• Floating-point numbers are interpreted as double by default. To represent a float, we have to


add the F suffix to our numbers (eg 1.2F).

• Whole numbers are interpreted as int by default. To represent a long, we have to use the L
suffix (eg 10L).

• Using the auto keyword, we can let the compiler infer the type of a variable based on its initial
value.

• Numbers can be represented using the decimal, binary, and hexadecimal systems.

• If we store a value larger or smaller than a data type’s limits, overflowing or underflowing
occurs.

• Using the sizeof() function, we can see the number of bytes taken by a data type.

Copyright 2022 Code with Mosh codewithmosh.com


Fundamental Data Types 9

• We can use stream manipulators to format data sent to a stream. The most common
manipulators are setw, fixed, setprecision, boolalpha, left, and right.

• The Boolean false is represented as 0. Any non-zero number is interpreted as the Boolean true.

• In C++, characters should be surrounded with single quotes.

• Characters are internally represented as numbers.

• A string is a sequence of characters and should be surrounded by double quotes.

• We use arrays to store a sequence of items (eg numbers, characters, etc).

• Array elements can be accessed using an index. The index of the first element in an array is 0.

• When we store a smaller value in a larger data type, the value gets automatically cast
(converted to) the larger type. When storing a large value in a smaller data type, we have to
explicit cast the value.

• C-style casting involves prefixing a variable with the target data type in parentheses. In C++, we
use the static_cast operator.

• C++ casting is safer because conversion problems can be caught at the compile-time. With C-
style casting, we won’t know about conversion issues until the run-time.

Copyright 2022 Code with Mosh codewithmosh.com


Fundamental Data Types 10

Copyright 2022 Code with Mosh codewithmosh.com


Decision Making 11

Decision Making
Terms
Boolean expression
Comparison operators
Conditional operator
If statement
Logical operators
Nesting if statements
Switch statement

Summary
• We use comparison operators to compare values.

• A Boolean expression is a piece of code that produces a Boolean value.

• With Logical operators we can combine Boolean expressions and represent more complex
conditions.

• With the logical AND operator (&&) both operands should be true. If either of them is false, the
result will be false.

• With the logical OR operator (||), the result is true if either of the operands is true.

• The logical NOT operator (!) reverses a Boolean value.

• Using if and switch statements, we can control the logic of our programs.

Copyright 2022 Code with Mosh codewithmosh.com


Decision Making 12

• An if statement can have zero or more else if clauses for evaluating additional conditions.

• An if statement can optionally have an else clause.

• We can code an if statement within another. This is called nesting if statements.

• Using the conditional operator we can simplify many of the if/else statements.

• We use switch statements to compare a variable against different values.

• A switch block often has two or more case labels and optionally a default label.

• Case labels should be terminated with a break statement; otherwise, the control moves to the
following case label.

• Switch statements are not as flexible as if statements but sometimes they can make our code
easier to read.

Copyright 2022 Code with Mosh codewithmosh.com


Decision Making 13

Copyright 2022 Code with Mosh codewithmosh.com


Decision Making 14

Copyright 2022 Code with Mosh codewithmosh.com


15
Loops

Loops
Terms
Break statement Iteration
Continue statement Loops
Do-while statement Loop variable
For statement Nested loop
Infinite loop While statement
Summary
• We use loops to repeat a set of statements.

• In C++, we have four types of loops: for loops, range-based for loops, while loops, and do-
while loops.

• For loops are useful when we know ahead of time how many times we want to repeat
something.

• Range-based for loops are useful when iterating over a list of items (eg an array or a string).

• While and do-while loops are often used when we don’t know ahead of time how many times
we need to repeat something.

• Using the break statement we can break out of a loop.

• Using the continue statement we can skip an iteration.

Copyright 2022 Code with Mosh codewithmosh.com


16
Loops

Functions
Terms
Debugging Function signature

Copyright 2022 Code with Mosh codewithmosh.com


Functions 17
Functions Global variables
Function arguments Invoking a function
Function declaration Header files
Function definition Local variables
Function parameters Namespaces
Function prototype Overloading functions
Summary
• A function is a group of one or more statements that perform a task. Each function should have
a clear responsibility. It should do one and only one thing.

• A function can have zero or more parameters

• Arguments are the values passed to a function.

• To call (or invoke) a function, we type its name, followed by parenthesis, and the arguments (if
any).

• Function parameters can have a default value. This way, we don’t have to provide arguments
for them.

• The signature of a function includes the function name, and the number, order, and type of
parameters.

Copyright 2022 Code with Mosh codewithmosh.com


Functions 18

• Overloading a function means creating another variation with a different signature. By


overloading functions, we can call our functions in different ways.

• Arguments of a function can be passed by value or reference. When passed by value, they get
copied to the parameters of the function.

• To pass an argument by a reference, we should add an & after the parameter type.

• Local variables are only accessible within the function in which they are defined. Global
variables are accessible to all functions.

• Global variables can lead to hard-to-detect bugs and should be avoided as much as possible.

• A function declaration (also called a function prototype) tells the compiler about the existence
of a function with a given signature. A function definition (or implementation) provides the
actual body (or code) for the function.

• As our programs grow in more complexity, it becomes critical to split our code into separate
files.

• A header file ends with “.h” or “.hpp” extension and consists of function declarations and
constants. We can import header files using the #include directive.

• An implementation file ends with “.cpp” extension and consists of function definitions.

• Using namespaces we can prevent name collisions in our programs.

• Debugging is a technique for executing a program line by line and identifying potential errors.

Copyright 2022 Code with Mosh codewithmosh.com


Functions 19

Holy

Copyright 2022 Code with Mosh codewithmosh.com


Functions 20

Copyright 2022 Code with Mosh codewithmosh.com

You might also like