473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why it call Derived ..,

include <iostream>
#include <unistd.h>
#include <stdlib.h>

class base{
public :
virtual void disp(){ std::cout << "Base Class\n";}
void show(){ std::cout << "Base Show\n"; }

};

class derived:public base{
public :
void disp(){ std::cout << "Derived Class\n"; }
void show(){ std::cout << "Derived Show\n";}
};
int main()
{
base *bptr;
derived *dptr = new derived;
bptr = (base *)dptr;

bptr->disp();

return 0;
}

Why does the bptr call derived version?? Even though I type casted
it..,
I know virtual func are called according to the type of object pointed
by the but not according to the type of pointer used to point to the
object,
Oct 11 '08 #1
3 1396
Pranav wrote:
Why does the bptr call derived version?? Even though I type casted
it..,
I know virtual func are called according to the type of object pointed
by the but not according to the type of pointer used to point to the
object,
First you ask a question and then you answer it. There's nothing left
for us to do.
Oct 11 '08 #2
On Oct 11, 6:42 am, Pranav <pranav...@gmail.comwrote:
include <iostream>
#include <unistd.h>
#include <stdlib.h>

class base{
public :
virtual void disp(){ std::cout << "Base Class\n";}
void show(){ std::cout << "Base Show\n"; }

};

class derived:public base{
public :
void disp(){ std::cout << "Derived Class\n"; }
void show(){ std::cout << "Derived Show\n";}

};

int main()
{
base *bptr;
derived *dptr = new derived;
bptr = (base *)dptr;

bptr->disp();

return 0;

}

Why does the bptr call derived version?? Even though I type casted
it..,
The pointer is pointing to an object, the fact that the pointer is
pointing to the base portion of that instance doesn't change the fact
that you are pointing to a derived object.
I know virtual func are called according to the type of object pointed
by the but not according to the type of pointer used to point to the
object,
It doesn't matter how you attempt to masquerade your base pointer, in
the end its the object that executes the virtual function call.

Picture some Animals in a series of cages. If you ask the 3rd Animal
(a cat) to talk(), would you expect anything less than a 'meaowww'?
Its not the pointer talking, its the cat.

Oct 11 '08 #3
On Oct 11, 4:02*pm, Salt_Peter <pj_h...@yahoo.comwrote:
Why does the bptr call derived version?? Even though I type casted
it..,
Even though 'disp' is not explicitly declared virtual in the derived
class, it is a virtual function,since it is declared so in 'base'.
Omitting 'virtual' keyword in the derived class does not change this.
It is a good practice, however, to use 'virtual' keyword also in
derived classes.
Oct 13 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
4
by: Amy Matlock | last post by:
Hi all: How does the hardware work if you invoke a BASE::METHOD() on a DERIVED class member? Do you still hit the v-table dynamically at run time? Suppose you have a derived class method, but...
3
by: John A. Prejean | last post by:
This one has me stumped. I have a base form I am trying to wrap up, but I have one problem. In two functions I am opening a "record detail" form. I would like to keep the code in the base form...
7
by: Rafał Maj Raf256 | last post by:
There was a way to trinck program to call purly abstract non-existing method (causing undefined behaviour), I would like to try that for curiosity (how different compilers/platforms react), how to...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
6
by: marcwentink | last post by:
Say I have class A that inherits from class B, and I call class A his constructor. Then somehow class B his constructor is called also. How does this work? Is a constructor under the hood a sort of...
1
by: mwebel | last post by:
Hi ikind of remember this being easy but cant find it on google: i have a base class and a derived class. The base class has two constructors normal and int overloaded one. thing is i want the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: Javier | last post by:
Hello, is this possible? I have a pure virtual function in the base class (to force the programmers of the derived classes to have this function implemented) but I want to call the derived class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.