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…

Virtual Function

Function is a virtual function when the virtual keyword is added in the beginning of function declaration.virtual void display();virtual keyword is required in declaration. In function definition, virtual keyword is not required. When you want to override(redefine) the function in derived class, then virtual function is required. This means virtual function works only when inheritance is involved. Let us understand with the help of an example. class shape {public: virtual void draw() {} virtual void func() {}};class rectangle: public shape {public: void draw() { cout << “Draw rectangle” << endl;…

Dynamic Cast

This is one of the type casting provided by C++. Let us understand with the help of an example. #include <iostream>using namespace std;#define MAX_LENGTH 50class person { unsigned int m_age; char m_name[MAX_LENGTH];public: person(unsigned int age, char* name) { m_age = age;    int len = strlen(name); strncpy(m_name, name, len); m_name[len+1] = ‘\0’; } virtual ~person(){}};class student : public person { unsigned int m_rollNumber;public: student(unsigned int age, char* name, unsigned int rollNumber):person(age, name),m_rollNumber(rollNumber) { } ~student(){}};class teacher: public person {unsigned int m_salary;public: teacher(unsigned int age, char* name, unsigned int salary):person(age, name),m_salary(salary) {…

Return Value Optimization(RVO)

Let us try to understand RVO(Return value optimization) with the help of below example: #include <iostream> using namespace std; class base { unsigned int m_mem1; unsigned int m_mem2; public: base(unsigned int x, unsigned int y): m_mem1(x), m_mem2(y) { cout << “base C’tor” << endl; } ~base() { cout << “base d’tor” << endl; } base(const base& b) { m_mem1 = b. m_mem1; m_mem2 = b. m_mem2; cout << “Copy C’tor” << endl; } static base getNewInstance() { base b(10,20); return b; } }; int main() { base b = base::getNewInstance();…

inline function

inline function is a function which is expanded inline i.e. function definition is copied to a place where it is called.When a function called in program, CPU does following: a) Stores the address of instruction following the function call. b) Push function arguments on the stack c) Transfer the control to the function definition. d) Execute the function e) Transfer the control to the address stored at step a. When execution time of function is less than the switching time from calling to called function, then this becomes overhead. To…