473,770 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

derivation doubts

Hi I have the following doubts:

I have 2 class Point and the derived Circle. Point doesn't have
virtual function.

Now can you explain me when came the conversion (compile or run time)
when I do:

Point *pointPtr = 0, p( 30, 50 );
Circle *circlePtr = 0, c( 2.7, 120, 89 );

1
*************** *************** *************** *************** *************** *****
// Treat a Circle as a Point (see only the base class part)
pointPtr = &c; // assign address of Circle to pointPtr
cout << "\nCircle c (via *pointPtr): " ;
pointPtr.print( );

here at compile time pointPtr is always of Point type ???
and at run time it is of Circle type but because there are not a
vritual function is
called the base class print() ???

2
*************** *************** *************** *************** *************** *********
// Treat a Circle as a Circle (with some casting)
// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nCircle c (via *circlePtr):\n"
circlePtr->area();

here pontPtr is at compile time always of Point type but at run-time
it is of Circle
type and than we can make the CAST ??? and than area() is called on
it ???

3
*************** *************** *************** *************** *************** ************
// DANGEROUS: Treat a Point as a Circle
pointPtr = &p; // assign address of Point to pointPtr

// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nPoint p (via *circlePtr):\n"
circlePtr->area();

here that forced cast make at run time circlePtr of Point type
but the area() is not in it defined... and at compile time which is is
type ???????

AAARGHHH confused HELP ME!!!!

Mar 30 '07 #1
8 1413
josh wrote:
Hi I have the following doubts:
I have 2 class Point and the derived Circle. Point doesn't have
virtual function.

Now can you explain me when came the conversion (compile or run time)
when I do:

Point *pointPtr = 0, p( 30, 50 );
Circle *circlePtr = 0, c( 2.7, 120, 89 );
it's clearer if you stick to one declaration per line.

Point* pointPtr = 0;
Point p( 30, 50 );
Circle *circlePtr = 0;
Circle c( 2.7, 120, 89 );
1
*************** *************** *************** *************** *************** *****
// Treat a Circle as a Point (see only the base class part)
pointPtr = &c; // assign address of Circle to pointPtr
this should earn you a compiler diagnostic unless a Circle is a Point.
That is Circle is derived from Point. A semantically dubious
proposition
if Circles and Points are supposed to be anything like their geometric
counterparts.

cout << "\nCircle c (via *pointPtr): " ;
pointPtr.print( );

here at compile time pointPtr is always of Point type ???
you declared pointPtr to be a Point*. It can't change.
and at run time it is of Circle type but because there are not a
vritual function is
called the base class print() ???
post a complete compiable program. There's too mach guess work
involved at the moment.

<snip>
AAARGHHH confused HELP ME!!!!
and you can see the code. Think how I feel...
--
Nick Keighley

Mar 30 '07 #2

"josh" <xd********@gma il.comwrote in message
news:11******** **************@ d57g2000hsg.goo glegroups.com.. .
Hi I have the following doubts:

I have 2 class Point and the derived Circle. Point doesn't have
virtual function.

Now can you explain me when came the conversion (compile or run time)
when I do:

Point *pointPtr = 0, p( 30, 50 );
Circle *circlePtr = 0, c( 2.7, 120, 89 );
1
*************** *************** *************** *************** *************** *****
// Treat a Circle as a Point (see only the base class part)
pointPtr = &c; // assign address of Circle to pointPtr
cout << "\nCircle c (via *pointPtr): " ;
pointPtr.print( );
pointPtr is a pointer. You would have to use:
pointPtr->print();
or
(*pointPtr).pri nt();
here at compile time pointPtr is always of Point type ???
and at run time it is of Circle type but because there are not a
vritual function is
called the base class print() ???
Yes. If print is not declared as virtual in your base class, then it would
use the base classes' print method.
2
*************** *************** *************** *************** *************** *********
// Treat a Circle as a Circle (with some casting)
// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nCircle c (via *circlePtr):\n"
circlePtr->area();

here pontPtr is at compile time always of Point type but at run-time
it is of Circle
type and than we can make the CAST ??? and than area() is called on
it ???
Yes. Nothing really magic about this, because your pointPtr was in fact
pointing to an instance of a Circle.
3
*************** *************** *************** *************** *************** ************
// DANGEROUS: Treat a Point as a Circle
pointPtr = &p; // assign address of Point to pointPtr

// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nPoint p (via *circlePtr):\n"
circlePtr->area();

here that forced cast make at run time circlePtr of Point type
but the area() is not in it defined... and at compile time which is is
type ???????
Yes, static_cast would in fact treat your Point as a Circle, and as you can
see, bad things happen, because it is not in fact a circle and is missing
information. Safer is to use Dynamic cast, which would return 0 (NULL) if
it's not the right class.

circlePtr = dyanmic_cast< Circle* >( pointPtr );
if ( circlePtr != NULL )
cout << "\nPoint p (via *circlePtr):\n" ;
else
cout << "I'm pointing to nothing, NULL"\n";
>
AAARGHHH confused HELP ME!!!!

Mar 30 '07 #3
On 30 Mar., 12:01, "josh" <xdevel1...@gma il.comwrote:
Hi I have the following doubts:

I have 2 class Point and the derived Circle. Point doesn't have
virtual function.
To begin with, it is silly to derive a circle from a point. A circle
is not a point and therefore you should not derive.
Secondly, base-classes normally need virtual destructors (if not it
should be protected). A class such as Point most probably should NOT
have any virtual functions - one more indication that your design is
wrong.
>
Now can you explain me when came the conversion (compile or run time)
when I do:

Point *pointPtr = 0, p( 30, 50 );
Circle *circlePtr = 0, c( 2.7, 120, 89 );

1
*************** *************** *************** *************** *************** ******
// Treat a Circle as a Point (see only the base class part)
Well - that comment is misleading. In your design you do not treat a
Circle as a Point. A Circle IS a point.
pointPtr = &c; // assign address of Circle to pointPtr
cout << "\nCircle c (via *pointPtr): " ;
pointPtr.print( );
This will not compile: it should be pointPtr->print(). According to
your design, it should print the point.
>
here at compile time pointPtr is always of Point type ???
Yes. Assuming you have declared pointPtr to be a Point*
and at run time it is of Circle type but because there are not a
vritual function is
called the base class print() ???
Yes.
>
2
*************** *************** *************** *************** *************** **********
// Treat a Circle as a Circle (with some casting)
// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nCircle c (via *circlePtr):\n"
circlePtr->area();

here pontPtr is at compile time always of Point type but at run-time
it is of Circle
There is no pointPtr here. circlePtr is whatever type you have
declared it to (presumably Circle*)
type and than we can make the CAST ??? and than area() is called on
it ???

3
*************** *************** *************** *************** *************** *************
// DANGEROUS: Treat a Point as a Circle
pointPtr = &p; // assign address of Point to pointPtr

// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nPoint p (via *circlePtr):\n"
circlePtr->area();

here that forced cast make at run time circlePtr of Point type
but the area() is not in it defined... and at compile time which is is
type ???????

AAARGHHH confused HELP ME!!!!
It is simple: a pointer is always of the type you declared it to. So
pointPtr is always a pointer to a point, and circlePtr is always a
pointer to a circle. What confuses you is that a pointer might point
to something that is more than a point: a pointer to a baseclass could
point to a base-object but also to any object that derives from base.

/Peter

Mar 30 '07 #4
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:yM******** ********@newsfe 04.lga...
>
"josh" <xd********@gma il.comwrote in message
news:11******** **************@ d57g2000hsg.goo glegroups.com.. .
>Hi I have the following doubts:

I have 2 class Point and the derived Circle. Point doesn't have
virtual function.

Now can you explain me when came the conversion (compile or run time)
when I do:

Point *pointPtr = 0, p( 30, 50 );
Circle *circlePtr = 0, c( 2.7, 120, 89 );
>1
************** *************** *************** *************** *************** ******
// Treat a Circle as a Point (see only the base class part)
pointPtr = &c; // assign address of Circle to pointPtr
cout << "\nCircle c (via *pointPtr): " ;
pointPtr.print ();

pointPtr is a pointer. You would have to use:
pointPtr->print();
or
(*pointPtr).pri nt();
>here at compile time pointPtr is always of Point type ???
and at run time it is of Circle type but because there are not a
vritual function is
called the base class print() ???

Yes. If print is not declared as virtual in your base class, then it
would use the base classes' print method.
>2
************** *************** *************** *************** *************** **********
// Treat a Circle as a Circle (with some casting)
// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nCircle c (via *circlePtr):\n"
circlePtr->area();

here pontPtr is at compile time always of Point type but at run-time
it is of Circle
type and than we can make the CAST ??? and than area() is called on
it ???

Yes. Nothing really magic about this, because your pointPtr was in fact
pointing to an instance of a Circle.
>3
************** *************** *************** *************** *************** *************
// DANGEROUS: Treat a Point as a Circle
pointPtr = &p; // assign address of Point to pointPtr

// cast base-class pointer to derived-class pointer
circlePtr = static_cast< Circle * >( pointPtr );
cout << "\nPoint p (via *circlePtr):\n"
circlePtr->area();

here that forced cast make at run time circlePtr of Point type
but the area() is not in it defined... and at compile time which is is
type ???????

Yes, static_cast would in fact treat your Point as a Circle, and as you
can see, bad things happen, because it is not in fact a circle and is
missing information. Safer is to use Dynamic cast, which would return 0
(NULL) if it's not the right class.

circlePtr = dyanmic_cast< Circle* >( pointPtr );
if ( circlePtr != NULL )
cout << "\nPoint p (via *circlePtr):\n" ;
else
cout << "I'm pointing to nothing, NULL"\n";
>>
AAARGHHH confused HELP ME!!!!
All that being said, Peter is right, a Circle is not a point, so should not
be derived from it. However, a cirle HAS points. Better would be something
like:

class Circle
{
private:
Point Center_;
doouble Radius_;
};

class Rectangle
{
private:
Point Corners[4]; // Some would use std::vector here
};

class Triangle
{
private:
Point Corners[3]; // Some would use std::vector here
};

etc...

If you do need a base, I would use Shape.

class Shape
{
public:
double virtual Area() = 0
double virtual Circumferance() = 0
~Shape() {}
};

class Circle: public Shape
{
public:
Circle( const Point Center, double Radius ): Center_( Center ),
Radius_( Radius ) {}
double Area() { return 3.1415926 * radius_ * radius_; }
double Circumfernace() { return 3.141592 * radius_ * 2; }
private:
Point Center_;
double Radius_;
};

etc...
Mar 30 '07 #5
To begin with, it is silly to derive a circle from a point. A circle
is not a point and therefore you should not derive.
yes is better to make Point inside a Circle and sayng
that a Circle has like coords a Point (relationship "has a" and not
"is a")
A Circle IS a point.
because it is to it derived...
pointPtr.print( );
yes I post it wrong!

circlePtr = static_cast< Circle * >( pointPtr );
following the code flow at now at compile time pointPtr is Point* but
at run-time it is pointong to a Circle
It is simple: a pointer is always of the type you declared it to. So
pointPtr is always a pointer to a point, and circlePtr is always a
pointer to a circle. What confuses you is that a pointer might point
to something that is more than a point: a pointer to a baseclass could
point to a base-object but also to any object that derives from base.
yes I make confusion from compile and run time when I make
assignements operation
so what can I ask myself to understand what is happening?
Mar 30 '07 #6
On Mar 30, 6:38 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
Safer is to use Dynamic cast, which would return 0 (NULL) if
it's not the right class.
Unfortunately, that won't work here, because, as stated in the
original description, our classes don't have virtual functions, which
are needed for dynamic_cast<to work.
Mar 30 '07 #7
On Mar 30, 7:44 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
class Rectangle
{
private:
Point Corners[4]; // Some would use std::vector here

};
Hmmmm... Now, while I really like std::vector and use it all the time,
I'm completely baffled why anyone would choose to use one there, where
it is assured that we can only ever have exactly four points.

Mar 30 '07 #8
"josh" <xd********@gma il.comwrote in message
news:11******** **************@ d57g2000hsg.goo glegroups.com.. .
>To begin with, it is silly to derive a circle from a point. A circle
is not a point and therefore you should not derive.

yes is better to make Point inside a Circle and sayng
that a Circle has like coords a Point (relationship "has a" and not
"is a")
>A Circle IS a point.

because it is to it derived...
pointPtr.print( );

yes I post it wrong!

circlePtr = static_cast< Circle * >( pointPtr );

following the code flow at now at compile time pointPtr is Point* but
at run-time it is pointong to a Circle
>It is simple: a pointer is always of the type you declared it to. So
pointPtr is always a pointer to a point, and circlePtr is always a
pointer to a circle. What confuses you is that a pointer might point
to something that is more than a point: a pointer to a baseclass could
point to a base-object but also to any object that derives from base.
yes I make confusion from compile and run time when I make
assignements operation
so what can I ask myself to understand what is happening?
What can you ask yourself?

What is this pointer actually pointing to. An instance of what. What it
points to is not changing. Only how you treat the pointer.
Mar 30 '07 #9

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

Similar topics

5
1777
by: Robert Brewer | last post by:
WARNING: The following is NOT all Pythonic. However, it is Python. This is just fun--nothing I'm going to put into production. I don't have any questions or problems--just thought I'd share. BACKGROUND: I've been playing around for a couple of days with parsing and
2
1454
by: Pierre Rouleau | last post by:
Greetings, I'm wondering why the >> operator does not use the write() method of a class derived from the built-in file class as in DerivedFile below. In the following example: - StringFile is just a file-like string accumulation class that can be used in place of a real file to accumulate strings that would otherwise be printed. Works fine, can accumulate strings with the >> operator,
0
1519
by: abbas reji | last post by:
--0-599929911-1059996886=:4358 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline ________________________________________________________________________
3
3817
by: Cat | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm having trouble getting my head around the reason the that following file won't validate with the following error output in Xerces version 2.6.0 and XSLTC version 2.5.2 ( from the standard jdk1.5.0 beta ). Is there something I'm missing from the spec? ============ OUTPUT (wrapped) =================
1
1735
by: Dietmar Gräbner | last post by:
Hi Right now I'm dealing with derivation by restriction and I have some questions concerning the attribute property use in context of the derivation. Consider following Schema: <xs:schema elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
2
2076
by: Patrick J. Maloney | last post by:
I received a file from a business partner. I ran it through XercesJ and it choked on the following element: <wcb-case-number>0</wcb-case-number> To debug this issue, I loaded it into XMLSPY and hit validate...it passed. Here are the related type/element definitions: // Redefine built-in string type to limit range of characters, mostly // to eliminate undesirable white space (tab, cr, lf, etc.)
3
2942
by: hre1 | last post by:
stan, once more, thank you for your assistance and patience. can you explain in more detail why my code violates the particle restriction ok (Elt:Elt -- NameAndTypeOK)? i tried to understand the w3c recommendation but the complexity is dazzling. best regards jeff
3
1612
by: Wells Caughey | last post by:
I am trying to create an XML Schema type definition by deriving from another type definition using restriction. For example suppose my base type was this: <xs:schema <<schema element setup>> > <xs:complexType name="Fruits"> <xs:sequence> <xs:any minOccurs="8" maxOccurs="8"/> </xs:sequence> </xs:complexType>
6
1582
by: ritesh | last post by:
Hi, I have been reading some text on C and C++ (i.e advanced books). One of the books mentioned that C++ requires a runtime support whereas C does not - what the author was trying to say was that once you compile a C program the executable created is all that is needed whereas if you compile a C++ program the executable created requires a C++ runtime installed on your system to run the program. Can someone please provide more...
0
9454
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,...
0
10259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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,...
1
7456
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
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.