How to use a derived class with constructor

A

Angus

Hello

I want to use polymorphic feature.

Eg if I had a base Shape class and a derived Circle class I could do
things like this:

Shape myShape;
MyCircle* pCircle = &myShape;
pCircle->SetValues(3,5);
cout << pCircle->area();

But my derived class has a constructor which gets passed variables.

Eg
Derived(int var1, std::string var2)

So how do I instantiate it?

I tried Derived* pDer(params);
 
L

Lionel B

Hello

I want to use polymorphic feature.

Eg if I had a base Shape class and a derived Circle class I could do
things like this:

Shape myShape;
MyCircle* pCircle = &myShape;
pCircle->SetValues(3,5);
cout << pCircle->area();

But my derived class has a constructor which gets passed variables.

Eg
Derived(int var1, std::string var2)

So how do I instantiate it?

I tried Derived* pDer(params);

Perhaps you are thinking along the lines of:

class Shape
{
public:
Shape();
virtual ~Shape();
virtual draw() =0; // pure virtual

...
};

class Circle : public Shape
{
public:
Circle(/* arguments */);
virtual ~Circle();
virtual draw(); // implements circle drawing functionality

...
};

class Rectangle : public Shape
{
public:
Rectangle(/* arguments */);
virtual ~Rectangle();
virtual draw(); // implements rectangle drawing functionality

...
};

then:

Circle circle(3,"foo");
Shape* pShape1 = &circle;

Rectangle rectangle(6,7,"bar");
Shape* pShape2 = &rectangle;

...

pShape1->draw(); // draws circle
pShape2->draw(); // draws rectangle

You get the idea.
 
J

Joe Greer

Hello

I want to use polymorphic feature.

Eg if I had a base Shape class and a derived Circle class I could do
things like this:

Shape myShape;
MyCircle* pCircle = &myShape;
pCircle->SetValues(3,5);
cout << pCircle->area();

But my derived class has a constructor which gets passed variables.

Eg
Derived(int var1, std::string var2)

So how do I instantiate it?

I tried Derived* pDer(params);

This is actually backwards. Public inheritance provides an *isa*
relationship. A *Shape* isn't a Circle, but a circle is a shape.
So....


You could have:

class Shape
{
};

class Circle : public Shape
{
public:
void SetRadius(int r) {}
};

Circle c;
Shape * s = & c; // ok, Circles provide the functionality of a Shape

Shape s1;
Circle * c1 = & s1; // Not ok, Shape doesn't provide the
// functionality of a Circle.

The way constructors work for derivations is:

class base
{
int i;
public:
base (int x) : i(x) {}
};

class derived : public base
{
int x;
public:
derived(int x1, i1) : base(i1), x(x1) {}
};

As you can see from the example, the initialization list offers the
opportunity to specify a constructor for the base class so that the base
can properly initialize itself. Of course, this implys that the derived
class has to be able to come up with initialization values for the base
either by haveing equivalent parameters itself, or by being able to get
the required values in some other fashion.

joe
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top