const data in descendant classes

Discussion in 'C++' started by Paul Smitton, Aug 20, 2007.

  1. Paul Smitton

    Paul Smitton Guest

    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
    }
     
    Paul Smitton, Aug 20, 2007
    #1
    1. Advertising

  2. Paul Smitton

    Jim Langston Guest

    "Paul Smitton" <> wrote in message
    news:...
    > 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
     
    Jim Langston, Aug 20, 2007
    #2
    1. Advertising

  3. Paul Smitton

    BobR Guest

    Paul Smitton <> 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
     
    BobR, Aug 20, 2007
    #3
  4. Paul Smitton

    BobR Guest

    Jim Langston <> 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
     
    BobR, Aug 20, 2007
    #4
  5. On Aug 20, 12:43 pm, "Paul Smitton" <> wrote:
    > 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
     
    =?iso-8859-1?B?QW5kcuk=?=, Aug 20, 2007
    #5
  6. Paul Smitton

    Paul Smitton Guest

    >
    > 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 :)
     
    Paul Smitton, Aug 21, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nik Coughin

    css - descendant selectors

    Nik Coughin, Jan 28, 2004, in forum: HTML
    Replies:
    2
    Views:
    391
    Alliss
    Jan 29, 2004
  2. Ales DOLECEK

    Templates and descendant classes

    Ales DOLECEK, Jul 22, 2003, in forum: C++
    Replies:
    2
    Views:
    440
    Rob Williscroft
    Jul 22, 2003
  3. Reid Priedhorsky
    Replies:
    2
    Views:
    336
    Bengt Richter
    Jul 12, 2005
  4. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  5. njsimha
    Replies:
    0
    Views:
    789
    njsimha
    Sep 16, 2008
Loading...

Share This Page