const data in descendant classes



 DEVELOP > c-Plus-Plus > const data in descendant classes

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Paul Smitton"
Date: 20 Aug 2007 10:43:36 AM
Object: const data in descendant classes
Hello,
I would like to be able to store some constant data that is specific to each
descendant class.
This data would then be accessable by base class functions. However, I
cannot find out how to do this.
I thought the following would be a practical way of achieving this, but it
will not work.
Any help would be appreciated.
class Vehicle {
private:
static const int int Wheels = 0;
public:
int GetWheels() {
return Wheels;
}
};
class Car: public Vehicle {
private:
static const int Wheels = 4;
};
class Motorbike: public Vehicle {
private:
static const int Wheels = 2;
};
void main() {
Vehicle *myMini = new Car;
n = myMini->Wheels; // n = 0, but i was hoping it
would be 4
}
.

User: "Jim Langston"

Title: Re: const data in descendant classes 20 Aug 2007 11:31:11 AM
"Paul Smitton" <paul.smitton@mutech.co.uk> wrote in message
news:6ZudnfKCpOI2K1TbRVnyiQA@bt.com...

Hello,

I would like to be able to store some constant data that is specific to
each descendant class.
This data would then be accessable by base class functions. However, I
cannot find out how to do this.
I thought the following would be a practical way of achieving this, but it
will not work.

Any help would be appreciated.


class Vehicle {
private:
static const int int Wheels = 0;
public:
int GetWheels() {
return Wheels;
}
};


class Car: public Vehicle {
private:
static const int Wheels = 4;
};


class Motorbike: public Vehicle {
private:
static const int Wheels = 2;
};


void main() {
Vehicle *myMini = new Car;
n = myMini->Wheels; // n = 0, but i was hoping it
would be 4
}

If each Vehicle has wheels then just store that value in your base class.
Output of following program is
4
2
Comments after code
#include <iostream>
class Vehicle {
private:
const int Wheels;
public:
Vehicle( int wheels ): Wheels( wheels ) {}
int GetWheels() const { return Wheels; }
};
class Car: public Vehicle {
public:
Car(): Vehicle( 4 ) {}
};
class Motorbike: public Vehicle {
public:
Motorbike(): Vehicle( 2 ) {}
};
int main() {
Vehicle* myMini = new Car;
Vehicle* myHarley = new Motorbike;
std::cout << myMini->GetWheels() << "\n" << myHarley->GetWheels() <<
"\n";
}
1. main returns an int, not void
2. A default assignment operator can not be constructored for the base or
derived
.
User: "BobR"

Title: Re: const data in descendant classes 20 Aug 2007 12:52:26 PM
Jim Langston <tazmaster@rocketmail.com> wrote in message...


#include <iostream>

class Vehicle {
private:
const int Wheels;
public:
Vehicle( int wheels ): Wheels( wheels ) {}
int GetWheels() const { return Wheels; }
};

class Car: public Vehicle {
public:
Car(): Vehicle( 4 ) {}
};

class Motorbike: public Vehicle {
public:
Motorbike(): Vehicle( 2 ) {}
};

int main() {
Vehicle* myMini = new Car;
Vehicle* myHarley = new Motorbike;

std::cout << myMini->GetWheels() << "\n" << myHarley->GetWheels() <<
"\n";
}

1. main returns an int, not void
2. A default assignment operator can not be constructored for the base or
derived

3. Put a virtual destructor in the 'base' class (you may want a sissy-bar
and saddle bags on that Hog. <G>). Don't 'slice' (unless it's intentional).
--
Bob R
POVrookie
.


User: "=?iso-8859-1?B?QW5kcuk=?="

Title: Re: const data in descendant classes 20 Aug 2007 01:23:34 PM
On Aug 20, 12:43 pm, "Paul Smitton" <paul.smit...@mutech.co.uk> wrote:

Hello,

I would like to be able to store some constant data that is specific to e=

ach

descendant class.
This data would then be accessable by base class functions. However, I
cannot find out how to do this.
I thought the following would be a practical way of achieving this, but it
will not work.

Any help would be appreciated.

class Vehicle {
private:
static const int int Wheels =3D 0;
public:
int GetWheels() {
return Wheels;
}

};

class Car: public Vehicle {
private:
static const int Wheels =3D 4;

};

class Motorbike: public Vehicle {
private:
static const int Wheels =3D 2;

};

void main() {
Vehicle *myMini =3D new Car;
n =3D myMini->Wheels; // n =3D 0, but i was hoping=

it

would be 4

}

Probably is a scope problem, is the same as:
int main()
{
int x =3D 20;
std::cout<< x << std::endl; // prints 20
if (1)
{
int x =3D 40;
std::cout << x << std::endl; // prints 40
}
std::cout << x << std::endl; // prints 20
}
Try to create a object of class Car and you probably will get your n
=3D=3D 4
void main()
{
Car *car =3D new Car();
int n =3D Car::Whells;
}
And one more thing
The static data, belongs to the Class not the object of Class, so for
every object of the class you will get the same value;
Send me a e-mail if you still having problems.
Andr=E9 Moraes
MG - Brasil
.

User: "BobR"

Title: Re: const data in descendant classes 20 Aug 2007 12:22:51 PM
Paul Smitton <paul.smitton@mutech.co.uk> wrote in message...

Hello,
I would like to be able to store some constant data that is specific to

each

descendant class.
This data would then be accessable by base class functions. However, I
cannot find out how to do this.
I thought the following would be a practical way of achieving this, but it
will not work.

Any help would be appreciated.

class Vehicle { // private:
static const int int Wheels = 0;

'int int'?

public:
int GetWheels() {
return Wheels;
}
};

class Car: public Vehicle { // private:
static const int Wheels = 4;
};

class Motorbike: public Vehicle { // private:
static const int Wheels = 2;
};

void main() {
Vehicle *myMini = new Car;
n = myMini->Wheels; // n = 0, but i was hoping it
would be 4
}

class Vehicle{
static const int Wheels = 0;
public:
virtual ~Vehicle(){} // base class
virtual int GetWheels(){
return Wheels;
}
};
class Car: public Vehicle {
static const int Wheels = 4;
public:
virtual int GetWheels(){
return Wheels;
}
};
class Motorbike: public Vehicle {
static const int Wheels = 2;
public:
virtual int GetWheels(){
return Wheels;
}
};
int main(){ // NOT void main()
Vehicle *myMini = new Car;
// int n = myMini->Wheels; // n = 0, but i was hoping it would be 4
int n = myMini->GetWheels();
cout <<"\nmyMini->Wheels="<<n<<std::endl;
delete myMini;
return 0;
} // main()
'Wheels' is private in class Vehicle, 'myMini' can't directly access it from
main().
Is there some reasom you want 'Wheels' 'static'?
If that is not just an example, you may want a separate class 'Wheel' (with
diameter, psu, color, lugnuts, etc.), and composite those into 'Vehicle'.
Re-think your design, IMHO.
--
Bob R
POVrookie
.
User: "Paul Smitton"

Title: Re: const data in descendant classes 21 Aug 2007 02:04:49 AM


Is there some reasom you want 'Wheels' 'static'?

If that is not just an example, you may want a separate class 'Wheel'
(with
diameter, psu, color, lugnuts, etc.), and composite those into 'Vehicle'.
Re-think your design, IMHO.

It does need to be static. I'm actually having a class of menu, with
different
types of menu as descendant classes, each of which has a constant number of
items.
I ought to have used 'Shape' and 'Sides' as they would have been a better
example.
Thankyou everyone for your help, it's ended a lot of fruitless searching.
Paul :)
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER