Wednesday, April 22, 2020

Array in C++ programming



Array in C++ programming:


array in c++ language
Array in c++


An array may be a fixed number of elements of the identical type stored sequentially in memory. Therefore, an integer array holds some number of integers, a personality array holds some number of characters, and so on. the scale of the array is noted as its dimension.


To declare an array in C++, we write the following:

                           

Syntax of declaration:


Type array Name [dimension];  

 For example;

char arr[5]={1,2,3,4,5}


Access of one dimensional Array:


The elements in an array can be accessed by using the index number at which that number is present.

For example:

{23,54,67,32,45}

Here if we'd like to access “67” we are able to get access by its  indicator which “2”  here because array’s index always starts from “0”.


Initialization of 1-Dimensional Array:



 The elements of an array also like every other variables must be initialized before they'll be used otherwise it'll show unexpected ends up in our program. There are two ways to initialize the array.


int arr[5];


Declaration of 1D Array:


An array can be declared within the following two ways:

One way is to declare the array and then initialize some or all of the elements:

 arr[0] = 87;

 arr[1] = 98;

 arr[2] = 34;

 arr[3] = 55;

 arr[4] = 66;


And the other way is to initialize some or all of the values at the time of declaration:

 int arr[4] = { 6, 0, 9, 6 };


EXAMPLE:


Let’s study an easy program to initialize and declare the 

worth of array:


Source code:


 #include<iostream>

using namespace std;

 int main()

{

 int arr[7];

 cout << “ enter the number:“ << endl;

 for(int i = 0; i < 7; i++)

 {

 cin >> arr[i];

}

 cout << “values assigned to array are:“;

  for(int i = 0; i < 7; i++)    
    
{

  cout << arr[i]<<endl;

}

  return 0;

 }


Output:

enter the number.

45
32
67
54
78
65
43

Values assigned to array are:

arr[0] = 45;
 arr[1] = 32;
 arr[2] = 67;
 arr[3] = 54;
 arr[4] = 78;
arr[5] = 65;
 arr[6] = 43;

OR

Int arr[7]={45,32,67,54,78,65,43}


Explanation:


In this above program first of all we initialized the data type and length of array and then we initialize the values that are stored in array by the user with the assistance of For loop and then we've used another For Loop to display the values stored in array.


Multidimensional Array/2D Array:


C++ also supports the creation of multidimensional array, through the addition of over one set of brackets. Thus, a two-dimensional array also created by the following:

Syntax:


 Type array Name[row][column];

Total no. of elements in 2-D array is calculated as row*column


For example:

int a[2][3];

Total no of elements=row*column is 2*3 =6

 It means the matrix consist of 2 rows and 3 column



Positions of 2-D array elements in an array are as below :

00     01     02
10      11     12


For example:

  6       2        8
  48     66     15


Initialization of 2D Array:


2D array are also initialized exceedingly in a similar way as the one-dimensional array are initialized:


Type array Name[row][column];          for example:

Char arr[2][3]


Note that dimensions should be provided when initializing multidimensional arrays, because it is otherwise impossible for the compiler to see what the intended element partitioning is.


Declaration of 2D Array:


2D Array can be declared by following ways:

int Array[2][4] = { 9, 2, 4, 22, 42,50, 11, 61 };

 int Array[2][4] = { { 9, 2, 4, 22 } , { 42, 50, 11, 61 };


EXAMPLE:

Let’s study an easy example in which we've initialized and declared a two


Dimensional/2D array:


Source code:


#include<iostream>

 using namespace std;

int main()

 {

int Array[1][2]=17;

 Array[0][0] =7 ;

 Array[0][1] = 8;

 Array[0][2] = 45;

 Array[1][0] = 12;

 Array[1][1] = 33;

 Array[1][2] = 11;

 for(int k = 0; i < 1; i++)

{

 for(int j = 0; j < 2; j++)

{

 cout << Array[k][j]<<endl;

}

}

return 0;

}


Output: 

7     8    45
12    33  11

Explanation:


In this program we've first initialized the length of the array and so then we've declared the values of Array by giving it the hardcoded values and then we've used 2 for loop to display the values stored in two dimensional array.




THANK YOU FOR READING THE ARTICLE I HOPE THAT IT HELPED YOU.

Monday, April 20, 2020

C++ OOP (object oriented programming)

C++ OOP (object-oriented programming):


c++ oop


C++ is also super-set  of C, which means every C program is also a legal C++ program.

C++ OOP is also  a statically typed, compiled, general-purpose, case-sensitive, a free form programming language that supports procedural, object-oriented, and generic programming.

C++ OOP was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C language and originally named C with Classes but then it's name was changed to C++ in 1983.

Object:


C++ OOP introduces object-oriented programming (OOP) features to C. 
It offers classes, which provide the four features commonly present in OOP  (and some non-OOP) languages:


FOUR MAIN KEY POINTS OF C++ OOP


1-Abstraction,
2- Inheritance
3- Polymorphism
4-Encapsulation


Abstraction:


Abstraction is also among the key concepts of C++ OOP.

Abstraction means to hide something unwanted details and exposing only the essential features of an object or concept. Abstraction is supported using the interface and an abstract class. Abstraction only focuses on what the object does instead of how it does.


Inheritance:


A very important aspect in the object-oriented program is inheritance. In Inheritance, you can use a code more than one time in the same program. As the name of the topic suggests that you can make other classes from the already existing classes.

In C++ OOP Inheritance means that you can use a code more than one time the re-usability of code have been made possible in C++ OOP


Polymorphism:


In C++ OOP:

Polymorphism means “many shapes.” It refers to the ability of one object to have many types. If we have a function that expects a Vehicle object, we can safely pass it a Car object, because every Car is also a Vehicle. This is how polymorphism works.


Encapsulation:


Encapsulation in C++ OOP is defined as:

Grouping of related data and functions together as objects and defining an interface to those objects is called Encapsulation.
Here it is clarified which object is engaged on which function.


I hope that you have already understood the concept of C++ Template which we have discussed earlier in introduction to C++.


CLASSES:


A class in C++ is a user-defined type or data structure declared with the keyword class that has data and functions (also called member variables and member functions) as its members whose depends upon the following three access specifiers:

1.     Private
2.     protected 
3.     Public.

 Public Members :

Members declared public within a class can also be accessed outside class but and within a program. Public members can be accessed using direct access operator (.) with the object of the class.

Private Members :


Members declared private cannot be accessed or even viewed by any object or function outside the class. Only the member functions or friend functions can access private members.

Protected Members :


Members declared protected cannot be accessed outside the class but they can be accessed by any derived class of that class. 


By default access to members of a C++ class is private. Private members cannot be accessed outside the class. They can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.


Library in C++ OOP:


The library of C++ OOP is categorized into two types:

  1. The Standard Function Library
  2. The Object-Oriented Class Library

The Standard Function Library:


It is further categorized into following:

<iostream>   It  Contains function prototypes for the C++ standard input and standard output functions. 

<cmath>    it Contains function prototypes for math library functions.

<cstdlib>   It  Contains function for conversions of numbers to text.

<ctime>      It Contains function and types for manipulating the time and date.

<cstring>     It Contains function prototypes for C-style string-processing functions.

<cstdio>     It Contains function  for the C-style standard input/output library functions and information used by them.

<fstream>    It Contains functions that perform input from files on disk and output to files on disk.
 

<string>     It Contains the definition of class string from the C++ Standard Library. 

The Object-Oriented Class Library:


 The class library is further categorized into following:

  • The String Class
  • The Numeric Classes
  • The STL Algorithms
  • The STL Function Objects

THANK YOU FOR READING THE ARTICLE I HOPE THAT IT HELPED U.


Sunday, April 19, 2020

Pointer in C Language


Pointer in C Language:


pointer in c language
pointer in c language

A pointer in c language is defined as a variable that store memory address or that contains address of another variable where addresses are the location number always contains a number.
 So, pointer always contains the number.

 It is called pointer because it points to a specific location in memory by storing address of that location.


Syntax of pointer in c language:


syntax of array

Operators in pointer in c language:


 When a pointer is declared in a program, it contains garbage value i.e. it will point toward any value in the memory.

Two operators which are utilized in the pointer i.e. address operator (&) and indirection operator or deference operator (*).

  1.   Indirection operator gives the values stored at a specific address.  
  2.     Address operator can't be used in any constant or any expression. 


Example:


example of indirection and address operator




Pointer assignment:



 int i=10;

 int *p=&i;        //     value assigning to the pointer


Here declaration tells the compiler that "P"  will store the address of integer value or in other word "P" is a pointer to an integer and *p reads the value at the address that "P" contains.

P++;

We can give the value of 1 pointer variable to others when their base type and data type is identical or both the pointer points to the identical variable as in the: 


Array:

pointer assignment






We can give constant 0 to a pointer of any type for that specific constant 

‘NULL’ is used such as:

 *p=NULL;

 Which Operations are performed in pointer???

   


Addition of any number using pointer:



Example:

addition of a number through pointer
 

Subtraction of any number from a pointer:




Example:


subtraction of a number through pointer



Arithmetic operation never performs on pointer are:


1-    Addition, multiplication and division of two-pointer.
2-    Multiplication between the pointer by any number.
3-    Division of pointer by any number.
4-    Addition of float or double value to the pointer.


Increment and decrement operator:


The precedence level of difference operator increment or decrement operator is same and their associativity from right to left.



example of increament operator in c language



since the operator associated from right to left, so we are using increment operator to the pointer p.


increment structure in c language



Pointer Comparison:

  1.      Pointer variable can be compared when both of the variables, object of the identical data type and when both of the pointers of variable points to element of the same array. 
  2.     ==,!=,<=,,>=, can be used with a pointer.
  3.     Equal and not equal operators used to compare two-pointer should find whether they contain same address or not and they will equal only if are null or contains the address of same variable. 


Example:-

example of pointer comparison in c language

Pointer to pointer:

Addition of  a pointer variable stored in another variable is called

pointer to pointer variable.


Syntax of pointer to pointer:



syntax of pointer to pointer

Pointer vs array:


Let’s take a glance at a simple example to differentiate between pointer and 


pointer vs array in c


In the above example, at the first time printf ( ), print the identical/same value array and pointer.

Here array “arr”, as pointer to character and “p” acts as a pointer to array of character.

 After we attempt to extend the value of arr it'd give the error because its known to compiler about an array and its base address which is usually printed to base address is thought as a constant pointer and also the bottom address of array which isn't allowed by the compiler.



THANKS FOR READING THE ARTICLE I HOPE THAT IT HELPED U





If else in c

IF Else in C: If-else in c: If else is the most used condition in our life. Whenever we need to make any decision we use...