cpp
program# include <iostream> //standard I/O library in cpp
using namespace std; //container for names
int main(){
cout << "Hello world!" << endl;
cout << "Enter an integer" << endl;
cin >> value; //input
cout << "The integer is: " << value << endl;
//endl is end of line/new line
return 0;
}
.o
file is an object file, intermediate file produced by the compiler during compilation
.o
file, and all these object files will be linkedin together to create the final executable.h
file contains declarations of functions, variables, classes or other data structures
#include
preprocessor directive#ifndef
prevents double inclusion and resulting compiler errors when a header file is indirectly included multiple times in a projectWhen you try to run a C++ program, the compiler first converts source files into object files, then links these with any required libraries and other object files, using information from header files, to produce a final executable
$n! = n*(n-1)(n-2)…32*1$
int factorial (int n){
int fact = 1;
for(int i = 1; i<=n; i++){
fact*=i;
}
}
In C (pass by value):
void swap(int* pa, int* pb){
//switch the addresses, not the variables
int temp = *pa
*pa = *pb;
*pb = temp;
}
int main(){
int a= 7, b = 13;
cout << "Before swap: a = " << a << "and b = " << b << endl;
swap(&a,&b);
cout << "After swap: a = " << "and b = " <<b<< endl;
return 0;
}
In C++ (pass by reference):
void swap(int& x, int& y){ // & access original variable in main
int temp = x;
x = y;
y = temp;
}
int main(){
int a = 7, b = 13;
swap(a,b);
return 0;
}