Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   vb6 style "implements interface" (http://www.velocityreviews.com/forums/t594554-vb6-style-implements-interface.html)

mick.mcmahon@gmail.com 02-27-2008 12:46 PM

vb6 style "implements interface"
 
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.

Alf P. Steinbach 02-27-2008 12:57 PM

Re: vb6 style "implements interface"
 
* mick.mcmahon@gmail.com:
> 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?

--
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?

Triple-DES 02-27-2008 01:24 PM

Re: vb6 style "implements interface"
 
On 27 Feb, 13:46, mick.mcma...@gmail.com wrote:
> 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

mick.mcmahon@gmail.com 02-27-2008 01:33 PM

Re: vb6 style "implements interface"
 
On 27 Feb, 12:57, "Alf P. Steinbach" <al...@start.no> wrote:
> * mick.mcma...@gmail.com:
>
>
>
>
>
> > 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?
>
> --
> 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

Michael.Boehnisch@gmail.com 02-27-2008 03:59 PM

Re: vb6 style "implements interface"
 
On 27 Feb., 13:46, mick.mcma...@gmail.com wrote:
> 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.

mick.mcmahon@gmail.com 02-28-2008 03:47 PM

Re: vb6 style "implements interface"
 
On 27 Feb, 15:59, Michael.Boehni...@gmail.com wrote:
> On 27 Feb., 13:46, mick.mcma...@gmail.com wrote:
>
>
>
>
>
> > 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.- 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


All times are GMT. The time now is 02:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.