Derived class constructor

Q

quickcur

Hi,

I have some legacy code looks like this:

class Tool
{
public:
Tool(char* name);

virtual void do();
};

class Work
{
private:
Tool _tool;

public:
Work();

void run();
}

The Work.cc is defined as:

Work::Work()
:
_tool("Old Tool")
{
....
}

void Work::run(){
....
_tool.do();
....
}

And some other code is using class Work.

Now I have a NewTool class:

class NewTool : public Tool
{
public:
NewTool(char* name);

virtual void do()
{
my new do...
}
}

I would like to use my NewTool in Work. But some old code is not
compatible with NewTool. So I have to keep Work class as it is and I
deceided to define a NewWork class derived from Work. All I want is to
give "_tool" a NewTool object. I want to keep code change as minimum
as possible and prefer not to change Tool or Work. But if I have to
change them, it is OK too.


class NewWork : public Work
{
public:
NewWork();
}

I am wondering how I can write the constructor of NewWork so that is
has all function from Work but uses NewTool. The following code does
not work:

NewWork::NewWork
:
_tool(NewTool("New Tool"))
{
}


Can anyone help?

Thanks,

qq
 
J

Jonathan Mcdougall

Hi,

I have some legacy code looks like this:

class Tool
{
public:
Tool(char* name);

virtual void do();
};

class Work
{
private:
Tool _tool;

public:
Work();

void run();
}

The Work.cc is defined as:

Work::Work()
:
_tool("Old Tool")
{
....
}

void Work::run(){
....
_tool.do();
....
}

And some other code is using class Work.

Now I have a NewTool class:

class NewTool : public Tool
{
public:
NewTool(char* name);

virtual void do()
{
my new do...
}
}

I would like to use my NewTool in Work. But some old code is not
compatible with NewTool. So I have to keep Work class as it is and I
deceided to define a NewWork class derived from Work. All I want is to
give "_tool" a NewTool object. I want to keep code change as minimum
as possible and prefer not to change Tool or Work. But if I have to
change them, it is OK too.


class NewWork : public Work
{
public:
NewWork();
}

I am wondering how I can write the constructor of NewWork so that is
has all function from Work but uses NewTool. The following code does
not work:

NewWork::NewWork
:
_tool(NewTool("New Tool"))
{
}

Work has a Tool and nothing else. You can either 1) change Tool to
NewTool in Work or 2) still in Work, make Tool a Tool* and create a
NewTool on the heap. Whatever you do, you'll end up modifying class
Work.

It is suprising (and imo broken) that Tool has virtual functions but is
created as an automatic object.


Jonathan
 
A

Ahmed Samieh

you can use Class Templates to solve this problem

so you need to modefy Work class as :

template <class Type>
class Work {
private:
Type _tool;
public:
Work();
void run();
};

then use it like
Work<Tool> work1; // _tool is Tool object
Work<NewTool> work2; // _tool is NewTool object

so you don't need to write NweWork class again

Ahmed
 
I

Ian Collins

Ahmed said:
you can use Class Templates to solve this problem
What problem? This makes no sense on its own, please quote context form
the message you are replying to.
 
A

Ahmed Samieh

you can use Class Templates to solve this problem

so you need to modefy Work class as :

template <class Type>
class Work {
private:
Type _tool;
public:
Work();
void run();
};

then use it like
Work<Tool> work1; // _tool is Tool object
Work<NewTool> work2; // _tool is NewTool object

so you don't need to write NweWork class again

Ahmed
 
A

Ahmed Samieh

Could you please give me some code?

Thanks,

qq


#include <iostream>
#include <stdlib.h>
using namespace std;

class OldTool {
protected:
string buffer;
public:
OldTool();
OldTool(char* name) : buffer(name){};
~OldTool(){};
virtual void proc();
};
void OldTool::proc() {
cout << buffer << "Old" << endl;
}
class NewTool : public OldTool {
public:
NewTool() : OldTool() {};
NewTool(char* name) : OldTool(name) {};
~NewTool() {};
virtual void proc();
};
void NewTool::proc() {
cout << buffer << "New" << endl;
}
template <class Type>
class Work {
private:
Type _tool;
public:
Work() : _tool("this tool is ") {};
~Work() {};
void run() {
_tool.proc();
}
};

int main(int argc, char *argv[]) {
Work<OldTool> x; // = Work
x.run();
Work<NewTool> y; // = NewWork
y.run();
system("PAUSE");
return 0;
}

now you don't need to write NewWork, the compiler will do it for you

Ahmed
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top