473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to use a derived class with constructor

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);

Jul 10 '08 #1
2 1334
On Thu, 10 Jul 2008 08:35:30 -0700, Angus wrote:
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.

--
Lionel B
Jul 10 '08 #2
Angus <an*********@gmail.comwrote in news:a0375667-103b-4cfe-b802-
3e**********@26g2000hsk.googlegroups.com:
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
Jul 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
10
by: William Stacey | last post by:
I know the following is not allowed, but shouldn't it be? sharedObject is part of Derived and should be able to be set in the constructor - no? tia public abstract class Base1 { protected...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.