C++ Introduction
-
How it works
There are three steps of building your code
- Preprocessing
Preprocessor statements gets evaluated (they start with#
)
It outputs a cpp file that doesn't contain any preprocessor directives - Compiling
Then your cpp files are compiled into a object files that contains machine code - Linking
Linker links all obj files and puts them together into exe file
- Preprocessing
-
Basic syntax
-
Preprocessor statements
These are directives which give instructions to the compiler to preprocess the information before actual compilation-
#include
includes (copies and pastes contents of) a header file into source file#include <iostream> // copies and pastes contents of a header file iostream into this file
-
#define
defines a word which when found in code is being replaced with a given value#define number 1 // if compiler sees "number" it replaces it with "1" int a = number;
-
#if
include or exclude code based on given condition// if 1 (always true) a=1 #if 1 int a = 1; #endif
-
#pragma once
it prevents us from including header file multiple times in one cpp file#pragma once
-
-
Main Function
This is main function that is starting point for program execution. Every C++ program must have a main function. No other function in program can be called main. Main is specific function that not returns any valueint main(){ // your code should go here }
-
Own Functions
Functions are modules of code that performs a specific tasks. Functions can (but it don't need to) take in data as parameters in ( ) and return a result. When function is not returning any result it is called a void. You can call a function wherever in your code// function declaration // [returned_type] [function_name] (arguments) void Multiply(int a, int b){ // function body } // function call Multiply(1, 5)
-
Variables
Variable is container in your ram memory to a piece of data that we can store and change. Constant is a variable but it cannot be changed.
Here are basic primitive data types:- Int
Int is integer number that is 4 bytes large - Char
Char is number or character that is 1 byte large - Float
Float is a decimal number that is 4 bytes large - Double
Double is a decimal number that is 8 bytes large - Bool
Boolean is a number that can have values 1 (True) or 0 (False) and it is 1 byte - Short
Short is number that is 2 bytes large - Long
Long is number that is 8 bytes large - Signed variable
Signed variable can store both positive and negative values - Unsigned variable
Unsigned variable can store only positive values
- Int
// variable declaration is when you specify type and identifier but not yet asigned value // [type] [name]; int a; float b; char c; unsigned int ab; short int abc; // variable initialization is when you provide a value at time of creation // [type] [name] = [value] int value = 10; char ch = 'c' bool isInt = false; bool isChar = true;
You can also not specify typename and use
auto
keyword.auto
keyword makes compiler deduce the type itself.auto variable = 2; // it will be int type
- Displaying variables in console
You can display variables to console using
std::cout
from standard c++ libray. Then you use<<
operator and you give variable name after. You can also use another<<
to display more variables orstd::endl
to get to a new line.
std::cout << "Some message" << std::endl;
-
-
Header files
Header files are files (with .h or .hpp extension) that contain function declarations, and several more other that needs to be shared between several cpp files. You then include them using
#include
-
Translation unit
Translation unit is file that contains C++ code that the C++ compiler will compile and generate an Object file for.