Introduction To C++ PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Introduction to C++

INSAT ACM Student Chapter


Introduction

C++ is a general-purpose programming language created as an extension of the C programming


language.It is increasingly used for programming “embedded systems”, small computers that
control devices such as automobile engines or cellular telephones. C++ also supports
object-oriented programming.
Why C++

C++ is by far the the most popular language of choice for competitive programmers across the
world as it is usually faster than Java and Python, and most of the resources are available in C++.
C++ also has a vast library called STL(Standard Template Library) which makes life a lot easier for
competitive coders.
What a C++ program looks like

#include <bits/stdc++.h>

using namespace std;

int main() {

cout << "Hello, World!" << endl;

return 0; }
Input output

int a;

cin >> a ;

cout << "The answer is" << 6 * 7 << endl;

Library :
<iostream>
Arithmetic Operators Assignment Operators

Addition +

Subtraction -
=
Multiplication *
+=
Division / -=
*=
Modulus % /= etc ...

Incrementation ++

Decrementation --
Relational operators Logical operators

< less than ! not


<= less than or equal && and
> greater than || or
>= greater than or equal

== equal

!= not equal
Data types

Type Size range

int 4 bytes -2147483648 to 2147483647


(<=10^9 OK)

double 8 bytes

float 4 bytes

char 1 byte -127 to 127 or 0 to 255

bool 1 bytes
The if statement

int a, b; int a,b;

a=2; a=2;

if (a==0) { b=0; } Or a==0 ? b=0 : b=1;

else { b = 1; }

The brackets are


unnecessary
here
Switch
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block}
default is
optional
loops

for (i = 1; i<= 10; i++) { while (condition) { do {

cout << i<< endl; statements; statements ;

} } } while (condition);

You can use break to get


out of the loop and
continue to skip to the
next iteration
Strings
#include <bits/stdc++.h>
using namespace std;
Library:
int main(){ <string>
string greeting= "Hello";
string name ;
cin >> name;
string output = greeting +” “+ name;
cout << "The length of the string is: " << output.length(); //or .size()
return 0;
} example

example2
Data structures
arrays Library for sort :
#include <bits/stdc++.h> <algorithm>
using namespace std;
int main(){
int T1[5];
int T3[2][4]; Add a function as a third
string T2[]={"hello","acm","people"}; parameter to sort in a particular
cout << "size of T2:" << sizeof(T2)/sizeof(T2[1])<<endl; order.
int T[5]={2, -3, 45}; In reverse order:
sort(T,T+5); sort(arr,arr+n,greater<int>
for (int i=0;i<5;i++) ());
cout<< T[i] <<endl;
return 0;
}
Vectors
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v1;
for (int i = 10; i >=0; i--) { v1.push_back(i); }
sort(v1.begin(), v1.end());
for (int i = 1; i <= 10; i++) { cout<<v1[i]<<endl; }
return 0;
}
Helpful links and tools
http://library.lol/main/EABCCEC887295E5DA350302DECA61D7C CP3 download

https://ideone.com/

Arabic Competitive Programming - YouTube

https://www.fosshub.com/Code-Blocks.html

https://www.youtube.com/watch?v=vLnPwxZdW4Y

You might also like