Sai Stuff to Developers

June 20, 2012

Explaining briefly the working mechanism of virtual functions in C++

Filed under: C++,OOPS Languages — tosaik @ 10:45 am
Tags: , ,

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class.


Binding refers to the act of associating an object or a class with its member. If we can call a method fn() on an object o of a class c, we say that the object o is binded with the method fn(). This happens at compile time and is known as static or compile – time binding. The calls to the virtual member functions are resolved during run-time. This mechanism is known as dynamic binding. The most prominent reason why a virtual function will be used is to have a different functionality in the derived class. The difference between a non-virtual member function and a virtual member function is, the non-virtual member functions are resolved at compile time.

Working Process of Virtual Functions:

Whenever a program has a virtual function declared, a v – table is constructed for the class. The v-table consists of addresses to the virtual functions for classes that contain one or more virtual functions. The object of the class containing the virtual function contains a virtual pointer that points to the base address of the virtual table in memory. Whenever there is a virtual function call, the v-table is used to resolve to the function address. An object of the class that contains one or more virtual functions contains a virtual pointer called the vptr at the very beginning of the object in the memory. Hence the size of the object in this case increases by the size of the pointer. This vptr contains the base address of the virtual table in memory. Note that virtual tables are class specific, i.e., there is only one virtual table for a class irrespective of the number of virtual functions it contains. This virtual table in turn contains the base addresses of one or more virtual functions of the class. At the time when a virtual function is called on an object, the vptr of that object provides the base address of the virtual table for that class in memory. This table is used to resolve the function call as it contains the addresses of all the virtual functions of that class. This is how dynamic binding is resolved during a virtual function call.

The following code shows how we can write a virtual function in C++ and then use the same to achieve dynamic or runtime polymorphism.

#include <iostream.h>
class base
virtual void display()
class derived : public base
void display()

void main()

base *ptr = new derived();

In the above example, the pointer is of type base but it points to the derived class object. The method display() is virtual in nature. Hence in order to resolve the virtual method call, the context of the pointer is considered, i.e., the display method of the derived class is called and not that of the base. If the method was non virtual in nature, the display() method of the base class would have been called.

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.


Virtual methods should be used judiciously as they are slow due to the overhead involved in searching the virtual table. They also increase the size of an object of a class by the size of a pointer. The size of a pointer depends on the size of an integer. Note that in DOS based systems the size of a pointer is 2 bytes whereas in UNIX based systems it is 4 bytes.

Happy Coding 🙂

Short Description about Abstract Classes

Filed under: C++,OOPS Languages — tosaik @ 10:40 am
Tags: , ,

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

The following is an example of an abstract class:

class vehicle{


virtual void speed() = 0;


Function vehicle::speed is a pure virtual function. A function declaration cannot have both a pure specifier and a definition. For example, the compiler will not allow the following:

struct A {

virtual void g() { } = 0;


You cannot use an abstract class as a parameter type, a function return type, or the type of an explicit conversion, nor can you declare an object of an abstract class. You can, however, declare pointers and references to an abstract class. The following example demonstrates this:

class A {

virtual void f() = 0;


class B :public A {

virtual void f() { }


// Error:

// Class A is an abstract class

// A g();

// Error:

// Class A is an abstract class

// void h(A);

A& i(A&);

int main() {

// Error:

// Class A is an abstract class

//   A a;

A* pa;

B b;

// Error:

// Class A is an abstract class

//   static_cast<A>(b);


Class A is an abstract class. The compiler would not allow the function declarations A g() or void h(A), declaration of object a, nor the static cast of b to type A.

Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class.

For example:

class AB {


virtual void f() = 0;


class D2 : public AB {

void g();


int main() {

D2 d;


The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f()from AB. The compiler will allow the declaration of object d if you define function D2::g().

Note that you can derive an abstract class from a nonabstract class, and you can override a non-pure virtual function with a pure virtual function.

You can call member functions from a constructor or destructor of an abstract class. However, the results of calling (directly or indirectly) a pure virtual function from its constructor is undefined. The following example demonstrates this:

Class A {

A() {




virtual void direct() = 0;

virtual void indirect() { direct(); }


The default constructor of A calls the pure virtual function direct() both directly and indirectly (through indirect()).

The compiler issues a warning for the direct call to the pure virtual function, but not for the indirect call.

June 19, 2012

How Inheritance is importance in Object Oriented Programming?

Filed under: C++,DotNet,OOPS Languages — tosaik @ 5:41 pm
Tags: , , ,

One of the important features in the Object Oriented Programming is Reusability, as we said above that it is always good way to reuse the already existing functionality rather than trying to create the same one again and again. By reusing the properties not only saves the time and money but also increases the reliability.

An advantage of inheritance is that modules with sufficiently similar interfaces can share a lot of code, reducing the complexity of the program.

The Benefits of Inheritance

  • Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times.
  • Programmers can implement superclasses called abstract classes that define “generic” behaviors. The abstract superclass defines and may partially implement the behavior but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

Happy Coding 🙂

You are attempting to access powerpivot data that was added using a newer version of powerpivot

Filed under: PowerPivot — tosaik @ 5:33 pm

The Report which you are using is built on new power pivot version, for this reason reports are not working on old power pivot version and showing below error message.

The Report which you are using is built on new power pivot version, for this reason reports are not working on old power pivot version and showing below error message.

Resolution: Please install new version of power pivot (i.e. 11.0.2100.60) from below link.

Impact : If you are installed new power pivot version from above link existing reports which are built on old Power pivot will open with the below message in a dialogue box

Saying that “Do you want Upgrade the workbook”, if you click on OK the report will work fine.

June 4, 2012

Quick tour of Visual Studio 2012

Filed under: DotNet — tosaik @ 12:43 pm
Tags: ,




A quick tour of the new features in Visual studio 2012.

Code window resize

A feature you can turn on/off is automatic resizing of the code window. When you are editing your code, selecting the code window maximizes it at the expense of Solution Explorer, the Output pane, etc.

Edit code while it executes

You could do it with Visual Basic 6 back in the ’90s and finally you can do it in VS2012. While the code is executing, you can pause it (breakpoint / debugging) and modify the code. Just as it did back in the ’90s, the compiler will carry on working with the code you just wrote.

Google It!

They just made everyone’s life easier by providing a button called Google It! In case of any run time error message box.

Automatically add semicolons.

Rumor has it that the compiler will also automatically add closing brackets and braces.

Advanced copy and pasting of code

The art of developing good software includes the skill of copying and pasting code found on the Internet. To make your life easier, VS2012 will apply some sense to the pasted code and rewire it for you. Simply write your stub method…Ask a question at or maybe try Goggling the code you need. Once you have it, simply copy the code and paste it into your stub method…then the magic begins. The compiler has recognized and wired up your connection string, replaced “textBox1.Text” with “term”, and because you are returning a string from this method, it assumes that you want the first result from the table and so will suggest returning that for you.

Evil Code Monkey ™

After copying and pasting code, things can get pretty messy really quickly. IntelliSense now includes Evil Code Monkey™ which will point out your ugly code, shaming you into Goggling the correct way to write it.

Language Style

VB, C# or F# – it’s all just language styles!

When you are viewing code you can opt to have it translated from its original source into VB, C# or into some of the new styles.

Add Reference dialog

Finally they figured out what people meant when they asked for a proper “Add Reference” dialog. They now provide the ultimate mix of speed and flexibility.

The creation of high quality applications is also something which Microsoft is aiming for with the release of VS2012, with the inclusion of high quality testing and debugging tools that allow local debugging, as well as the ability to debug apps on the simulator and remotely on a connected device. Microsoft has undergone a lengthy process in an attempt to ensure that Visual Studio 2012 allows continuous value and quality to be delivered through the use of the software. With the inclusion of what they called Application Lifecycle Management, development teams of any size can be confident that required collaboration to deliver apps will be seamless and problem free.

The Visual Studio 2012 RC is available to download from the official VS product website and includes a lot of final touches and enhancements that makes it substantially different from the previously released beta versions of the product.

Visual Studio 2012 is available to download from the Visual Studio product website, with users being able to get their hands on the Ultimate, Premium and Professional versions along with Microsoft’s .NET Framework 4.5 RC required for the install. As always with pre-released builds, treat this installation accordingly and make sure to read the supplied read me files attached with the downloads.

Create a free website or blog at

%d bloggers like this: