Skip to content

Commit

Permalink
[ADD CONTENT] C++ Basic Concepts (SRA-VJTI#72)
Browse files Browse the repository at this point in the history
* Adding Basics of C++

* Documentation of Basics of C++

* Updated Readme of C++

* code files updated

* Updated Readme of C++ with fun,class,templates

* Updated Readme of C++ with control_loops

* Final changes

* Updated Readme of C++ with comments

* Final changes

* Changed the readme name to README

* Updated Readme of C++ with comments

* Updated Readme of C++ with changes in output,images

* Adding assets folder

* Adding Image to readme

* Removing redundant files

* Final changes

* Corrected the formatting

* Changed naming format

---------

Co-authored-by: VanshPanchal0308 <vansh5chal0308@gmail.com>
  • Loading branch information
2 people authored and Khushi-Balia committed Mar 28, 2023
1 parent d911b1c commit e4e06ac
Show file tree
Hide file tree
Showing 48 changed files with 1,384 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 1_cpp_basics/0_typecasting/explicit_typecasting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// C++ program to demonstrate
// explicit type casting

#include <iostream>
using namespace std;

int main()
{
float x = 1.2;

// Explicit conversion from double to int
int sum = (int)x + 1;

cout << "Sum = " << sum << endl;

return 0;
}
23 changes: 23 additions & 0 deletions 1_cpp_basics/0_typecasting/implicit_typecasting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// An example of implicit conversion

#include <iostream>
using namespace std;

int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;

// x is implicitly converted to float
float z = x + 1.0;

cout << "x = " << x << endl
<< "y = " << y << endl
<< "z = " << z << endl;

return 0;
}
Empty file removed 1_cpp_basics/1_cpp_basics.cpp
Empty file.
Empty file removed 1_cpp_basics/1_cpp_basics.hpp
Empty file.
26 changes: 26 additions & 0 deletions 1_cpp_basics/1_namespace/namespace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
// first name space
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}

// second name space
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main()
{
// This calls function from first name space.
func();
return 0;
}
12 changes: 12 additions & 0 deletions 1_cpp_basics/2_conditional_statements/if.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
using namespace std;
int main()
{
int num = 10;
if (num > 5) // condition
{
cout << "Hello From SRA\n";
// If condn is fullfilled it will get executed
}
return 0;
}
16 changes: 16 additions & 0 deletions 1_cpp_basics/2_conditional_statements/if_else.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Code to find even and odd num using if else statement
#include <iostream>
using namespace std;
int main()
{
int num = 10;
if (num % 2 == 0) // if num is divisible by 2 i.e remainder is 0 when divided by 2
{
cout << "the number is even\n";
}
else // if If statement is not satisfied the else statement is executed
{
cout << "The number is odd\n";
}
return 0;
}
34 changes: 34 additions & 0 deletions 1_cpp_basics/2_conditional_statements/switch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;
int main()
{
int day = 4;
switch (day)
{ // Switch statement with day given as condition for cases
case 1: // If day=1 then case1 is executed
cout << "Monday";
break; // if case1 is executed then break is executed and exits switch statement
case 2: // If day=2 then case2 is executed
cout << "Tuesday";
break; // if case2 is executed then break is executed and exits switch statement
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default: // If none of the case is satisfied then default statement is executed.
cout << "Not a day";
break;
}
return 0;
}
21 changes: 21 additions & 0 deletions 1_cpp_basics/3_enumeration/enumeration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
using namespace std;

enum seasons
{
spring = 34,
summer = 4,
autumn = 9,
winter = 32
};
// enum named season is made and spring,summer ,autumn and winter are its member with values 34,5,9,32 respectively
int main()
{

seasons s; // s is a variable of type seasons(enum)

s = summer; // s is assigned value of summer
cout << "Summer = " << s << endl;

return 0;
}
25 changes: 25 additions & 0 deletions 1_cpp_basics/4_functions/function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
using namespace std;

// Following function that takes two parameters 'x' and 'y'
// as input and returns max of two input numbers
int max(int x, int y) // max function made with return type int and parameters int x and int y
{
if (x > y)
return x;
else
return y;
}

// main function that doesn't receive any parameter and
// returns integer
int main()
{
int a = 10, b = 20;

// Calling above function to find max of 'a' and 'b'
int m = max(a, b);

cout << "m is " << m;
return 0;
}
41 changes: 41 additions & 0 deletions 1_cpp_basics/5_classes_and_objects/classes_and_objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// C++ program to demonstrate function
// declaration outside class

#include <iostream>
using namespace std;
class Sra
{
public:
char first_letter;
int id;

// printletter is not defined inside class definition
void printletter();

// printid is defined inside class definition
void printid()
{
cout << "SRA id is: " << id;
}
};

// Definition of printletter using scope resolution operator ::
void Sra::printletter()
{
cout << "First letter of my name is: " << first_letter;
}
int main()
{

Sra obj1;
obj1.first_letter = 'v';
obj1.id = 24;

// call printletter()
obj1.printletter();
cout << endl;

// call printid()
obj1.printid();
return 0;
}
20 changes: 20 additions & 0 deletions 1_cpp_basics/6_templates/class_template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
template <class T> // Declaring the Class template
class A
{
public:
T num1 = 5;
T num2 = 6;
void add()
{
std::cout << "Addition of num1 and num2 : " << num1 + num2 << std::endl;
}
};

int main()
{
A<int> d;
d.add(); // Calling the class method
return 0;
}
21 changes: 21 additions & 0 deletions 1_cpp_basics/6_templates/functional_template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Example of Function Template

#include <iostream>
using namespace std;
template <class T>
T add(T &a, T &b) // Declaring a Functional template
{
T result = a + b;
return result;
}
int main()
{
int i = 2;
int j = 3;
float m = 2.3;
float n = 1.2;
cout << "Addition of i and j is :" << add(i, j); // Calling the template for integer data type
cout << '\n';
cout << "Addition of m and n is :" << add(m, n); // Calling the template for float data type
return 0;
}
23 changes: 23 additions & 0 deletions 1_cpp_basics/7_arrays/arrays2d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;
int main()
{
int arr[4][2] = {// An array with 4 rows and 2 columns is made and initialized
{10, 11},
{20, 21},
{30, 31},
{40, 41}};

int i, j; // Two variables for looping

cout << "Printing a 2D Array:\n";
for (i = 0; i < 4; i++) // traversing through row
{
for (j = 0; j < 2; j++) // traversing through column
{
cout << "\t" << arr[i][j];
}
cout << endl;
}
return 0;
}
35 changes: 35 additions & 0 deletions 1_cpp_basics/7_arrays/passing2darraytofunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
using namespace std;

void display(int (*ptr)[4], int row, int col) // Call by Address
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
cout << "\t" << *(*(ptr + i) + j); // Array is printed with help of pointer arithmetic
cout << "\n";
}
cout << "\n";
}

void print(int ptr[][4], int row, int col) // Call by value
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
cout << "\t" << ptr[i][j];
cout << "\n";
}
cout << "\n";
}

int main()
{
int a[3][4] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};

display(a, 3, 4);
print(a, 3, 4);
return 0;
}
12 changes: 12 additions & 0 deletions 1_cpp_basics/8_pointers/pointers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
using namespace std;
int main()
{
int number = 30;
int *p;
p = &number; // stores the address of number variable
cout << "Address of number variable is:" << &number << endl;
cout << "Address of p variable is:" << p << endl;
cout << "Value of p variable is:" << *p << endl;
return 0;
}
23 changes: 23 additions & 0 deletions 1_cpp_basics/8_pointers/pointers_and_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;
/* Usage of pointer to an array */
int main()
{
int a[4][2] = {// Declaring and initialising a 2D array
{1, 2},
{1, 2},
{1, 2},
{1, 2}};

int(*ptr)[2]; // Declaring a 2D pointer
int i, j;
for (i = 0; i <= 3; i++)
{
ptr = &a[i]; // Stores the address of Array a with index i, in ptr
cout << "Row" << i << ":";
for (j = 0; j <= 1; j++)
cout << "\t" << *(*ptr + j); // Prints the data at pointed by address ptr+j
cout << endl;
}
return 0;
}
14 changes: 14 additions & 0 deletions 1_cpp_basics/9_vectors/access_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> num{1, 2, 3, 4, 5}; // Initialised a vector

cout << "Element at Index 0: " << num.at(0) << endl; // Printing value at vector index 0
cout << "Element at Index 2: " << num.at(2) << endl; // Printing value at vector index 2
cout << "Element at Index 4: " << num.at(4); // Printing value at vector index 4

return 0;
}
Loading

0 comments on commit e4e06ac

Please sign in to comment.