CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Inheritance

Threaded View

  1. #1
    Join Date
    Jun 2006
    Posts
    31

    Inheritance

    I have a class "Car" and a subclass "LuxuryCar" derived from "Car". I also have a class "Radio" and a subclass "SatelliteRadio" derived from "Radio".

    I would like the Car to maintain a reference to a Radio. And I would like a LuxuryCar to maintain a reference to a SatelliteRadio.

    When instantiating a LuxuryCar object, it creates a reference to a SatelliteRadio. SatelliteRadio has available all the methods of Radio and a method unique to SatelliteRadio. But since LuxuryCar is a subclass of Car, it by default has a reference to a Radio (inherited from the Car class) that I don't really need. And the constructor of the base class Car instantiates a Radio I don't need (I only want a SatelliteRadio). I do need the methods of Car.

    I could instantiate the Radio and SatelliteRadios outside of the Car and LuxuryCar classes. But I would still have a reference to Radio in LuxuryCar that I don't really want.

    My gut tells me this is not correct and there is a better way to design this. I would like to maintian the inheritence relationship between the cars and the radios. And I want to be able to instantiate both a Car and LuxuryCar if I want.

    Any help or thoughts would be appreciated.

    Thanks,
    Adam







    class Car
    {
    public:
    Car();
    virtual ~Car();
    virtual Start();
    provate:
    Radio* theRadio;
    };

    Car::Car()
    {
    theRadio = new Radio();
    }

    Car::Start()
    {
    cout << "Starting Car" << endl;
    theRadio->TurnOn();
    }

    class LuxuryCar : public Car
    {
    public:
    LuxuryCar();
    virtual ~LuxuryCar();
    Start();
    private:
    SatelliteRadio* theSatelliteRadio;
    };

    LuxuryCar::LuxuryCar()
    {
    theSatelliteRadio = new SatelliteRadio();
    }

    LuxuryCar::Start()
    {
    cout << "Starting Luxury Car" << endl;

    theSatelliteRadio->TurnOn();
    TheSatelliteRadio->FindSatellite();
    }

    class Radio
    {
    public:
    Radio();
    virtual ~Radio();
    TurnOn();
    };

    Radio::radio()
    {
    }

    Radio::TurnOn()
    {
    cout << "Radio Turned On" << endl;
    }

    class SatelliteRadio : public Radio
    {
    public:
    SatelliteRadio();
    virtual ~Satelliteradio();
    FindSatellite();
    };

    SatelliteRadio::SatelliteRadio()
    {
    }

    SatelliteRadio::FindSatellite()
    {
    cout << "Searching for Satellite" << endl;
    }




    void main()
    {
    Car* cars[2];

    cars[0] = new Car();
    cars[1] = new LuxuryCar();

    for (int x=0l x<2; x++)
    {
    cout << "Car: " << x << endl;
    cars[x]->Start();
    }
    }
    Attached Files Attached Files
    Last edited by AdamDH; June 1st, 2006 at 01:50 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured