--- In [email protected], Gopi Krishna Komanduri <gopikomand...@...> wrote:
>
> HI,
>    I have a small query. In virtual inheritence , the
> function wil be called basing on the object we assign
> for the base class pointer. But how variable address
> will be resolved. I observed that even variable are
> getting resolved basing on the object we assign (like
> function). But I think there is no virtual concept for
> variables. only for functions.

Correct.

> 
> #include<stdio.h>
> #include<conio.h>

Use C++ standard headers. [See my other post.]

> class base
> {
> public:
>     int var1 , var2;
>     base(int a,int b)

 : var1(a), var2(b)

>     {
>         var1=a;
>         var2=b;
>         printf("\n I am in base. var1 = %d , var2 = %d",var1 , var2);
>     };

Don't put ; at the end of function definitions.

>     virtual void show()
>     {
>         printf("\n I am in base class virtual method . show.
> My var1 value is %d , var 2 value is %d",var1 , var2);
>     };
>     void disp()
>     {
>         printf("\n I am in  base class non virtual method.
> disp. My var1 is %d , var2 is %d",var1 , var2);
>     };
> };
> class derived:public base
> {
> public:
>     int var1 , var2;

You now have two var1 variables in derived, base::var1 and
derived::var1. Similarly for var2.

>     derived(int a,int b):base(a+1,b+1)
>     {
>         var1=a;
>         var2=b;
>         printf("\n I m in derived class. My var1 is %d , var 2
> is %d ",var1 , var2);
>     };
>     void show()
>     {
>         printf("\n I am in defrived class virtual method .
> show. My var1 value is %d , var 2 value is %d",var1 , var2);
> 
>     }
>     void disp()
>     {
>         printf("\n I am in  derived class non virtual method.
> disp. My var1 is %d , var2 is %d",var1 , var2);
>     }
> };
> void main()

  int main()

> {
>     base *x = new derived(1,2);
>     
>     base *y = new base(10,20);
>     x->show();  /// show here displaying 1,2 instead of base
> variable values. But I think there is no virtual concept here.
> so I think it should display base class variable values. please
> clarify my confusion.

To show the base var1, you'll need to use base::var1.

>     printf("\n \n ]\n");
>     y->show();
> 
> }

-- 
Peter

Reply via email to