C++ Mutable

What is need for Mutable? Data members cannot be changed in const objects. In some scenarios this is required and mutable keyword provides the solution. Demonstrate the usage of mutable keyword #include <iostream>using namespace std;class MyClass {mutable int x;int y;public:MyClass (int a, int b){x=a;y=b;}void SetX(int a) const{x=a;}void SetY(int b) const{/* Compilation Errory=b;Error E2024 mutable.cpp 21: …

C++ Mutable Read More »

C++ Templates

What are templates? Templates provide a mechanism to handle different data types in the same function or class. When used in functions they are referred as function templates. When used with classes they are referred as class templates. Template arguments are specified either with keywords “class” or “typename”. Main advantages of templates are that they …

C++ Templates Read More »

C++ Namespaces

What is a namespace? Namespaces come in handy to avoid name conflicts when multiple programmers are involved. Any name in the namespace could be accessed using the scope resolution operator. To use the names without any qualifiers (::) keyword “using” is specified. e.g) “using namespace myspace1”. All the names are visible in the current scope. …

C++ Namespaces Read More »

C++ Virtual Functions

What is a virtual function? Virtual function is a polymorphism technique. Refers to performing the same operation in a hierarchy of classes. Typically used in scenarios where the base class pointer is used to hold derived class objects and perform the same operation. Refer example below. When a virtual functions is called on a base …

C++ Virtual Functions Read More »

C++ Typecasting

What are the typecasting mechanisms in C++? C++ provides a casting syntax with 4 reserved words, dynamic_cast, const_cast, static_cast and reinterpret_cast. What is dynamic_cast? dynamic_cast is used to perform type conversions at run-time. dynamic_cast operator can be used only on polymorphic classes. i.e. virtual functions are present. On success the dynamic_cast returns a pointer to …

C++ Typecasting Read More »

C++ Inheritance

What is inheritance? Inheritance is a mechanism of reusing existing classes thereby allowing rapid development. A new class is created as a type of an existing class using the syntax “class : public , public …” where “public” is an access specifier. The reused class is refered as base class and the new class is …

C++ Inheritance Read More »

C++ Observer Pattern

What is Observer Pattern? Observer pattern is a behavioral design pattern. Observer pattern is used to solve the problem of notifying multiple objects of a change to keep them in sync like the Model-View-Controller (MVC) concept. Useful for event management kind of scenarios. Two classes are involved. The Observable class is where the actual data …

C++ Observer Pattern Read More »

C++ Debugging

Using Assertions Assertions are a good mechanism to test the conditions in a program that are expected to be true. If an assert statement fails then program in aborted with a message. This can be used a good source for debugging during development. Asserts can be quietly turned off when production code is to be …

C++ Debugging Read More »

C++ Factory Pattern

Factory Pattern Factory pattern is a creational design pattern. Idea of the factory patterns is to localize the object creation code. This prevents disturbing the entire system for a new type introduction. Typically when a new type is introduced in the system, change is at one place only where the object is created to decide …

C++ Factory Pattern Read More »

C++ Auto_Ptr

What is auto_ptr? auto_ptr is a smart pointer. Owns a dynamically allocated object and performs cleanup when not needed. Prevents memory leaks The object destructor is called during destruction. “release()” method could be used to take manual ownership of the object. EXAMPLE: Demonstrate the auto_ptr releasing dynamically allocated object. #include <iostream>using namespace std;class MyClass {int …

C++ Auto_Ptr Read More »