const data in descendant classes


P

Paul Smitton

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
}
 
Ad

Advertisements

J

Jim Langston

Paul Smitton said:
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
 
B

BobR

Paul Smitton said:
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.
 
B

BobR

Jim Langston said:
#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).
 
?

=?iso-8859-1?B?QW5kcuk=?=

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

}

Probably is a scope problem, is the same as:
int main()
{
int x = 20;
std::cout<< x << std::endl; // prints 20
if (1)
{
int x = 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
== 4

void main()
{
Car *car = new Car();
int n = 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é Moraes
MG - Brasil
 
Ad

Advertisements

P

Paul Smitton

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 :)
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top