C++ Questions

S

Samineni

I have two problems in C++ which I couldn't properly understand.
Appreciate if you can explain/eloborate more on this.

Problem 1:
-----------------

class Base
{
public:
virtual int fun(int i=10)
{
cout<<"Base::fun()"<<endl;
cout<<"The value of i="<<i<<endl;
}
};

class Derived : public Base
{
public:
int fun(int i=20)
{
cout<<"Derived::fun()"<<endl;
cout<<"The value of i="<<i<<endl;
}
};

void main()
{
Base* bptr = new Derived();
bptr->fun();
}

output:
---------
Derived::fun()
The value of i=10;

Here my question, how the default value is assigned to 10 ?

Problem 2:
--------------
class Base
{
public:
virtual int fun()
{
cout<<"Base::fun( )"<<endl;
}
virtual int fun(int i)
{
cout<<"Base::fun(int )"<<endl;
}
};

class Derived : public Base
{
public:
class Base
{
public:
virtual int fun()
{
cout<<"Derived::fun( )"<<endl;
}
};

void main()
{
Derived dObj;
dObj.fun();
dObj.fun(10); // Gives compile error.
}

Here my question why Derived Class instance doesn't inherit the
non-overridden methods directly even the other fucntion is virtual. In
case of non-virtual functions I know about this problem, but in this
case (vitrual fucntions) I am not why the compiler is complaining about
the non-overridden method. ?
 
C

Carlos Martinez

Samineni said:
I have two problems in C++ which I couldn't properly understand.
Appreciate if you can explain/eloborate more on this.

Problem 1:
-----------------

class Base
{
public:
virtual int fun(int i=10)
{
cout<<"Base::fun()"<<endl;
cout<<"The value of i="<<i<<endl;
}
};

class Derived : public Base
{
public:
int fun(int i=20)
{
cout<<"Derived::fun()"<<endl;
cout<<"The value of i="<<i<<endl;
}
};

void main()
{
Base* bptr = new Derived();
bptr->fun();
}

output:
---------
Derived::fun()
The value of i=10;

Here my question, how the default value is assigned to 10 ?
I think it's because you're using the interface of Base, then you're
using the rules of Base.
Due to polymorphism, program executes Derived::fun, but I think the
default parameter is established at compile time, and compiler only
knows that is of type Base*.
Only during compilation of line:
Base* bptr = new Derived();
knows about it is a object of Derived class. After this sentence, only
knows bptr is of type Base.

Problem 2:
--------------
class Base
{
public:
virtual int fun()
{
cout<<"Base::fun( )"<<endl;
}
virtual int fun(int i)
{
cout<<"Base::fun(int )"<<endl;
}
};

class Derived : public Base
{
public:
class Base
{
public:
virtual int fun()
{
cout<<"Derived::fun( )"<<endl;
}
};

void main()
{
Derived dObj;
dObj.fun();
dObj.fun(10); // Gives compile error.
}

Here my question why Derived Class instance doesn't inherit the
non-overridden methods directly even the other fucntion is virtual. In
case of non-virtual functions I know about this problem, but in this
case (vitrual fucntions) I am not why the compiler is complaining about
the non-overridden method. ?

For this problem, please, read:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9

It's explained better than I could
 
S

Samineni

Thanks Carlos
Now I am very clear about the question #1.
For 2nd Question, still I am not clear, why name hiding is happening in
case of virtual functions ? I understand this in case og non-virtual
functions. If the class contains at least one virtual function, one
V-Table will be formed and it contains the addresses of all virtual
functions. In the derived class if any of these virtual fucntions are
over rided, in the V-table of Derived class original function address
will be replaced by derived class's fucntion address. If u don't
override still it will have Base class fucntion address. Generally all
this happen in background if your class contain any virtual fucntions.
Why the same thing is not happening in this case.
 
C

Carlos Martinez

Samineni said:
Thanks Carlos
Now I am very clear about the question #1.
For 2nd Question, still I am not clear, why name hiding is happening in
case of virtual functions ? I understand this in case og non-virtual
functions. If the class contains at least one virtual function, one
V-Table will be formed and it contains the addresses of all virtual
functions. In the derived class if any of these virtual fucntions are
over rided, in the V-table of Derived class original function address
will be replaced by derived class's fucntion address. If u don't
override still it will have Base class fucntion address. Generally all
this happen in background if your class contain any virtual fucntions.
Why the same thing is not happening in this case.
Excuse me, Samineni. I don't know what's the reason for 2nd question.
I only know that's the C++ behaviour for that case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,890
Members
47,423
Latest member
henerygril

Latest Threads

Top