-
Notifications
You must be signed in to change notification settings - Fork 97
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 CONTENT] C++ Basic Concepts #72
Changes from 16 commits
0772c1d
90c792e
dc2248f
ef0c177
4a9246e
b6e69f6
9b15ef5
396b86f
3157090
26c796a
db3faf4
570ab59
7efd33e
4eb7b58
0ee122a
fb945b9
b07810b
4577a1d
ddfa580
0f7440f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#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; | ||
} | ||
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#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; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new line