Re: Inheritence problem
From: Ali R. (nospam_at_company.com)
Date: 11/11/03
- Next message: Thomas Matthews: "Re: Inheritence problem"
- Previous message: Andrey Tarasevich: "Re: Inheritence problem"
- In reply to: Shaf: "Inheritence problem"
- Next in thread: Thomas Matthews: "Re: Inheritence problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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;
}
- Next message: Thomas Matthews: "Re: Inheritence problem"
- Previous message: Andrey Tarasevich: "Re: Inheritence problem"
- In reply to: Shaf: "Inheritence problem"
- Next in thread: Thomas Matthews: "Re: Inheritence problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|