🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c++.moderated
3 messages
3 total messages Started by Eric Simon Tue, 27 Feb 1996 10:34
Simple inheritance Q
#56
Author: Eric Simon
Date: Tue, 27 Feb 1996 10:34
37 lines
1542 bytes
I have a base class, called Animal, and a derived class, called Cat.

In my main program I have:

1.    Animal *p;
2.    Cat a(args);
3.    p = &a;

Now, if I define
    class Cat:Animal{...

then I get the error in line 3:
  Can not convert Cat * to Animal *

But if I define
   class Cat:public Animal{...

everything is fine.

Can anyone tell me why?

Thanks,

Eric

+-----------------------------------------------------------------+
| Eric Simon     | erics@access.digex.net                         |
| ULTIMATE!      | http://www.access.digex.net/~erics/home.html   |
+-----------------------------------------------------------------+
| proud daddy to Joshua (4/18/93) and Eliana (3/12/95)            |
+-----------------------------------------------------------------+


      [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
      [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
      [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
      [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
Re: Simple inheritance Q
#57
Author: Wes Cowley
Date: Wed, 28 Feb 1996 13:11
31 lines
1417 bytes
Eric Simon (erics@access2.digex.net) wrote:
: 1.    Animal *p;
: 2.    Cat a(args);
: 3.    p = &a;
: 
: Now, if I define
:     class Cat:Animal{...
: 
: then I get the error in line 3:
:   Can not convert Cat * to Animal *
: 
: But if I define
:    class Cat:public Animal{...
: 
: everything is fine.
: 
: Can anyone tell me why?

Inheritance defaults to private, which essentially means that if you use:

   class Cat : Animal { ...

code outside your Cat class has no way of knowing that Cat inherits from 
Animal and cannot use the implicit conversion from Cat* to Animal*. 

Wes

      [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
      [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
      [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
      [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
Re: Simple inheritance Q
#58
Author: Stephen C. Marney
Date: Fri, 01 Mar 1996 13:31
37 lines
1556 bytes
Eric Simon wrote:
> 
> I have a base class, called Animal, and a derived class, called Cat.
> 
> In my main program I have:
> 
> 1.    Animal *p;
> 2.    Cat a(args);
> 3.    p = &a;
> 
> Now, if I define
>     class Cat:Animal{...
> 
> then I get the error in line 3:
>   Can not convert Cat * to Animal *
> 
> But if I define
>    class Cat:public Animal{...
> 
> everything is fine.
> 
> Can anyone tell me why?
> 	[clip]

The default access for classes is private.
p = &a; 	// asks for an implicit conversion.

"Pointers and references to derived classes can be implicitly converted to
pointers and references 
to their base classes if there is an accessible, unambiguous base class."

Key word in this case is accessible (i.e., at least, not private.)

      [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
      [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
      [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
      [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads