Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CPP #312

Merged
merged 1 commit into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions cpp/arrays/arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index.

Index starts from 0 to size-1.

Arrays can be one-dimensional, multi-dimensional in C++ language. The more popular and frequently used arrays are one-dimensional and two-dimensional arrays.

Arrays store the collection of data sequentially in memory and they must share same data type.

# How to declare an array?

### One dimentional Array:

```c
data-type array-name[size];
```

### Two dimensional array:

```c
data-type array-name[size][size];
```

## Examples

### One dimentional Array:

```c
int a[5];
```

### Two dimentional Array:

```c
int a[2][3];
int b[][3]; // is also valid
```

# How to initialize arrays

### One dimentional Array:

```c
int a[5] = {1,2,3,4,5};
```

### Two dimentional Array:

```c
int a[2][3] = {
{1,2,3},
{4,5,6}
};
```

# How to access array elements

Array elements can be accessed by using indices. Array indices starts from `0`. `Array[n-1]` can be used to access nth element of an array.

## Examples

### One dimentional array:

```c
#include <iostream>
using namespace std;

int main()
{
int arr[5] = {1,2,3,4,5};
int n = sizeof(arr)/sizeof(arr[0]); // gives length of an array
cout << "Length of the array:" << n << endl;
cout << "first element: " << arr[0] << endl; // prints first element of the array
cout << "last element: " << arr[n-1]; // prints last element of the array
return 0;

}

```
### Check result [here](https://onecompiler.com/cpp/3vmbf4ed9)

### Two dimentional array:

```c
#include <iostream>
using namespace std;

int main()
{
int a[2][3] = {
{1,2,3},
{4,5,6}
};
for(int i=0; i<2; i++) // iterates for each row
{
for(int j=0; j<3; j++) // iterates for each column
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
```
### Check result [here](https://onecompiler.com/cpp/3vmbfbbzg)

## Summary

* Arrays is a collection of homogeneous data.
* Arrays stores data sequentially in memory.
* Arrays are finite.

78 changes: 78 additions & 0 deletions cpp/basics/constants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

Literals/constants are used to represent fixed values which can't be altered later in the code.

## 1. Integer literals

Interger literals are numeric literals. Below are the examples of various types of literals

```c
79 // decimal (base 10)
0253 // octal (base 8)
0x4F // hexadecimal (base 16)
22 // int
53u // unsigned int
79l // long
7953ul // unsigned long
```

## 2. Float point literals

Float point literals are also numeric literals but has either a fractional form or an exponent form.

```c
79.22 // valid
79E-5L // valid
53E // not valid as it is incomplete exponent
.e22 // not valid as missing integer or fraction
```

## 3. Boolean literals

There are two Boolean literals which are part of standard C++ keywords −

* true value representing true.

* false value representing false

## 4. Character literals

Character literals are represented with in single quotes. For example, `a`, `1` etc. A character literal can be a simple character (e.g., 'a'), an escape sequence (e.g., '\n'), or a universal character (e.g., '\u02C0').

|Escape sequence| Description|
|----|----|
|\n | New line|
|\r | Carriage Return|
|\? | Question mark|
|\t | Horizontal tab|
|\v | Vertical tab|
|\f |Form feed|
|\\ | Backslash|
|\' | Single quotation|
|\" | Double quotation|
|\0 | Null character|
|\? | ? Question mark|
|\b |Back space|
|\a | alert or bell|

## 5. String literals

String literals are represented with in double quotes. String literals contains series of characters which can be plain characters, escape sequence or a universal character.

```c
"Hello World"
```

## How to define constants

You can use either `#define` or `const` to define constants as shown below.

* Using #define

```c
#define identifier-name value
```
* Using Const

```c
const datatype variable-name = value;
```
56 changes: 56 additions & 0 deletions cpp/basics/data-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
As the name suggests, data-type specifies the type of the data present in the variable. Variables must be declared with a data-type.

There are three different types of Data types in C++.

| Types | Data-type|
|----|----|
|Basic | int, char, float, double, short, short int, long int etc |
|Derived | array, pointer etc |
|User Defined Data Type | structure, enum, Class, Union, Typedef |


## 1. Basic data types

Basic data types are generally arithmetic types which are based on integer and float data types. They support both signed and unsigned values. Below are some of the oftenly used data types

| Data type | Description | Range | Memory Size|
|----|----|----|----|----|
| int| used to store whole numbers|-32,768 to 32,767|2 bytes|
|short int| used to store whole numbers|-32,768 to 32,767| 2 bytes|
|long int| used to store whole numbers| -2,147,483,648 to 2,147,483,647| 4 bytes|
|float| used to store fractional numbers|6 to 7 decimal digits| 4 bytes|
|double| used to store fractional numbers|15 decimal digits| 8 bytes|
|char|used to store a single character|one character|1 bytes|
|bool| Boolean data type| 1 byte|

### Examples

```c
#include <iostream>
using namespace std;

int main()
{

int x = 90;
int y = sizeof(x);
cout << "size of x is: " << y << endl;

float f = 3.14;
cout << "size of float is: " << sizeof(f) << endl;

double d = 2.25507e-308;
cout << "size of double is: " << sizeof(d) << endl;

char c = 'a';
cout << "size of char is: " << sizeof(c) << endl;

return 0;
}

```
#### Run [here](https://onecompiler.com/cpp/3vm83rs6y)

## 2. Derived or User defined Data types

Derived Data types and user defined dta types are the ones which are derived from fundamental data types and are defined by user. Arrays, Pointers, functions etc. are examples of derived data types. Enum, Structures, Class, union, enum, typedef etc are User defined data types. Let's learn more about them in next chapters.
52 changes: 52 additions & 0 deletions cpp/basics/fundamentals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Sample C++ program

```c
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!!";
return 0;
}
```

**`#include <iostream>`** : iostream is a inbuilt header library which allows you to deal with input and output objects like cout etc.

**`using namespace std`** : The above line means that the object and variable names can be used from standard library.

**`cout`** : cout is used to print the output.

**`main()`** : main function is the entry point of any C++ program.

**`return 0`** : To end the main function

# Compile & Run a C++ Program

## How to compile a program in C++

* Open your terminal, navigate to the directory where you have saved your program. Consider `firstprogram.cpp` is the name of your program.

```c
sudo g++ -o firstprogram firstprogram.cpp
```

## How to run a C++ program

```
./firstprogram
```

## Using Onecompiler

* You don't need to install any software or compiler.
* Just goto [OneCompiler](https://onecompiler.com/cpp) and enjoy programming without any installation.

## Comments

1. Single line comments

`//` is used to comment a single line

2. Multi line comments

`/**/` is used to comment multiple lines.

65 changes: 65 additions & 0 deletions cpp/basics/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
C++ is a widely used middle-level programming language.

* Supports different platforms like Windows, various Linux flavours, MacOS etc
* C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
* Case-sensitive
* C++ is a compiler based language
* C++ supports structured programming language
* C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
* Like C, C++ also allows you to play with memory using Pointers.

## Why you should learn C++?

* Many databases like MySQL, MongoDB, MemSQL, etc are written in C++
* C and C++ are used in developing major operating systems such as Windows, Linux, Android, Ubuntu, iOS etc.
* C++ has large community support.
* C++ is used in writing compilers, web browsers, Games and even in embedded systems.
* C++ developers are offered with high salaries.
* C++ is considered as a foundational language to learn advanced programming languages.

## Why C++ is popular?

* C++'s greatest strength is it's scalability and hence challenging 3D games are often built in C++.
* C++ is fourth popular language according to [Tiobe Index](https://www.tiobe.com/tiobe-index/)
* Applications which are critically reliable on speed and resource usage are usually preferred to be built in C++.
* Good community support.

# Installation

## 1. on MacOS

* Install [Xcode](https://developer.apple.com/xcode/) from Appstore.
* Once installed, accept the terms and conditions and provide your MAC password.
* Now, components will get installed.

## 2. on Windows

* There are many C++ compilers available in the market which are free to use.
* Download C++ compiler from [VS-C-andC++](https://visualstudio.microsoft.com/vs/features/cplusplus/)
* Run the executable file.

## 3. on Linux

* Most Linux operating systems are pre-installed with GCC.

```shell
gcc -version
```
* For Ubuntu and Debian, use the below command

```shell
sudo apt-get update
sudo apt-get install GCC
sudo apt-get install build-essential
```
* For redhat, use the below command

```shell
yum groupinstall 'Development Tools'
```

## 4. Using Onecompiler

* You don't need to install any software or compiler.
* Just goto [OneCompiler](https://onecompiler.com/) and choose the programming language as C++ and enjoy programming without any installation.

Loading