473,503 Members | 1,691 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 1594
On 28 Mar, 12:25, vb.h...@gmail.com 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.com 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, "Mathematician" <mathemtician1234567...@yahoo.com>
wrote:
On Mar 28, 2:25 am, vb.h...@gmail.com 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.com 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.com 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 objektorientierter Datenverarbeitung
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.com wrote:
On Mar 28, 3:48 pm, "Mathematician" <mathemtician1234567...@yahoo.com>
wrote:


On Mar 28, 2:25 am, vb.h...@gmail.com 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.com 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...@gmail.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.com 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 objektorientierter Datenverarbeitung
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...@gmail.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.com 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 objektorientierter Datenverarbeitung
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
On Mar 28, 4:50 pm, "Mathematician" <mathemtician1234567...@yahoo.com>
wrote:
On Mar 28, 6:17 am, "mliptak" <Meht...@gmail.comwrote:
On Mar 28, 3:34 pm, "James Kanze" <james.ka...@gmail.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.com 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 objektorientierter Datenverarbeitung
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
of course it's not..
if Base::g() is not virtual, then Base::g() would be invoked
however in my example, Derived::g() is invoked
Mar 28 '07 #11

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

Similar topics

11
4894
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.,...
6
1600
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. ...
8
1341
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...
6
1806
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 */...
2
2041
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...
2
1045
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...
8
1570
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...
10
1557
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
1819
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...
11
3879
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...
0
7203
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
7089
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
7282
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,...
1
6995
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...
0
5581
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,...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.