Object-oriented programming is based on three fundamental concepts: data abstraction, inheritance, and dynamic binding. In C++ we use classes for data abstraction and class derivation to inherit one class from another: A derived class inherits the members of its base class(es). Dynamic binding lets the compiler determine at run time whether to use a function defined in the base or derived class.
Inheritance and dynamic binding streamline our programs in two ways: They make it easier to define new classes that are similar, but not identical, to other classes, and they make it easier for us to write programs that can ignore the details of how those similar types differ.
By default, function calls in C++ do not use dynamic binding. To trigger dynamic binding, two conditions must be met: First, only member functions that are specified as virtual can be dynamically bound. By default, member functions are not virtual; nonvirtual functions are not dynamically bound. Second, the call must be made through a reference or a pointer to a base-class type.
In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP). (Wikipage)
Virtual functions overcome the problems with the type-field solution by allowing the programmer to declare functions in a base class that can be redefined in each derived class. The distinction between virtual and non-virtual resolves this ambiguity. If the function in question is designated "virtual" in the base class then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called. C++ non-virtual function calls are resolved at compile time with static binding, while virtual function calls are resolved at run time with dynamic binding
A destructor in base class need to be declared virtual.
Calling a method with an object pointer always invokes:
» the most derived class function, if a method is virtual
» the function implementation corresponding to the object pointer type (used to call the method), if a method is non-virtual
A virtual destructor works in the same way A destructor gets called when an object goes out of scope or when we call delete on an object pointer When any derived class object goes out of scope, the destructor of that derived class gets called first It then calls its parent class destructor so memory allocated to the object is properly released. But, if we call delete on a base pointer which points to a derived class object, the base class destructor gets called first (for non-virtual function). We should use virtual destructors if we call delete on a base class pointer which points to a derived class
=========
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type. The library containers and iterators are examples of generic programming. There is a single definition of each container, such as vector, but we can define many different kinds of vectors that differ by the element type that the vector contains. Similarly, we can, and have, used templates without understanding how they are defined.
A template is a blueprint or formula for creating a class or a function. A function template is a type-independent function that is used as a formula for generating a type-specific version of the function. For example, the standard library defines a single class template that defines what it means to be a vector. That template is used to generate any number of type-specific vector classesfor example, vector<int> or vector<string>.
reference <C++ primer>
March 29, 2011
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment