Explicit 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) { if ((other.m_age == m_age) && (other.m_gender == m_gender)) { return true; } return false; }};int main() { person p(40, ‘M’); person p1 = 50; //Implicit conversion if ( p == 40) { cout << “Person age is 40” << endl; } return 0;} Output:Person age is 40 If we can call constructor with…

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…

Switch Virtual Interface

Switch virtual interface(SVI) is also VLAN interface or logical interface. To understand the concept, let us take the below topology: There are two PCs connected to switch. PC-1 is connected to Fa0/8 port and PC-2 is connected to Fa0/9. Both the ports of the switch are in different VLAN as you can see above. In addition, both PC are on different IP Network. PC-1 is on 44.44.44.0 Network and PC-2 is on 55.55.55.0 Network.Since both the PCs are on different VLAN (different broadcast domain), they cannot communicate with each other.…

Constructor & Destructor

Constructor :As name suggest, it construct an object. Constructor is used to initialize the data members of a class.Consider a class below: #include <iostream>#include <string> using namespace std;class employeeData { unsigned int m_salary; char* m_name;public: ~ employeeData() { //Destructor if (m_name) { delete []m_name; } } employeeData() { // default constructor cout << “employeeData default C’tor” << endl; m_salary = 0; m_name = NULL; } employeeData(unsigned int salary, char* name) { //Parameterized constructor cout << “employeeData parameterized C’tor” << endl; m_salary = salary; unsigned int len = strlen(name); m_name =…

Static member

When we add a static data member in class, it will have only one copy per class.Let us understand with an example.//Header file#include <iostream>using namespace std;class base {     static unsigned int m_classMember; //static data member    unsigned int m_objectMember; //nonstatic data memberpublic:    base() {         m_objectMember = 0;        ++ m_classMember;         ++ m_objectMember;       cout << “Count: “ << m_classMember  << “ “ << m_objectMember <<           endl;    }}; //Source file:unsigned int…

Initializer List

Let us understand the concept of initializer list in C++ with the help of an example. Consider a class viz. person, which has data members like age, name and employee dataExample #include <iostream>#include <string>using namespace std;class employeeData { unsigned int m_salary;public: employeeData() { cout << “employeeData default C’tor” << endl; m_salary = 0; } employeeData(unsigned int salary) { cout << “employeeData parameterized C’tor” << endl; m_salary = salary; } employeeData(const employeeData& data) { cout << “employeeData Copy C’tor” << endl; m_salary = data. m_salary; } employeeData& operator=(const employeeData& data) {…

const data members

If you want to make a data member of class to be constant data member, then you can use the const keyword before its data member declaration. The const data members must be initialized by constructor in initialization List otherwise compiler throw an error. Once initialized, const data member value cannot be changed. Example:When we create a class of stack, we want to limit the number of entries for stack object. For that, we can use one const data member which can be used for validating the stack full scenario.…

this pointer

Let us understand with an example how and where this pointer is used. class employee { unsigned int m_age; unsigned int m_salary; employee(unsigned int age, unsigned int salary) { m_age = age; // same as this->m_age = age; m_salary = salary; //same as this->m_salary = salary }void update(unsigned int age, unsigned int salary) { m_age = age; // same as this->m_age = age; m_salary = salary; //same as this->m_salary = salary }}; int main() { employee emp(40, 45000); emp.update(40, 50000); return 0; } In the above example, employee object viz.…

Assignment Operator

Let us understand assignment operator with the help of an example of employeeData class. #include <iostream>#include <string>using namespace std;class employeeData { unsigned int m_salary; char* m_name;public: ~ employeeData() { if (m_name) { delete []m_name; } } employeeData() { cout << “employeeData default C’tor” << endl; m_salary = 0; m_name = NULL; } employeeData(unsigned int salary, char* name) { cout << “employeeData parameterized C’tor” << endl; m_salary = salary; unsigned int len = strlen(name); m_name = new char[len +1]; strncpy(m_name, name, len); m_name[len+1] = ‘\0’; } //Overloaded Assignment Operator employeeData& operator=(const…

Copy Constructor

Prototype of copy constructor <classname>(const <classname>& ); Let say class name is foo. Then the copy constructor prototype will look like: foo(const foo& ); Scenarios:Below are the scenarios where copy constructor is called. 1) When an object is passed by value as function argument. 2) When function returns object by value. 3) When new object is created from an already existing/created object. Need:Compiler provides default copy constructor for each class. However, there are scenarios where we need to define the copy constructor explicitly. Consider a class below: #include <iostream>using namespace…