Monday, June 10, 2013

Name Space in C++


Usage :

To create a separate region for a group of variables, functions and classes etc

Creating Namespace :

namespace
{
     Members of namespace ;

}

Usage Namespace_name::member_variable_name.

namespace myconstants {
 const double pi = 3.141592;
}
 
usage =>  myconstants::pi
 
Namespaces have the following important points:

1) We can have more than one namespace of the same name. This gives the advantage of defining the same namespace in more than one file (although they can be created in the same file as well).

2)We can have anonymous namespaces (namespace with no name). They are directly usable in the same program and are used for declaring unique identifiers. It also avoids making global static variable.

The “anonymous” namespace you have created will only be accessible within the file you created it in.

3) C++ has a default namespace named std, which contains all the default library of the C++ included using #include directive.

Using Namespaces

Namespaces are used with the ‘using’ keyword, which makes all the members of the namespace available in the current program and the members can be used directly, without taking reference of the namespace.

Namespaces are also usable by taking their name reference with scope resolution operator. This method allows distinguishing between the members of the same name in two different namespaces.

ex:

using namespace std; and use cout directly
        else
use std::cout .

Namespace alias

It is also possible to declare an alternate name for an existing namespace.
 namespace new_name = current_name;

 

 

 

Friday, June 7, 2013

RTTI (Run-time type information) in C++




  • What is RTTI ?

RTTI refers to run time type Information/identification.


A mechanism in cpp that exposes information about an object's data type at runtime.

Where it is used ?

Run-time type information can apply to simple data types, such as integers and characters, or to generic types

Why it is required ?

RTTI is used to know the type of an object at runtime.


  • Where it is used ?

  • How RTTI is used in CPP

In C++ RTTI can be achieved with the help of two ways

1.typeid
operator  is defined in header file.
2.dynamic_cast
<> operation 


The C++ run-time type information permits performing safe typecasts and manipulate type information at run time.


If CShape is a base class and CCircle is a class drived from CShape. Then casting CShape object to CCircle object is said to be the downcasting and casting CCircle object to CShape object is said to be the upcasting. 


  • Sample Code

NOTE:
1.RTTI doesn't work with non ploymorphic classes.The classes which doesn't have the virtual function.
2.RTTI doesn't work with void pointers. 


Others :

This is a C++ specialization of a more general concept called type introspection.

 Similar mechanisms are also known in other programming languages, such as Delphi (Object Pascal).

RTTI is available only for classes which are polymorphic, which means they have at least one virtual method. In practice, this is not a limitation because base classes must have a virtual destructor to allow objects of derived classes to perform proper cleanup if they are deleted from a base pointer.

RTTI is optional with some compilers; the programmer can choose at compile time whether to include the function. There may be a resource cost to making RTTI available even if the program does not use it.




Monday, June 3, 2013

My Reference Links

TypeCasting in C++


Typecasting is the concept of converting the value of one type into another type.

 Implicit conversion or Automatic typecasting


Compiler automatically converts one type into another type. If the compiler converts a type it will normally give a warning.

Example  warning: conversion from ‘double’ to ‘int’, possible loss of data

Explicit conversion

#include <iostream>
using namespace std;

int main()
{
 int a;
 double b=2.55;
 a = b;       //Implicit by compiler with warning
 cout << a << endl;  
 a = (int)b; //Explicit C Style
 cout << a << endl;
 a = int(b); //Explicit C++ Style
 cout << a << endl; 
} 
O/P : All print 2