I only have problem with the getdim() function within "class area". I don't understand what is the necessity of this function.
Futhermore, in the derived class when the virtual function overridden, getdim function is used outside the "class area". But the getdim function is not identified in the derived class.
Is it always possible to use a function to another class where the function is not identified??

Honestly, this code looks like it may be an overly-explanatory example from a text. The code contains many things that you generally won't see in well-designed/written code. I suggest you read the chapter it's part of a little closer. Here is a highly generalized summary (I want to avoid doing your homework for you).

These lines say that the rectangle and triangle classes inherit properties from the area class. Because they inherit from area, they have access to the members of the area class.

In actuality, because the functions/methods are declared as part of the "public" section of area, any piece of code can access them as long as there is an object of that type available to call them through.

getdim() is declared as public in the base class, and the inheritance is public, so the derived classes will have access to this method (as public).

This method retrieves the values of the dim1 and dim2 variables by reference. In triangle and rectangle , the values of d1 and d2 are passed into the method, and are assigned the values of dim1 and dim2 respectively.

getdim() is declared as public in the base class, and the inheritance is public, so the derived classes will have access to this method (as public).

This method retrieves the values of the dim1 and dim2 variables by reference. In triangle and rectangle , the values of d1 and d2 are passed into the method, and are assigned the values of dim1 and dim2 respectively.

Thanks a lot Jonsca.

But can't I output the same things without getdim() function?? This is not my homework but I just need the concept to understand some fundamental things of c++.

Unfortunately no. The variables area::dim1 and area::dim2 are private. This means that only members of the actual area class can access them directly. In order to allow rectangle and triangle to access them directly, they need to be re-classified as protected.

This is just one of the atypical things that I alluded to in my previous post.

Unfortunately no. The variables area::dim1 and area::dim2 are private. This means that only members of the actual area class can access them directly. In order to allow rectangle and triangle to access them directly, they need to be re-classified as protected.

This is just one of the atypical things that I alluded to in my previous post.

What would if I declare the dim1 and dim2 as public inside the class area?? Then can I delete the getdim() function??

Is it always necessary to declare the variable as private?? Everywhere I see that the variable always declare as private....what is the advantage of this and what is the disadvantage of declaring the variable inside the public??

Read Wikipedia's explanation of encapsulation to see some of the philosophy behind it.

You could certainly make it public, but what if you want people using your classes not to have to muck around with those things. As an example, what if you wanted to change the way you stored the member variables? If they are private, all you'd have to do is modify getdim() and you'd be good to go. Everyone could access the values like they always did. If the member variables were public, you'd need to modify each time they are used in the code.