473,322 Members | 1,232 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,322 software developers and data experts.

Can we use abstract class templating classes?

Say I have the following class:

class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }
}

class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;
}

class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
-----------------------------------------
Can I do something like this? Thanks

template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo(); }
}

Mar 16 '07 #1
4 1625
"newbie" <mi******@yahoo.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
Say I have the following class:

class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }
}

class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;
}

class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
-----------------------------------------
Can I do something like this? Thanks

template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo(); }
}
Your code shows classes MyAbs, MyDer1 and MyDer2, yet your template shows
Toy. I'm presuming in my response that by
Toy toy_;
Play() ( toy_.foo(); )

you actually meant
MyAbs toy_;
Play() { toy_.foo(); }

No. Your template is attempting to instantize a class MyAbs which is a
virtual class. You couldn't do it in main so you couldn't do it in a
template. However, I believe you could do:

MyAbs* toy_ = new MyDer1;
Play() { toy_->foo(); }
Mar 16 '07 #2
On Mar 16, 4:37 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"newbie" <mitbb...@yahoo.comwrote in message

news:11**********************@n59g2000hsh.googlegr oups.com...
Say I have the following class:
class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }
}
class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;
}
class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
-----------------------------------------
Can I do something like this? Thanks
template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo(); }
}

Your code shows classes MyAbs, MyDer1 and MyDer2, yet your template shows
Toy. I'm presuming in my response that by
Toy toy_;
Play() ( toy_.foo(); )

you actually meant
MyAbs toy_;
Play() { toy_.foo(); }

No. Your template is attempting to instantize a class MyAbs which is a
virtual class. You couldn't do it in main so you couldn't do it in a
template. However, I believe you could do:

MyAbs* toy_ = new MyDer1;
Play() { toy_->foo(); }
Thanks. I am not sure

What I really want is to ask class MyTemplateClass behave clever
enough to
understand things like

///////////////////////////////////
class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }

}

class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;

}

class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
///////////////////////////////////////

template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo();
}

typedef MyTemplateClass<MyDer1MyClass1;
typedef MyTemplateClass<MyDer2MyClass2;
////////////////////////////////////////
int main() {
MyClass1 m1;
MyClass2 m2;
m1.Play();
m2.Play();
return 0;
}

Mar 16 '07 #3
newbie wrote:
Say I have the following class:

class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }
}

class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;
}

class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
-----------------------------------------
Can I do something like this? Thanks

template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo(); }
}
Yeah, but you don't need to. You are using generics (although a limited
form if I understand the term correctly) in conjunction with templates
when it isn't really necessary *in the scope of this case*. You could
do the same thing without subclassing MyAbs. Of course whatever
MyTemplateClass takes as a parameter, it must have a function foo that
takes no parameters.

Generics is a runtime solution, "I want to plug in any class at runtime
that has this interface so that I may interact with it using those
interfaces", as opposed to templates which is a compile time solution "I
want my programme to emit code that will interact with this set of
classes/or constants in the way I define."

Although, by specifying the interface class, it does make it easy to see
what functions are needed. But again it is not necessary *in the scope
of this case*.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #4
newbie wrote:
On Mar 16, 4:37 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"newbie" <mitbb...@yahoo.comwrote in message

news:11**********************@n59g2000hsh.googleg roups.com...
>>Say I have the following class:
class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }
}
class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;
}
class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
-----------------------------------------
Can I do something like this? Thanks
template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo(); }
}
Your code shows classes MyAbs, MyDer1 and MyDer2, yet your template shows
Toy. I'm presuming in my response that by
Toy toy_;
Play() ( toy_.foo(); )

you actually meant
MyAbs toy_;
Play() { toy_.foo(); }

No. Your template is attempting to instantize a class MyAbs which is a
virtual class. You couldn't do it in main so you couldn't do it in a
template. However, I believe you could do:

MyAbs* toy_ = new MyDer1;
Play() { toy_->foo(); }

Thanks. I am not sure

What I really want is to ask class MyTemplateClass behave clever
enough to
understand things like

///////////////////////////////////
class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }

}

class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;

}

class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
///////////////////////////////////////

template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo();
}

typedef MyTemplateClass<MyDer1MyClass1;
typedef MyTemplateClass<MyDer2MyClass2;
////////////////////////////////////////
int main() {
MyClass1 m1;
MyClass2 m2;
m1.Play();
m2.Play();
return 0;
}
Again, yes, but unless you are going to pass out the object to something
that is going to take a MyAbs object which you may pass MyDer1 or
MyDer2, you do not need to subclass MyAbs to make it work.
Adrian

--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #5

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

Similar topics

12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.