473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
}
Aug 20 '07 #1
5 1852
"Paul Smitton" <pa**********@mutech.co.ukwrote in message
news:6Z*********************@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
Aug 20 '07 #2

Paul Smitton <pa**********@mutech.co.ukwrote 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
Aug 20 '07 #3

Jim Langston <ta*******@rocketmail.comwrote 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
Aug 20 '07 #4
On Aug 20, 12:43 pm, "Paul Smitton" <paul.smit...@mutech.co.ukwrote:
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 hopingit
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

Aug 20 '07 #5
>
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 :)
Aug 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
25
2899
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
2
1998
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
30
3353
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
19
2194
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
4
3275
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
20
2110
by: Derek Hart | last post by:
For testing purposes, I need some code that will loop through all attributes and elements in an XML file, and replace the data with the element or attribute name? Does anybody have code that will...
3
1599
by: Torsten Wiebesiek | last post by:
Hi folks, currently I'm writing image classes for easier handling of Intel's IPP library. My idea is to have to different classes. One class that represents a complete image and deals with all...
15
7838
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.