Re: Inheritence problem

From: Ali R. (nospam_at_company.com)
Date: 11/11/03


Date: Tue, 11 Nov 2003 22:08:41 GMT


"Shaf" <ks2322000@yahoo.com> wrote in message
news:5Gcsb.12479$xI2.246210@news20.bellglobal.com...
> Hi,
>
> I have a base class called Animal and in its constructor I initialize a
> bunch of protected values such as this
>
> Animal::Animal() //Base Class
> {
> X =0; //Protected Variable
> Y = 1; //Protected Variable
> ...
> }
>
> int Animal::getY()
> {
> return Y;
> }
>
> Cow::Cow() //Derived class
> {
> }
>
>
> To my understanding when I instantiate a new derived class called Cow,
> it should retain those values for x and y, so in my main I would do this
>
>
> void main ()
> {
> Animal * myCow;
> myCow = new Cow();
>
> cout<<myCow->getY()<<endl;
> }
>
> //output
> Y = -4.73000 * e08 ....
>
> My expected value for Y should be 1 but as you can see its not. When I
> try to debug I notice that once it leaves the Animal Constructor, All
> Protected variables lose thier initialization value.
>
>
> Any insight would be appreciated
>

You assumptions are all correct. And depending on the decleration of your
Cow class every thing you typed here should work just fine. Maybe you
should cut an past your actuall code here.

something like this

#include <iostream>

using namespace std;

class Animal
{
public:
   Animal();
   int GetY();
protected:
   int m_X;
   int m_Y;
};

class Cow : public Animal
{
public:
   Cow();
};

Animal::Animal()
: m_X(0) //perfered method of initialization
, m_Y(10)
{
// m_X = 0;
// m_Y = 10;
}

int Animal::GetY()
{
   return m_Y;
}

Cow::Cow()
{
}

int main()
{
   Animal *pCow = new Cow();
   cout << pCow->GetY() << endl;
   return 0;
}



Relevant Pages

  • Re: what does this mean ?
    ... int main ... To zero-initialize an object of type T means: ... - if T is a non-POD class type, the default constructor for T is ... called (and the initialization is ill-formed if T has no accessible default ...
    (microsoft.public.vc.language)
  • Re: Inheritence problem
    ... > try to debug I notice that once it leaves the Animal Constructor, ... > Protected variables lose thier initialization value. ... We need the declaration of both Animal and Cow classes. ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (alt.comp.lang.learn.c-cpp)
  • Re: List container passed as a reference
    ... > which uses a default constructor, ... alternate cstor or copy cstor is irrelevent. ... It is initialization, not assignment. ... >> int main ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Build-in types initialization
    ... > Thanks for your answer, but if it is true, how do I define my custom class ... > way to distinguish between these 2 types of initialization when writing ... Both are handled by default constructor. ... The default constructor for int, float, bool initializes them to 0 /false. ...
    (comp.lang.cpp)
  • Re: List container passed as a reference
    ... >>Point has are a default and a copy constructor, ... > is a cstor with an assignment operator that initializes publicly accessible ... struct Point ... this is initialization, but it does not use constructors ...
    (alt.comp.lang.learn.c-cpp)