473,657 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused .. What is happenning here

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.
#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}
};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;
}

Regards,
vb

Mar 28 '07 #1
10 1611
On 28 Mar, 12:25, vb.h...@gmail.c om wrote:
Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.
Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just
as well with references as with pointers. So under the hood the same
thing happens when you call print() on the pointer as on the
reference.

--
Erik Wikström

Mar 28 '07 #2
On Mar 28, 2:25 am, vb.h...@gmail.c om wrote:
Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}

};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;

}

Regards,
vb
You mean the memory leak ?

Mar 28 '07 #3
On Mar 28, 3:48 pm, "Mathematic ian" <mathemtician12 34567...@yahoo. com>
wrote:
On Mar 28, 2:25 am, vb.h...@gmail.c om wrote:
Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.
#include <iostream>
using namespace std;
class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}
};
class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}
};
int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();
//using reference
base &c = d;
c.print();
return 0;
}
Regards,
vb

You mean the memory leak ?
Ok .. Barring the memory leak .. :-)

Mar 28 '07 #4
On Mar 28, 12:44 pm, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 28 Mar, 12:25, vb.h...@gmail.c om wrote:
Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just
No, you don't need pointers nor references in order to invoke member
functions polymorphically .
as well with references as with pointers. So under the hood the same
thing happens when you call print() on the pointer as on the
reference.

--
Erik Wikström

Mar 28 '07 #5
On Mar 28, 2:08 pm, "mliptak" <Meht...@gmail. comwrote:
On Mar 28, 12:44 pm, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 28 Mar, 12:25, vb.h...@gmail.c om wrote:
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.
Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just
No, you don't need pointers nor references in order to invoke member
functions polymorphically .
I'm not sure what you're trying to say. Dynamic polymorphism
only occurs when the dynamic type of an object can differ from
the static type, and in C++, that pretty much means pointers or
references.

--
James Kanze (GABI Software) mailto:ja****** ***@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34

Mar 28 '07 #6
On Mar 28, 3:31 pm, vb.h...@gmail.c om wrote:
On Mar 28, 3:48 pm, "Mathematic ian" <mathemtician12 34567...@yahoo. com>
wrote:


On Mar 28, 2:25 am, vb.h...@gmail.c om wrote:
Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.
#include <iostream>
using namespace std;
class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}
};
class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}
};
int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();
//using reference
base &c = d;
c.print();
return 0;
}
Regards,
vb
You mean the memory leak ?

Ok .. Barring the memory leak .. :-)- Hide quoted text -

- Show quoted text -
TIP:'delete' pointers allocated via 'new'

Mar 28 '07 #7
On Mar 28, 2:25 pm, vb.h...@gmail.c om wrote:
Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}

};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;

}

Regards,
vb
under the hood: refference is compiled as if it where a pointer but it
improves C++ interfacing a lot.

Mar 28 '07 #8
On Mar 28, 3:34 pm, "James Kanze" <james.ka...@gm ail.comwrote:
On Mar 28, 2:08 pm, "mliptak" <Meht...@gmail. comwrote:
On Mar 28, 12:44 pm, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 28 Mar, 12:25, vb.h...@gmail.c om wrote:
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.
Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just
No, you don't need pointers nor references in order to invoke member
functions polymorphically .

I'm not sure what you're trying to say. Dynamic polymorphism
only occurs when the dynamic type of an object can differ from
the static type, and in C++, that pretty much means pointers or
references.
What I meant is that virtual op can be invoked from non-virtual in the
base class, e.g.:

class Base
{
public:
void f()
{ g(); }
virtual void g() {}
};

class Derived : public Base
{
public:
void g() {}
};

int main()
{
Derived d;
d.f();
return 0;
}
>
--
James Kanze (GABI Software) mailto:james.ka ...@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34

Mar 28 '07 #9
On Mar 28, 6:17 am, "mliptak" <Meht...@gmail. comwrote:
On Mar 28, 3:34 pm, "James Kanze" <james.ka...@gm ail.comwrote:


On Mar 28, 2:08 pm, "mliptak" <Meht...@gmail. comwrote:
On Mar 28, 12:44 pm, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 28 Mar, 12:25, vb.h...@gmail.c om wrote:
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference.And
both of them printed the same thing.
I want to know what is going on under the hood.
Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just
No, you don't need pointers nor references in order to invoke member
functions polymorphically .
I'm not sure what you're trying to say. Dynamic polymorphism
only occurs when the dynamic type of an object can differ from
the static type, and in C++, that pretty much means pointers or
references.

What I meant is that virtual op can be invoked from non-virtual in the
base class, e.g.:

class Base
{
public:
void f()
{ g(); }
virtual void g() {}

};

class Derived : public Base
{
public:
void g() {}

};

int main()
{
Derived d;
d.f();
return 0;

}
--
James Kanze (GABI Software) mailto:james.ka ...@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
virtual key is meaningless, whether you use it or not, the output's
still unchanged, you just handle a simple scope problem that you call
g() via f() on purpose

Mar 28 '07 #10

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

Similar topics

11
4941
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g., http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Tidy7IDbDHA.2108%40cpmsftngxa06.phx.gbl) that indicate that ASP will queue up requests when they come in with the same "session".
6
1618
by: ree32 | last post by:
I am a bit confused with capabilities of XML. I have an XML document with information on images(photos). Is there way to use XSL/XSLT to create a page that will display the images as gallery. I am a bit confused as whether I have to run XT processor each time I update the XML file. Or is the does the web browser come with XT processor which will automatically load the xml document and transform it using an XSLT?
8
1352
by: Chris | last post by:
Hi, I was just asigned a project and was doing some research and am now confused with Enterprise Service, Remoting and Web Services. Now I am not sure which will be benificial to my project. I need to create and expose a component to process credit card payment and log the details to a database. The component will be used by an asp.net app locally and then our sister company need to send payment to the same component. My idea is to create...
6
1813
by: m_a_t_t | last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on chapter 1.5.1 and here's the program you're sposed to make: #include <stdio.h> /* copy input to output; 1st version */ main() { int c;
2
2055
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP (non-.Net) if I wanted to fill a list it may look something like this: -------START CODE <%
2
1052
by: Moe | last post by:
Hello, I am very confused. Here is my situation: I have a web form with a few fields in place that are used as search criteria for a SQL database. Since I want the search criteria to be specific, I allow users to select values from a dropdown list, and add them to a listbox. When the users selects a value from the dropdown list and clicks "add", the page posts back very quickly and adds the value into the listbox. This works as it...
8
1580
by: Najib Abi Fadel | last post by:
Hi all i am running PostgreSQL 7.3.2, i have a VIEW for which i implemented multiple RULES on UPDATE. The weird think is that the Update Query corresponding to one of the rules is updating MULTIPLE ROWS even though it should only update one ROW !! THE WEIRDEST is that when i remove 2 of the update Rules on the VIEW The Update Query Works FINE !!!! WHY IS THAT HAPPENNING ??
10
1578
by: Xiaoshen Li | last post by:
Dear All, I am confused with prototypes in C. I saw the following code in a C book: void init_array_1(int data) { /* some code here */ }
13
1832
by: Marvin | last post by:
Hi: I have been programming with OOP and C++ for over 10 years but I am new to javascript. I am very confused about one aspect and would appreciate it if someone could explain the differences between the following 2 groups of "statements": function myFunc (x) { a = 1;
11
3907
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try to do some example to explain, However, after some trying, I confused when I try to do some integer constant division, I know I should do float division something like 3.0/6.0, but I'm still confused where the -0.124709 comes for the following...
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.