C++ Examples
C++ Syntax
Syntax ExplainedC++ Output/Print
Use cout to output values/print text
Using many cout objects
Insert a new line with \n
Insert a new line with endl
Output Explained
C++ Comments
Single-line comment before a line of code
Single-line comment at the end of a line of code
Multi-line comment
Comments Explained
C++ Variables
Create an integer variable
Create a variable without assigning the value, and assign the value later
Assign a new value to an existing value (this will overwrite the previous value)
Create an unchangeable variable with the const keyword
Combine text and a variable on print
Add a variable to another variable
Declare many variables of the same type with a comma-separated list
Identifiers
Variables Explained
C++ User Input
User Input ExplainedC++ Data Types
A demonstration of different data types
Create an int type
Create a float type
Create a double type
Create boolean types
Create a char type
Create a string type
Data Types Explained
C++ Operators
Operators ExplainedC++ Strings
Create a string
String concatenation
String length
Access string characters
Change string characters
User input strings
Strings Explained
C++ Math
Find the highest value of two numbers
Find the lowest value of two numbers
Use the cmath header file for other math functions
Math Explained
C++ Booleans
Booleans ExplainedC++ If...Else (Conditions)
If...Else ExplainedC++ Switch
Switch ExplainedC++ Loops
Loops ExplainedC++ Arrays
Create and access an array
Change an array element
Loop through an array
Get the size/length of an array
Multidimensional arrays
Arrays Explained
C++ References
References ExplainedC++ Pointers
Create a pointer variable
Get the value of a variable with the dereference operator *
Modify the pointer value
Pointers Explained
C++ Files
Files ExplainedC++ Functions
Create and call a function
Call a function multiple times
Function declaration and definition
Parameters and arguments
Default parameter value
Multiple parameters
Return value
Return the sum of two parameters
Pass by reference
Pass an array to a function
Function overloading
Functions Explained
C++ Classes/Objects
Create an object of a class and access class attributes
Create multiple objects
Create class methods
Define a class method outside the class definition
Add parameters to a class method
Create a constructor
Constructor parameters
Constructor defined outside the class
Public and private specifiers
Encapsulation - hide sensitive data from users
Inheritance - inherit attributes and methods from one class to another
Multilevel inheritance
Multiple inheritance
Polymorphism - perform a single action in different ways
Files - Create, write and read a file
Exceptions - Handle errors
Classes/Objects Explained