Function Overloading

To understand function overloading, we need to understand the concept of polymorphism. Polymorphism means having many or multiple forms. There are two type of polymorphism.1) Static Polymorphism2) Dynamic Polymorphism Function overloading comes under static Polymorphism. Function overloading means functions with same name but different number or type of arguments.Let us understand with the help of example. #include <iostream>using namespace std;class base {public: void display(int num) { cout << “Num: ” << num << endl; } void display(int num1, int num2 = 10) { cout << “Num1: ” << num1 <<…

Composition

Every big thing is made up of small-small things. For example, a house is built from walls, ceiling, and doors. Now in terms of C++, we can say that every complex object is built from small or simple object. This process of creating a complex object from simple and small objects is called composition in C++. In other words, whenever there is “has-a” relationship, then composition comes in to picture. E.g., house has four walls, two doors and one ceiling. Let us try to understand the concept of composition with…

Explicit conversion constructor

Consider an example below to understand the concept of explicit constructor #include <iostream>using namespace std;class person {private: unsigned int m_age; char m_gender;public: person (unsigned int age = 40, char gender = ‘M’); bool operator==(const person& other) { cout << “Comparison”<< endl; if ((other.m_age == m_age) && (other.m_gender == m_gender)) { return true; } return false; }};person::person(unsigned int age, char gender){ cout << “C’tor” << endl; m_age = age; m_gender = gender;}int main() { person p(40, ‘M’); person p1 = 40; //Implicit conversion if ( p == 40) { //Implicit conversion…

802.1Q VLAN Trunking

Let us take the below topology to understand the concept of trunking in switches.In the below topology, we have total four PC devices. PC-1 and PC-3 are connected to access port of switch 1 and under VLAN 22 and 23 respectively. Similarly, PC-2 and PC-4 are connected to access port of switch 2 and under VLAN 22 and 23 respectively. Let us say PC-1 wants to send a message to PC-2. Both are under VLAN 22(under same network: 44.44.44.0) but both the PCs are connected to different switches. Below is…

Queue using stack

Let us understand how to prepare queue using two stacks When we say we have to prepare queue using stack that means all the operations of stack should be used to get the queue functionality. Queue has FIFO property i.e. First-In First-Out i.e. element which is inserted first , same element should be removed or pop first. Consider that we have two stacks s1 and s2. Initially both the stacks are empty. Here we have used Stack Using Linked List. When we push any data in to stack, we have…

nullptr

Let us understand the need of nullptr with help of an example. #include <iostream>using namespace std;class base {public: void display(int num) { cout << “Integer display: “ << num << endl; } void display(char* ptr) { cout << “ptr display: “ << ptr << endl; }}; int main() { base b; b.display(NULL); return 0; } The above code will not compiled. Below error will come: “In function int main():     15:15: error: call of overloaded display(NULL) is ambiguous            b.display(NULL); 15:15: note: candidates are:  …

auto keyword

Let us understand the usage of auto keyword with the help of an example. #include <iostream>using namespace std;int main(){ auto i = 10; auto d = 7.5; cout <<  “Integer and double: ” << i << “ “ << d << endl; return 0;} Above code can be compiled using -std=c++11 flag. Here, we have not defined the type for variable i and d.  When we initialize the variable at the time of declaration, compiler can identify or deduce the type for it. Based on this, C++11 came with the…

Stack Using Linked List

Let us see in the below program how stack can be implemented using Linked List. #include <iostream>using namespace std;typedef struct node{ int data; struct node* next; }Node;class stack{private: Node* head;public: stack() { head = NULL; } ~stack() { while (NULL != head) { Node* temp = head; head = head ->next; delete temp; } } void push(int data) { Node* temp = new Node; temp->data = data; if (NULL == head) { head = temp; head->next = NULL; return; } temp->next = head; head = temp; } int pop() {…

Stack using fixed size array

Stack is one of the data structure that uses LIFO (Last In First Out) or FILO (First In Last Out) property i.e. element which is stored in last, will be out (read) first.Real life example of stack is stack of plates. Last plate put over the plates is the first plate that is removed or used. #include <iostream>using namespace std;const int MAX_INDEX = 50;  // Maximum index of array.const int MIN_INDEX  = -1; // Minimum index  of array.class stack{private: unsigned int arr[MAX_INDEX]; int top;public: stack() { top = MIN_INDEX; }…

Initializer_list

Let us understand the concept of initializer_list with the help of an example: #include <iostream>#include <vector>#include <initializer_list>using namespace std;#define MAX 10class array{public: array(std::initializer_list<unsigned int> list) { index = 0; for(auto item: list) { arr[index++] = item; } cout  << “C’tor” << endl; } void display() { for (unsigned int i = 0; i < index; ++i) { cout << arr[i] << endl; } }private: unsigned int arr[ MAX]; unsigned short index;};int main(){ array arrObj = {0,1,5,7,8}; arrObj.display(); vector <unsigned int> v = {5,8,9,10,20}; cout << “Entries in vector: ” <<…