Member Avatar for nalasimbha

Hi,

I have a question related to pointers to base and derived classes. I have the following base and derived class:

class A
{
protected:
double a,x;
public:
void set_ax();
virtual void mult_ax();
};

class B:virtual public A
{
protected:
double b;
public:
void set_b();
void mult_ax();
};

class C:virtual public A
{
protected:
public:
};

class D
{
protected:
double d;
public:
void set_d();
};

class E:public B, public C, public D
{
protected:
public:
};

I am trying to access the base class objects and dervived class objects using pointers. By declaring a base class pointer to class A i am able to point to objects created from B and C. However I am not sure how to access the objects created from E which has two different base class. How would I go about doing this. Any help is appreciated.

Thank you

Recommended Answers

All 2 Replies

Delcare two pointers; class D and class A to have an address of object of class E,

int main(){
   A *a;
   D *d;

   E e;
   a=&e;
   d=&e;

   a->mult_ax();
   d->set_d();
   return 0;
}

Take a look at tutorial - Polymorphism

Member Avatar for nalasimbha

Thank you for the suggestion. However this way the person should know which function belongs to which base class :) . Another question I would like to ask is:
If the derived class E has its own function say set_e(), which point would i need to downcast? Can I downcast either *a or *d?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.