Please enable JavaScript.
Coggle requires JavaScript to display documents.
C++ Fundamentals (Merry and Radi), C++ Functions, C++ Classes - Coggle…
C++ Fundamentals (Merry and Radi)
Data Types
string
Stores sequence of characters
int
Stores whole numbers, without decimal
boolean
Stores true or false values
The data type takes the values true(1) or false(0)
float
Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
double
Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
char
Stores a single character/letter/number, or ASCII values
Functions
Definition: a block of code which only runs when it is called
Syntax ex: "void myFunction() {}"
To call a function-in the main function type "NameofFunction(); "
Function Parameters
syntax: "void functionName(Parameter1, par 2, par 3){code}"
Conditions
<
less than
<=
less than or equal to
">"
greater than
">="
greater than or equal to
==
equal to
!=
not equal to
if function
used to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Comments
use "//" for single line
for multiple lines- begin with "/
" and finish with "
/"
User Input
Using "cin>>"
Operators
Operators are used to perform operations on variables and values
/ (Division)
Divides one value by another
% (Modulus)
Returns the division remainder
(Multiplication)
Multiplies two values
++ (Increment)
Increases the value of a variable by 1
(Subtraction)
Subtracts one value from another
-- (Decrement)
Decreases the value of a variable by 1
(Addition)
Adds together two values
Output
Using "cout<<"
Pointers
a variable that stores the memory address as its value
points to a data type (like int or string) of the same type, and is created with the * operator. The address of the variable you're working with is assigned to the pointer
Classes and Objects
Class
a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects
To create a class, use the "class" keyword
Object
an object is created from a class
To create an object of a class, specify the class name, followed by the object name
Loops
For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop
While Loop
The while loop loops through a block of code as long as a specified condition is true
Loops can execute a block of code as long as a specified condition is reached
Loops are handy because they save time, reduce errors, and they make code more readable
Syntax
Header file library(first line of code): "#include <iostream>"
To use the standard library(2nd line): "using namespace std;"
Break line
Main Function: "int main(){code/ return 0;}"
Strings
A string variable contains a collection of characters surrounded by double quotes
To use strings, you must include an additional header file in the source code, the <string> library
Math
fmin(x, y)
Returns the lowest value of a floating x and y
pow(x, y)
Returns the value of x to the power of y
fmax(x, y)
Returns the highest value of a floating x and y
sqrt(x)
Returns the square root of a number
abs(x)
Returns the absolute value of x
Switch
"switch(expressions){}" statement-used to select one of many code blocks to be executed by using "case" and for the end "break"
Break
"break"-used to "jump out" of a switch statement
Constructors
a special method that is automatically called when an object of a class is created.
To create a constructor, use the same name as the class, followed by parentheses ()
They can take parameters (just like regular functions), which can be useful for setting initial values for attributes
Function Overloading
With function overloading, multiple functions can have the same name with different parameters.
Example for usage: If two functions that need to be defined do the same thing. it is useful to overload one.
OOP
Object-Oriented Programming
It is about creating objects that contain both data and functions
Advantages over procedural programming
provides a clear structure for the programs
helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
faster and easier to execute
makes it possible to create full reusable applications with less code and shorter development time
Access Specifiers
Define how the members (attributes and methods) of a class can be accessed
private
members cannot be accessed (or viewed) from outside the class
protected
embers cannot be accessed from outside the class, however, they can be accessed in inherited classes
public
members are accessible from outside the class
Encapsulation
Meaning:to make sure that "sensitive" data is hidden from users
Declare class variables/attributes as "private"
Inheritance
Definition:used to inherit attributes and methods from one class to another
derived class (child) - the class that inherits from another class
base class (parent) - the class being inherited from
Polymorphism
means "many forms"
occurs when we have many classes that are related to each other by inheritance
perform a single action in different ways
Files
The "fstream" library allows us to work with files
Syntax: #include <fstream>
"ofstream"-Creates and writes to files
"ifstream" Reads from files
"fstream" A combination of ofstream and ifstream: creates, reads, and writes to files
Exceptions
When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception
Exception handling in C++ consist of three keywords: try, throw and catch
The try statement allows you to define a block of code to be tested for errors while it is being executed
The throw keyword throws an exception when a problem is detected, which lets us create a custom error
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block
The try and catch keywords come in pairs
References
A reference variable is a "reference" to an existing variable, and it is created with the & operator
Class Methods
Two ways to define functions that belong to a class
Inside class definition
Define a function inside the class
Outside class definition
Declare a function inside the class and then define it outside of the class. This is done by specifying the name of the class, followed the scope resolution :: operator, followed by the name of the function
Methods are functions that belongs to the class
Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value
Declaration: variable NameOfArray [];
C++ Functions
C++ Classes