0% found this document useful (0 votes)
106 views

PDF 6

aboutc ++

Uploaded by

sweeter
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
106 views

PDF 6

aboutc ++

Uploaded by

sweeter
Copyright
© © All Rights Reserved
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/ 19

1

The Ultimate

Part 2: Intermediate

Mosh Hamedani
Copyright 2022 Code with Mosh codewithmosh.com
codewithmosh.com

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 nd the full course on my website.

https://codewithmosh.com

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

https://twitter.com/moshhamedani

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

Copyright 2022 Code with Mosh codewithmosh.com


fi

Table of Content

Arrays………..……………………………………………………………………..…………..4

Pointers…………………………………………………………………………….…………..6

Strings………………………………………………………………………………………….9

Structures and Enumerations……………………………………………………..…….….13

Streams………………………………………………………………………………….….…16

Copyright 2022 Code with Mosh codewithmosh.com


Arrays 4

Arrays
Terms
Arrays Linear search algorithm
Array elements Pointers
Bubble sort algorithm Unpacking arrays

Summary
• An array is a collection of items stored in sequence.

• We can access the items (also called elements) in an array using an index. The index of
the rst element in an array is 0.

• An array can be unpacked into separate variables.

• Using the size() function in the C++ Standard Library (STL) we can determine the size
of an array.

• When passing an array to a function, we should always pass its size as well. The reason
for this is that array parameters are interpreted as pointers (memory addresses). We
cannot use a memory address to determine how many elements exist in an array.

• In C++, we cannot assign an array to another. To copy an array, we need to copy


individual elements using a loop.

• Similarly, arrays cannot be compared for equality. To compare two arrays, we have to
compare their individual elements.

• There are many algorithms for searching and sorting arrays. These algorithms differ
based on their performance and memory usage.

Copyright 2022 Code with Mosh codewithmosh.com


fi
Arrays 5

Copyright 2022 Code with Mosh codewithmosh.com


Pointers 6

Pointers
Terms
Address-of operator Pointer
Dereference operator Reference parameters
Free store Shared pointers
Heap memory Smart pointers
Indirection operator Stack memory
Memory leak Unique pointers
Null pointer

Summary
• Using the address-of operator (&) we can get the address of a variable.

• A pointer is a variable that holds the memory address of another variable.

• Using the indirection or dereference operator (*) we can get the content of a memory
address stored in a pointer.

• One of the applications of pointers is to ef ciently pass objects between function calls.
Reference parameters are a safer and simpler alternative for the same purpose.

• A null pointer is a pointer that doesn’t point to any objects.

• Local variables are stored in a part of memory called the stack memory. The memory
allocated to these variables is automatically released when they go out of scope.

Copyright 2022 Code with Mosh codewithmosh.com


fi
Pointers 7

• We can use the new operator to dynamically allocate memory on a different part of
memory called the heap (or free store).

• When allocating memory on the heap, we should always deallocate it using the delete
operator. If we don’t, our program’s memory usage constantly increases. This is known
as a memory leak.

• Smart pointers in the STL are the preferred way to work with pointers because they take
care of releasing the memory when they go out of scope.

• There are two types of smart pointers: unique and shared.

• A unique pointer owns the memory address it points to. So we cannot have two unique
pointers pointing to the same memory location.

• If we need multiple pointers pointing to the same memory location, we have to use
shared pointers.

Copyright 2022 Code with Mosh codewithmosh.com


Pointers 8

Copyright 2022 Code with Mosh codewithmosh.com


Strings 9

Strings
Terms
C String Escape sequences
C++ String Raw string
Character literal String literal
Concatenate Substring

Summary
• A C-string (also called C-style String) is an array of characters terminated with the null
terminator (\0).

• C-strings cannot be copied, compared, or concatenated (combined). That’s why you


should avoid them in new code and prefer to use C++ strings.

• C++ strings are represented using the string class in the STL. This class internally uses a
character array to hold a string. But it hides away all the complexity around C-strings. It
dynamically resizes the array when needed and provides useful methods for working
with strings.

• A string literal is a sequence of characters enclosed with double quotation marks.

• A character literal is a character enclosed with single quotation marks.

• We use escape sequences to represent special characters within string and character
literals. Examples of escape sequences are \\, \”, \’, \n and \t.

• In raw strings, escape sequences are not processed.

Copyright 2022 Code with Mosh codewithmosh.com


Strings 10

Copyright 2022 Code with Mosh codewithmosh.com


Strings 11

Copyright 2022 Code with Mosh codewithmosh.com


Strings 12

Copyright 2022 Code with Mosh codewithmosh.com


Structures 13

Structures
Terms
Enumerations Operator overloading
Enumerators Strongly-typed enums
Methods Structures
Nested structures Structure members

Summary
• We use structures to de ne custom data types.

• Members of a structure can be variables or functions (also called methods).

• Structures can be nested to represent more complex types.

• To compare two structures, we have to compare their individual members.

• We can provide operators for our structures using a technique called operator
overloading.

• Just like the built-in data types, structures can be used as function parameters or their
return type.

• Using an enumeration, we can group related constants into a single unit. Members of this
unit are called enumerators.

• Strongly-typed enumerations de ne a scope for their members. This allows two enums
having members with the same name.

Copyright 2022 Code with Mosh codewithmosh.com


fi
fi
Structures 14

Copyright 2022 Code with Mosh codewithmosh.com


Structures 15

Copyright 2022 Code with Mosh codewithmosh.com


Streams 16

Streams
Terms
Binary les Output streams
Buffer Streams
File streams String streams
Input streams Text les

Summary
• A stream is an abstraction for a data source or destination. Using streams, we can read
data or write it to a variety of places (eg terminal, les, network, etc) in the same way.

• In the C++ STL, we have many stream classes for different purposes. All these classes
inherit their functionality from ios_base.

• A buffer is a temporary storage in memory used for reading or writing data to streams.

• If an error occurs while reading data from a stream, the invalid data stays in the buffer
and will be used for subsequent reads. In such situations, rst we have to put the
stream into a clean state using the clear() method. Then, we should clear the data in the
buffer using the ignore() method.

• In the C++ STL, we have three stream classes for working with les. (ifstream for
reading from les, ofstream for writing to les, and fstream for reading and writing to
les).

• Binary les store data the same way it is stored in memory. They are more ef cient for
storing large amount of numeric data but they’re not human readable.

• Using string streams we can convert data to a string or vice versa.

Copyright 2022 Code with Mosh codewithmosh.com


fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Streams 17

Copyright 2022 Code with Mosh codewithmosh.com


Streams 18

Copyright 2022 Code with Mosh codewithmosh.com


Streams 19

Copyright 2022 Code with Mosh codewithmosh.com

You might also like