do your search here :) :-

Google
 

Thursday, January 24, 2008

Inheritance

Inheritance allows one data type to acquire properties of other data types. Inheritance from a base class may be declared as public, protected, or private. This access specifier determines whether unrelated and derived classes can access the inherited public and protected members of the base class. Only public inheritance corresponds to what is usually meant by "inheritance". The other two forms are much less frequently used. If the access specifier is omitted, inheritance is assumed to be private for a class base and public for a struct base. Base classes may be declared as virtual; this is called virtual inheritance. Virtual inheritance ensures that only one instance of a base class exists in the inheritance graph, avoiding some of the ambiguity problems of multiple inheritance.
Multiple inheritance is a C++ feature sometimes considered controversial. Multiple inheritance allows a class to be derived from more than one base class; this can result in a complicated graph of inheritance relationships. For example, a "Flying Cat" class can inherit from both "Cat" and "Flying Mammal". Some other languages, such as Java or C#, accomplish something similar (although more limited) by allowing inheritance of multiple interfaces while restricting the number of base classes to one (interfaces, unlike classes, provide only declarations of member functions, no implementation or member data).

[edit] Polymorphism
See also:
Polymorphism in object-oriented programming
Polymorphism enables one common interface for many implementations, and for objects to act differently under different circumstances.
C++ supports several kinds of static (
compile-time) and dynamic (run-time) polymorphism. Compile-time polymorphism does not allow for certain run-time decisions, while run-time polymorphism typically incurs a performance penalty.

[edit] Static polymorphism

[edit] Function overloading
Function overloading allows programs to declare multiple functions having the same name (but with different arguments). The functions are distinguished by the number and/or types of their formal parameters. Thus, the same function name can refer to different functions depending on the context in which it is used. The type returned by the function is not used to distinguish overloaded functions.

[edit] Default arguments
Default arguments are used when defining a different function is not needed when supplying a default value for an argument will suffice.

[edit] Class and function templates
Templates in C++ provide a sophisticated mechanism for writing generic, polymorphic code. In particular, through the Curiously Recurring Template Pattern it's possible to implement a form of static polymorphism that closely mimics the syntax for overriding virtual methods (a dynamic polymorphism technique described below). Since C++ templates are type-aware and Turing-complete they can also be used to let the compiler resolve recursive conditionals and generate substantial programs through template metaprogramming.

[edit] Dynamic polymorphism

[edit] Inheritance
Variable pointers (and references) to a base class type in C++ can refer to objects of any derived classes of that type in addition to objects exactly matching the variable type. This allows arrays and other kinds of containers to hold pointers to objects of differing types. Because assignment of values to variables usually occurs at run-time, this is necessarily a run-time phenomenon.
C++ also provides a dynamic_cast operator, which allows the program to safely attempt conversion of an object into an object of a more specific object type (as opposed to conversion to a more general type, which is always allowed). This feature relies on
run-time type information (RTTI). Objects known to be of a certain specific type can also be cast to that type with static_cast, a purely compile-time construct which is faster and does not require RTTI.

[edit] Virtual member functions
Ordinarily when a method in a derived class
overrides a method in a base class, the method to call is determined by the type of the object. A given method is overridden when there exists no difference, in the number or type of parameters, between two or more definitions of that method. Hence, at compile time it may not be possible to determine the type of the object and therefore the correct function to call, given only a base class pointer; the decision is therefore put off until runtime. This is called dynamic dispatch. Virtual member functions or methods allow the most specific implementation of the function to be called, according to the actual run-time type of the object. In C++, this is commonly done using virtual function tables. If the object type is known, this may be bypassed by prepending a fully qualified class name before the function call, but in general calls to virtual functions are resolved at run time.
In addition to standard member functions, operator overloads and destructors can also be virtual. A general rule of thumb is that if any functions in the class are virtual, the destructor should be as well. As the type of an object at its creation is known at compile time, constructors, and by extension copy constructors, can not be virtual. Nontheless a situation may arise where a copy of an object needs to be created when a pointer to a derived object is passed as a pointer to a base object. In such a case a common solution is to create a Clone() (or similar) method and declare that as virtual. The Clone() method creates and returns a copy of the derived class when called.
A member function can also be made "pure virtual" by appending it with = 0 after the closing bracket and before the semicolon. Objects can not be created of a class with a pure virtual function and are called abstract data types. Such abstract data types can only be derived from. Any derived class inherits the virtual function as pure and must override it (and all other pure virtual functions) with a non-pure virtual function for objects to be created from the derived class. An attempt to create an object from a class with a pure virtual function or inherited pure virtual function will be flagged as a compile-time error.
An example:
#include

class Bird // the "generic" base class
{
public:
virtual void OutputName() {std::cout << "a bird";}
virtual ~Bird() {}
};

class Swan : public Bird // Swan derives from Bird
{
public:
void OutputName() {std::cout << "a swan";} // overrides virtual function
};

int main()
{
Swan mySwan; // Creates a swan.

Bird* myBird = &mySwan; // Declares a pointer to a generic Bird,
// and sets it pointing to a newly created Swan.

myBird->OutputName(); // This will output "a swan", not "a bird".

return 0;
}
This example program makes use of virtual functions, polymorphism, and inheritance to derive new, more specific objects from a base class. In this case, the base class is a Bird, and the more specific Swan is made.

No comments:

TAKE A TOUR HERE ::-