do your search here :) :-

Google
 

Thursday, January 24, 2008

Operators and operator overloading

C++ provides more than 30 operators, covering basic arithmetic, bit manipulation, indirection, comparisons, logic and more. Almost all operators can be overloaded for user-defined types, with a few notable exceptions such as member access (. and .*). The rich set of overloadable operators is central to using C++ as a domain specific language. As a simple example, a class that represents a matrix could overload the multiplication (*) and other arithmetic operators, allowing it to be treated by application code similarly to the standard numerical types.:
matrix A, B;
matrix C = A * B;
The overloadable operators are also an essential part of many advanced C++ programming techniques, such as
smart pointers.
Overloading an operator does not change the precedence of calculations involving the operator, nor does it change the number of operands that the operator uses (any operand may however be ignored).


Templates
See also:
generic programming and template metaprogramming
Templates are different from macros: while both of these compile-time language features can be used to produce conditional compilation, templates are not restricted to lexical substitution. Templates have an awareness of the semantics and type system of their companion language as well as all compile-time type definitions and can perform high-level operations including programmatic flow control based on evaluation of strictly type-checked parameters. Macros are capable of conditional control over compilation based on predetermined criteria but cannot instantiate new types, recurse or perform type evaluation and in effect are limited to pre-compilation text-substitution and text-inclusion/exclusion. In other words, macros can control compilation flow based on pre-defined symbols but cannot, unlike templates, independently instantiate new symbols. Templates are a tool for static
polymorphism (see below) and generic programming. For example, a template replacing the common, but dangerous, macro #define max(x,y) ((x)>(y)?(x):(y)):
template
const T& max(const T& x, const T& y)
{
return x > y ? x : y;
}
This can be found in the algorithm header as std::max(). Traditionally the keyword class may also be used in place of typename.
In addition, templates are a compile time mechanism in C++ which is
Turing-complete, meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram prior to runtime.
In summary, defining a template for a function or class is the equivalent of defining a function or class for each type that can be used as an argument, but does not require prior knowledge of which types will be used.

[edit] Objects
Main article:
C++ structures and classes
C++ introduces some object-oriented (OO) features to C. It offers classes, which provide the four features commonly present in OO (and some non-OO) languages: abstraction, encapsulation, inheritance and polymorphism. Objects are instances of classes created at runtime. Think of the class as a template from which many different individual objects may be generated as a program runs.

[edit] Encapsulation
Encapsulation is the grouping together of data and functionality. C++ implements encapsulation by allowing all members of a class to be declared as either public, private, or protected. A public member of the class is accessible to any function. A private member is accessible only to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends"). A protected member is accessible to members of classes that inherit from the class in addition to the class itself and any friends.
The OO principle is that all of the functions (and only the functions) that access the internal representation of a type should be encapsulated within the type definition. C++ supports this (via member functions and friend functions), but does not enforce it: the programmer can declare parts or all of the representation of a type to be public, and is also allowed to make public entities that are not part of the representation of the type. Because of this, C++ supports not just OO programming but other weaker decomposition paradigms, like
modular programming.
It is generally considered good practice to make all
data private or protected, and to make public only those functions that are part of a minimal interface for users of the class. This hides all the details of data implementation, allowing the designer to later fundamentally change the implementation without changing the interface in any way.

No comments:

TAKE A TOUR HERE ::-