473,503 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cline/Lomow Book (Original) FAQ # 158

Tom
FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.

Below is a copy of the FAQ 158 program:

=========================================

class Printer2 {
public :
virtual ~Printer2( ) { }
virtual void italics(const char* s) = 0;
};

class EpsonPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "i+" << s << esc << "i-"; }
};

class ProprinterPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "[i" << s << esc << "[n"; }
};

class StarPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "x" << s << esc << "y"; }
};

void
printUsingItalics ( Printer2& p, const char* s)
{
p.italics(s);
}
Dec 15 '05 #1
13 1277

Tom wrote:
FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.

Below is a copy of the FAQ 158 program:

=========================================

class Printer2 {
public :
virtual ~Printer2( ) { }
virtual void italics(const char* s) = 0;
};

class EpsonPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "i+" << s << esc << "i-"; }
};

class ProprinterPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "[i" << s << esc << "[n"; }
};

class StarPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "x" << s << esc << "y"; }
};

void
printUsingItalics ( Printer2& p, const char* s)
{
p.italics(s);
}


Since italics is a virtual function, it is bound dynamically. In other
words, the exact version of italics is selected from the actual class
held by the reference p.

Dec 15 '05 #2
Tom schrieb:
FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.


Hi,

http://www.parashift.com/c++-faq-lit...functions.html

has a good explanation [see 20.4]

regards marcas
Dec 15 '05 #3
Tom
On 15 Dec 2005 00:22:27 -0800, "Neelesh Bodas"
<ne***********@gmail.com> wrote:

Tom wrote:
FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.

Below is a copy of the FAQ 158 program:

=========================================

class Printer2 {
public :
virtual ~Printer2( ) { }
virtual void italics(const char* s) = 0;
};

class EpsonPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "i+" << s << esc << "i-"; }
};

class ProprinterPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "[i" << s << esc << "[n"; }
};

class StarPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "x" << s << esc << "y"; }
};

void
printUsingItalics ( Printer2& p, const char* s)
{
p.italics(s);
}


Since italics is a virtual function, it is bound dynamically. In other
words, the exact version of italics is selected from the actual class
held by the reference p.


Thanks for the reply Neelesh. I am definately struggling in the
transition to OO. Some of the examples within "C++ FAQs" are far more
advanced than my current skill level. The examples I guess are not
complete blocks of code? In particular the main( ) is missing? Your
response made me reread chapter #23 on references and referents. (130
pages deeper into the book. I have read it cover-to-cover and will
have to repeatedly it seems.)

So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}
Dec 15 '05 #4
Tom wrote:

So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}


I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

Better is to do this :

Printer2 *p = new StarPrinter2;
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)

Dec 15 '05 #5

Neelesh Bodas wrote:
Tom wrote:

So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}
I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.


That's not a problem. You don't get slicing when you pass by reference
like that. You get slicing when you pass a derived object by *value* to
a function expecting a base object. Have you ever thrown an exception
derived from std::exception then caught a std::exception&. It works.
Better is to do this :

Printer2 *p = new StarPrinter2;
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );


Dynamic allocation and pointers are not necessary to avoid slicing. So,
as ever, if you don't need to manually control the lifetime of the
object, don't use dynamic allocation.

Gavin Deane

Dec 15 '05 #6
Neelesh Bodas wrote:
Tom wrote:

So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}
I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.


Incorrect. Passing an object by value might slice it, but passing by
pointer OR by reference will both produce correct polymorphic behavior
and without chance of slicing. See these FAQs:

http://www.parashift.com/c++-faq-lit....html#faq-31.1
http://www.parashift.com/c++-faq-lit....html#faq-31.8
Better is to do this :

Printer2 *p = new StarPrinter2;
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)


Better at least to use std::auto_ptr (or boost::scoped_ptr,
boost::shared_ptr, etc.) if you're going to use new. The OP's code is
better in my opinion since it avoids dynamic allocation altogether when
it is unnecessary.

Cheers! --M

Dec 15 '05 #7
Neelesh Bodas wrote:
Tom wrote:
So to make the code complete ... one needs to add something along the
following lines?
What are you talking about?

Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2 No, it's an attempt to instantiate an abstract base class -- which you
can't do.

char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}

I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

Better is to do this :

Printer2 *p = new StarPrinter2;


See above.
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)

Please have *some* idea what you're doing before posting.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 15 '05 #8

Artie Gold wrote:
What are you talking about?

Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.
My reply was w.r.t. the tom's post and I just quoted the "only relavent
portions from that". May be I quoted less, will take care in future.

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2 No, it's an attempt to instantiate an abstract base class -- which you
can't do.
StartPrinter2 is a derived class, derived from the base class Printer2
which is abstract. In other words, I am _not_ instantiating ABC - I am
simply creating an object of a derived class.

I believe that the confusion is due to the fact that OP has mis-spelled
italics as intalics in all derived classes. But looking at the context,
it was clear to me that OP actually meant italics at all these places.

char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}

I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

Better is to do this :

Printer2 *p = new StarPrinter2;


See above.


Same applies here. I am _not_ instantiating an ABC.
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)

Please have *some* idea what you're doing before posting.


OK. Thanks for that. Will remember in future.

Dec 15 '05 #9
Artie Gold schrieb:

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2


No, it's an attempt to instantiate an abstract base class -- which you
can't do.


Hi,

it is true, that Printer2 is an abstract class. He makes an instance
of StarPrinter2, which is a non abstract class due to replacemant of the
pure virtual function. I think, that it is ok to assume that he meant
"italics" instead of "intalics".

regards marcas
Dec 15 '05 #10
Artie Gold wrote:
Neelesh Bodas wrote:
Tom wrote:
So to make the code complete ... one needs to add something along the
following lines?
What are you talking about?
The OP doesn' understand the code snippet because it isn't a complete
program. He's supplying what's lacking. (Or are you just noting that he
didn't quote the whole message?)
Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2

No, it's an attempt to instantiate an abstract base class -- which you
can't do.

[snip]

Clearly, there was just a typo ("intalics" instead of "italics"), and
this, as stated, is an attempt to instantiate (or, "create") a derived
object. What's your objection, exactly?

Cheers! --M

Dec 15 '05 #11

mlimber wrote:
I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.


Incorrect. Passing an object by value might slice it, but passing by
pointer OR by reference will both produce correct polymorphic behavior
and without chance of slicing. See these FAQs:


Yes you are right. Aplogoies.
Thanks (also to deane) for pointing out. It was a silly thing on my
part to get confused in this point.

Dec 15 '05 #12
mlimber wrote:
Artie Gold wrote:
Neelesh Bodas wrote:
Tom wrote:
So to make the code complete ... one needs to add something along the
following lines?


What are you talking about?

The OP doesn' understand the code snippet because it isn't a complete
program. He's supplying what's lacking. (Or are you just noting that he
didn't quote the whole message?)

Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2


No, it's an attempt to instantiate an abstract base class -- which you
can't do.


[snip]

Clearly, there was just a typo ("intalics" instead of "italics"), and
this, as stated, is an attempt to instantiate (or, "create") a derived
object. What's your objection, exactly?

Cheers! --M


Point taken.
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 15 '05 #13
Tom
My many thanks to those who responded to my confusion about FAQ #158.
I have learned more following the discussion and from the online
references I was pointed towards. It was definitely an error on my
part misspelling the overrides in the derived classes. There's a lot
of very smart folks in this group. A bit intimidating for sure, but if
I can just draft along the education will be a thrill.

BTW - I adjusted my font size in Agent editor and maybe now I won't
need stronger reading glasses and might catch my spelling errors more
easily.
Dec 16 '05 #14

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

Similar topics

4
2422
by: Steve Richfie1d | last post by:
I wrote my first BASIC compiler when Bill Gates was going to Lakeside School, and am believed to be the original inventor of the ON ERROR statement. Now, my son wants to learn Visual Basic, but...
23
4092
by: Anthony | last post by:
Hello I'm currently trying to read Core Python Programming, but by the looks of it im never going to get done the book is about 860 pages long real intimidating, but im guess im going to have to...
4
3294
by: Chris | last post by:
I'm wondering what is the "best" way to structure a set of info about an item, like a book or a hotel. Eg, let's say you had this info to present for each book in a collection: Title Author...
8
1386
by: Andy Turner | last post by:
I'm thinking of buying this book because I like the angle it's coming from and I found his previous DCOM book more enlightening than most. The thing is, it's 18 months old now (an age in .NET...
4
1364
by: Alexia | last post by:
Hi, Im sorry if this is the incorrect place to post this message. Can anyone reccomend a good crystal reports book. Im a complete beginner and would like some examples using vb.net? any...
7
5238
by: tada991 | last post by:
Hello Everyone, I just purchased Visual Studio .Net Architect 2003 and want to know what's a good book for begginers to start with. I know nothing about programming whatsoever, but I do have a...
4
1779
by: Pat | last post by:
Hi, Could you please suggest a good reference book for VB.NET? At present, I am working on VB.NET project. and looking for a book that would be useful as a reference book. Also, I would...
4
1543
by: blueghost73 | last post by:
I do software support for software that works with both Oracle and SQL Server, so I mostly just write queries to look at the data related to the software. When I first started, I bought a couple of...
1
1809
by: tao3256 | last post by:
Hi, all: Does anyone try to do C++ recoding of the original C code of W. R. Stevens' book "Advanced Programming in the Unix Environment", ISBN:0201563177, 1992 ? I got the redhat linux version...
0
7093
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
7287
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
7348
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...
1
7006
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
7467
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...
0
3175
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
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
397
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.