vb6 style "implements interface"

M

mick.mcmahon

Hi, i need some help, i'm new-ish to c++ but was an experienced VB6
programmer.

I am trying to do something in c++ along the lines of something i did
a couple of years ago in vb.

Basically, i have loads classes, and want to store them in some sort
of array/container and enumerate through them and call a couple of
methods. I don't need to know which one i'm calling.

In VB i use an shell class and with all the methods that i wanted to
call. then created my other classes implementing the shell interface.
I stored them all in a collection and went through them by creating a
class varible of the interface type and then storing the class from
the collection into it and calling the methods.

I've spent a couple of days looking now, and it's either impossible or
i just don't know the teminolgy for how to do it.

Any help much appreciated.

Mick.
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi, i need some help, i'm new-ish to c++ but was an experienced VB6
programmer.

I am trying to do something in c++ along the lines of something i did
a couple of years ago in vb.

Basically, i have loads classes, and want to store them in some sort
of array/container and enumerate through them and call a couple of
methods. I don't need to know which one i'm calling.

In VB i use an shell class and with all the methods that i wanted to
call. then created my other classes implementing the shell interface.
I stored them all in a collection and went through them by creating a
class varible of the interface type and then storing the class from
the collection into it and calling the methods.

I've spent a couple of days looking now, and it's either impossible or
i just don't know the teminolgy for how to do it.

Any help much appreciated.

What's the real problem you're trying to solve by doing this?
 
T

Triple-DES

Hi, i need some help, i'm new-ish to c++ but was an experienced VB6
programmer.

I am trying to do something in c++ along the lines of something i did
a couple of years ago in vb.

Basically, i have loads classes, and want to store them in some sort
of array/container and enumerate through them and call a couple of
methods. I don't need to know which one i'm calling.

In VB i use an shell class and with all the methods that i wanted to
call. then created my other classes implementing the shell interface.
I stored them all in a collection and went through them by creating a
class varible of the interface type and then storing the class from
the collection into it and calling the methods.

I've spent a couple of days looking now, and it's either impossible or
i just don't know the teminolgy for how to do it.

Any help much appreciated.

Mick.

Sounds to me like polymorphism achieved through public inheritance.

class I // the "interface class"
{
public:
virtual void f() const = 0;
virtual void g() const = 0;
};

class S // a subclass
{
public:
void f() { }
void g() { }
};

It shouldn't be too hard to find more info on the subject. Just google
"c++ OOP" or something similar.

DP
 
M

mick.mcmahon

* (e-mail address removed):











What's the real problem you're trying to solve by doing this?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -

I'm writing a front end for a program. I've got two barcode classes to
start it off with but after that some other programmer will be adding
more barcode classes over time. I wanted to do a system where they
wrote the class and had to implement a coulpe of standard methods then
all they would have to do is add their class to the project, and in
the initialstion function create an instance of it and add it to the
collection. The program would do the rest. Add the name to the list,
and call the input and display routines as when selected.

I know how to do it in vb (which make it so frustrating), but i'm
trying to force myself to learn c properly.

-Mick
 
M

Michael.Boehnisch

Hi, i need some help, i'm new-ish to c++ but was an experienced VB6
programmer.

I am trying to do something in c++ along the lines of something i did
a couple of years ago in vb.

Basically, i have loads classes, and want to store them in some sort
of array/container and enumerate through them and call a couple of
methods. I don't need to know which one i'm calling.

In VB i use an shell class and with all the methods that i wanted to
call. then created my other classes implementing the shell interface.
I stored them all in a collection and went through them by creating a
class varible of the interface type and then storing the class from
the collection into it and calling the methods.

I've spent a couple of days looking now, and it's either impossible or
i just don't know the teminolgy for how to do it.

Any help much appreciated.

Mick.

While C++ does not have an explicit "interface" keyword like Java or
C#, an abstract base class is the appropriate equivalent:

class Shell {
public:
virtual int method1( int, char ) = 0;
virtual void method2() = 0;
...
};

class Derived1 : public Shell {
public:
int method1( int x, char c ) { ... }
void method2() { ... }
};

class Derived2 : public Shell {
public:
int method1( int x, char c ) { ... }
void method2() { ... }
};

Note the "= 0" declaration. This enforces an implementation for any
derived class for that you want to create an actual object.
Alternatively you can define real implementations in class Shell that
get used as defaults if a derived class does not offer an
implementation on its own.
In your application, you can create instances of Derived1 and Derived2
and store pointers to them in a common container for objects of class
Shell:

std::list<Shell*> mycontainer;

mycontainer.push_back( new Derived1 );
mycontainer.push_back( new Derived2 );

Note, you cannot use a std::list<Shell>, you need to dereference
pointers here.
You can iterate through the list and calling methods will resolve
dependend on the derived type Derived1 resp. Derived2:

for ( std::list<Shell*>::iterator p = mycontainer.begin(); p !=
mycontainer.end(); ++p ) {
std::cout << (*p)->method1( 1, 'x' ) << std::endl;
(*p)->method2();
}

or, if you prefer STL functor style:

class f {
public:
void operator() ( Shell* p ) {
std::cout << p->method1( 1, 'x' ) << std::endl;
p->method2();
}
};
....
std::for_each( mycontainer.begin(), mycontainer.end(), f() );

best,

Michael.
 
M

mick.mcmahon

While C++ does not have an explicit "interface" keyword like Java or
C#, an abstract base class is the appropriate equivalent:

class Shell {
public:
    virtual int method1( int, char ) = 0;
    virtual void method2() = 0;
    ...

};

class Derived1 : public Shell {
public:
    int method1( int x, char c ) { ... }
    void method2() { ... }

};

class Derived2 : public Shell {
public:
    int method1( int x, char c ) { ... }
    void method2() { ... }

};

Note the "= 0" declaration. This enforces an implementation for any
derived class for that you want to create an actual object.
Alternatively you can define real implementations in class Shell that
get used as defaults if a derived class does not offer an
implementation on its own.
In your application, you can create instances of Derived1 and Derived2
and store pointers to them in a common container for objects of class
Shell:

std::list<Shell*> mycontainer;

mycontainer.push_back( new Derived1 );
mycontainer.push_back( new Derived2 );

Note, you cannot use a std::list<Shell>, you need to dereference
pointers here.
You can iterate through the list and calling methods will resolve
dependend on the derived type Derived1 resp. Derived2:

for ( std::list<Shell*>::iterator p = mycontainer.begin(); p !=
mycontainer.end(); ++p ) {
   std::cout << (*p)->method1( 1, 'x' ) << std::endl;
   (*p)->method2();

}

or, if you prefer STL functor style:

class f {
public:
    void operator() ( Shell* p ) {
       std::cout << p->method1( 1, 'x' ) << std::endl;
       p->method2();
    }};

...
std::for_each( mycontainer.begin(), mycontainer.end(), f() );

best,

   Michael.- Hide quoted text -

- Show quoted text -

That is exactly what i was looking for.
Thank you very much for taking the time to answer.

-Mick
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top