C++ 11 Smart pointers : shared_ptr tutorial and example

Smart Pointers are available from C++ 11. Smart pointers are used to make sure that the object gets deleted if it is no longer used. In this chapter we shall learn about “shared_ptr” concept. We shall learn about below topics: 1. Introduction 2. How to create a shared_ptr object? 3. How to create an object […]

C++ 11 feature rvalue reference and move constructor

In this chapter we shall learn about: 1. what are lvalue and rvalue. 2. What are reference variables? 3. what are rvalue reference. 4. Example for rvalue reference. 5. Move constructor What are lvalue and rvalue? The value on the left side of assignment operator is lvalue. The value on the right side of assignment […]

Overloading new and delete operator in C++

We can overload new and delete operator in C++. There are 2 ways to overload. 1. Locally Overloading new and delete operator 2. Globally Overloading new and delete operator Syntax for new operator overload: void* operator new(size_t size); 1. Locally Overloading new and delete operator #include <iostream> // for more tutorials check www.prodevelopertutorial.com using namespace […]

Assignment Operators Overloading in C++

In this tutorial we shall see how to overload assignment operator “=”. #include <iostream> // for more tutorials check www.prodevelopertutorial.com using namespace std; class MyClass { private: int num_1; int num_2; public: MyClass(int a, int b): num_1(a), num_2(b){ } void operator = (const MyClass &obj ) { num_1 = obj.num_1; num_2 = obj.num_2; } void […]

Relational Operators Overloading in C++

In this chapter we shall see how to overload relational operators [<, >, <=, >=, ==]. #include <iostream> // for more tutorials check www.prodevelopertutorial.com using namespace std; class MyClass { private: int num_1; public: MyClass(int a): num_1(a){ } friend void operator < (MyClass &obj1, MyClass &obj2); friend void operator > (MyClass &obj1, MyClass &obj2); friend […]

Binary operator overloading in C++

In this chapter we shall see how to overload binary operators [+, -, *, /]. Example: #include <iostream> // for more tutorials check www.prodevelopertutorial.com using namespace std; class MyClass { private: int num_1; int num_2; public: MyClass(int a, int b): num_1(a), num_2(b){ } friend MyClass operator + (MyClass &obj1, MyClass &obj2); friend MyClass operator – […]

Overloading of stream insertion “<>” operator in C++

In this chapter we shall see how to overload stream insertion and stream extraction operator. Stream Insertion “<<” operator is used for output. Stream Extraction “>>” operator is used for input. cout is an object of Ostream Class. cin is an object of Istream Class. Few important points on stream insertion and extraction operator We […]

C++ Overloading Unary Increment (++) and Decrement Operator (–)

In this chapter we shall see: 1. Overloading prefix increment operator without return type 2. Overloading prefix increment operator with return type 3. Overloading Postfix increment operator 4. Overloading prefix and postfix decrement operator 1. Overloading prefix increment operator without return type Consider the example below; #include <iostream> using namespace std; class MyClass { private: […]

C++ Operator Overloading tutorial

As we saw in previous chapters, we are able to perform function overloading. Similarly we can also overload operators “+, -, =, *” and we call this concept as operator overloading. Why do we need operator overloading? Consider a simple example: Generally we use arithmetic addition operator to add integers. But there comes a case […]

C++ 11 feature: C++ Multithreading Tutorial: std::try_lock

1. try_lock is used to lock multiple mutex. 2. try_lock is used to lock the objects by using try_lock member functions. 3. It is a non blocking call 4. It tries to lock all the objects in the given order 5. On success it will return -1 otherwise it will return 0-based mutex index number […]