Posts

Showing posts from November, 2020

Initializer List

  (a) Initializer List When do we use Initializer List in C++? Initializer List is used in initializing the data members of a class.  The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon.  Following is an example that uses the initializer list to initialize x and y of Point class. -------------------------------------------------------------- #include<iostream>  using namespace std;  class Point {  private:  int x;  int y;  public:  Point(int i = 0, int j = 0):x(i), y(j) {}  /* The above use of Initializer list is optional as the  constructor can also be written as:  Point(int i = 0, int j = 0) {  x = i;  y = j;  }  */ int getX() const {return x;}  int getY() const {return y;}  };  int main() {  Point t1(10, 15);  cout<<"x = "<<t1.getX()<<", ";  cout<...

list

 Lists are sequence containers that allow non-contiguous memory allocation.  As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about doubly linked list.  For implementing a singly linked list, we use forward list. ----------------- #include <iostream>  #include <list>  #include <iterator>  using namespace std;  //function for printing the elements in a list  void showlist(list <int> g)  {  list <int> :: iterator it;  for(it = g.begin(); it != g.end(); ++it)  cout << '\t' << *it;  cout << '\n';  }  int main()  {  list <int> gqlist1, gqlist2;  for (int i = 0; i < 10; ++i)  {  gqlist1.push_back(i * 2);  gqlist2.push_front(i * 3);  }  cout << "\nList 1 (gqlist1) is : ";  showlist(...

Map, Multimap, Unordered Map

/********************************* Maps ***********************************/ Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Some basic functions associated with Map: begin()  – Returns an iterator to the first element in the map end()     – Returns an iterator to the theoretical element that follows last element in the map size()     – Returns the number of elements in the map max_size() – Returns the maximum number of elements that the map can hold empty()  – Returns whether the map is empty pair insert(keyvalue, mapvalue) – Adds a new element to the map erase(iterator position) – Removes the element at the position pointed by the iterator erase(const g)– Removes the key value ‘g’ from the map clear() – Removes all the elements from the map ============================================================ Example #include <iost...

multithreading

 #include <iostream> #include <thread>  #include<unistd.h> using namespace std; void threadFunc() { cout << "Welcome to Multithreading" << endl; } int main() { //pass a function to thread while(1) { thread funcTest1(threadFunc); funcTest1.join(); sleep(3); } } //---------------------------------------------------------program1----- #include <iostream> #include <thread> #include<unistd.h> class DisplayThread { public:     void operator()()          {         for(int i = 0; i < 2; i++)             std::cout<<"Display Thread Executing"<<std::endl;             sleep(4);     } };   int main()   {     std::thread threadObj( (DisplayThread()) );      sleep(5);     //  for(int i = 0; i < 3; i++)       //  ...

new operator

 new operator The new operator denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable. Syntax to use new operator: To allocate memory of any data type, the syntax is: pointer-variable = new data-type; Here, pointer-variable is the pointer of type data-type. Data-type could be any built-in data type including array or any user defined data types including structure and class. Example: // Pointer initialized with NULL // Then request memory for the variable int *p = NULL;  p = new int;                OR // Combine declaration of pointer  // and their assignment int *p = new int;  Initialize memory: We can also initialize the memory using new operator: pointer-variable = new data-type(value); Example: int *p = new int(25); float *q = new float(75.25); Allocate block of m...

opps concept

 Object-oriented programming –  As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like  inheritance,  hiding,  polymorphism, etc in programming.  The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. **Encapsulation: In normal terms, Encapsulation is defined as wrapping up of data and information under a single unit.  In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them. ***Abstraction: Data abstraction is one of the most essential and important features of object-oriented programming in C++. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or imple...

Operator overloading

  Operator overloading  ///////////////////// Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. >Operator overloading is used to overload or redefines most of the operators available in C++.  >It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types. The advantage of Operators overloading is to perform different operations on the same operand. Operator that cannot be overloaded are as follows: Scope operator (::) Sizeof member selector(.) member pointer selector(*) ternary operator(?:) ======================= Syntax of Operator Overloading return_type class_name  : : operator op(argument_list)   {        // body of the function....

Find the two repeating elements

  // C++ program to Find the two repeating  // elements in a given array  #include<bits/stdc++.h>  using namespace std;  void printRepeating(int arr[], int size)  {  int i, j;  printf(" Repeating elements are ");  for(i = 0; i < size; i++)  for(j = i + 1; j < size; j++)  if(arr[i] == arr[j])  cout << arr[i] << " ";  }  // Driver Code  int main()  {  int arr[] = {4, 2, 4, 5, 2, 3, 1};  int arr_size = sizeof(arr)/sizeof(arr[0]);  printRepeating(arr, arr_size);  }  // This code is contributed by Shivi_Aggarwal 

storage class

  Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.  C++ uses 5 storage classes, namely: auto register extern static mutable 1. auto:                                                   ========= The auto keyword provides type inference capabilities, using which automatic deduction of the data type of an expression in a programming language can be done. It is the default storage class for all local variables. The auto keyword is applied to all local variables automatically. {    auto int y;   float y = 3.45;   }   The above example defines two variables with a same storage class, auto can only be used within functions. 2.regist...

Type Conversion in C++

  Type Conversion in C++ A type cast is basically a conversion from one type to another. There are two types of type conversion: 1.     Implicit Type Conversion  Also known as ‘automatic type conversion’. ·          Done by the compiler on its own, without any external trigger from the user. ·          Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data. ·          All the data types of the variables are upgraded to the data type of the variable with largest data type. ·     bool -> char -> short int -> int -> ·       ·     unsigned int -> long -> unsigned -> ·       ·     long long -> float -> double -> long double ...