Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vb6 style "implements interface"

Reply
Thread Tools

vb6 style "implements interface"

 
 
mick.mcmahon@gmail.com
Guest
Posts: n/a
 
      02-27-2008
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-27-2008
* http://www.velocityreviews.com/forums/(E-Mail 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?

--
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?
 
Reply With Quote
 
 
 
 
Triple-DES
Guest
Posts: n/a
 
      02-27-2008
On 27 Feb, 13:46, (E-Mail Removed) 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
 
Reply With Quote
 
mick.mcmahon@gmail.com
Guest
Posts: n/a
 
      02-27-2008
On 27 Feb, 12:57, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail 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?
>
> --
> 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
 
Reply With Quote
 
Michael.Boehnisch@gmail.com
Guest
Posts: n/a
 
      02-27-2008
On 27 Feb., 13:46, (E-Mail Removed) 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.
 
Reply With Quote
 
mick.mcmahon@gmail.com
Guest
Posts: n/a
 
      02-28-2008
On 27 Feb, 15:59, (E-Mail Removed) wrote:
> On 27 Feb., 13:46, (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Javascript Style Switcher that remebers current site style in use Hardeep Rakhra HTML 8 01-15-2004 08:00 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments