Thread: question time (classes and pointers)

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    14

    question time (classes and pointers)

    ok here is my following program:

    #include <iostream>
    using namespace std;

    class Aircraft
    {
    private:
    int range;
    public:
    Aircraft(int r) {range = r;}
    ~Aircraft() {cout << range << endl;}
    };

    class Airplane
    {
    private:
    int length;
    public:
    Airplane(int l) {length = l;}
    ~Airplane() {cout << length << endl;}
    };

    class Helicopter
    {
    private:
    int clearance;
    public:
    Helicopter(int c) {clearance = c;}
    ~Helicopter() {cout << clearance << endl;}
    };

    int main()
    {
    Aircraft carrier(5);
    Airplane bowan(9);
    Helicopter apache(15);

    }

    now the next thing that needs to be done is create a 5 element array of pointers to Aircraft. Then using the new operator, create dynamically 1 helicpoter, 3 Airplanes, and 1 Aircraft object. Than assign these objects to the elemtns of the array. Than in a loop delete each object...im assumign delete[] can work with that?

    i understand id have to do
    Aircraft *p[5];

    but how would i go about intialising the new statements?
    i tried p[5] = new helicopter;
    but that just winds up giving me alot of errors

    how can i go about doing this?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You need to inherit Airplane and helicopter from Aircraft -

    class Airplane : public Aircraft
    {
    ...
    }

    class Helicopter : public Aircraft
    {
    ...
    }

    and then implement a vitual method or interface in Aircraft which is overrided in Airplane and Helicopter.

    Then

    p[5] = new helicopter;

    would work if your array had six elements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q: Pointers to Classes '->' notation
    By Howie17 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2008, 10:09 AM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 02:03 PM
  4. Question on pointers in classes
    By swarm in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2002, 06:16 PM
  5. C++ question (Classes and pointers)
    By incognito in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2001, 05:49 PM