473,789 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this an OO concept?

I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept? Here is some code to illustrate:

#include <iostream>

class Abstract
{
public:
virtual void func() const = 0;

virtual ~Abstract() { }
};

template <typename T>
class Derived : public Abstract
{
T obj;
public:
Derived() { }

void func() const { obj.func(); }
};

class A
{
public:
void func() const
{ std::cout << "A::func called" << std::endl; }
};

class B
{
public:
void func() const
{ std::cout << "B::func called" << std::endl; }
};

int main()
{
Abstract *obj = new Derived<A>;
obj->func();

obj = new Derived<B>;
obj->func();
}

The output of the program is:

A::func called
B::func called

Thanks.
Jul 23 '05 #1
14 1386
On Sun, 19 Jun 2005 09:39:07 +0400, Jason Heyes
<ja********@opt usnet.com.au> wrote:
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept?
Why do you care?
Here is some code to illustrate:

#include <iostream>

class Abstract
{
public:
virtual void func() const = 0;

virtual ~Abstract() { }
};

template <typename T>
class Derived : public Abstract
{
T obj;
public:
Derived() { }

void func() const { obj.func(); }
};


Derived template is the adapter design pattern. It uses object composition
to add Abstract interface to the contained object. And it's perfectly OO.

--
Maxim Yegorushkin
Jul 23 '05 #2
"Maxim Yegorushkin" <e-*****@yandex.ru > wrote in message
news:op.sslslho gti5cme@home...
On Sun, 19 Jun 2005 09:39:07 +0400, Jason Heyes
<ja********@opt usnet.com.au> wrote:
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept?
Why do you care?


I would like to find the right naming for my classes.
Here is some code to illustrate:

#include <iostream>

class Abstract
{
public:
virtual void func() const = 0;

virtual ~Abstract() { }
};

template <typename T>
class Derived : public Abstract
{
T obj;
public:
Derived() { }

void func() const { obj.func(); }
};


Derived template is the adapter design pattern. It uses object composition
to add Abstract interface to the contained object. And it's perfectly OO.


Ok. I'd better rename Derived to Adapter. Thanks for the information.
Jul 23 '05 #3
Jason Heyes wrote:
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept?


i hope u do realize the fact that only Derived (or now Adapter) is the
_only_ derived class of Abstract. classes A & B r diff classes. ur
template class imposes the restriction on objects that they must have
func() defined. i wouldnt exactly attribute such template restrictions
to OO. instead u might wanna try:

class Abstract {
public:
virtual void func() const = 0;
};

class Caller {
Abstract *x;
public:
Caller() { x = NULL; }
Caller(Abstract *x) { this->x = x; }
~Caller() { if(x) delete x; }
void func() { if(x) x->func(); }
void set_x(Abstract *x)
{ if(this->x) delete this->x;
this->x = x; }
// u might wanna try overloadin da = operator
// but keep in mind that <Caller obj> = new <Abstract derived> won't
work!
};

class A : public Abstract {
public:
void func() const { cout << "A" << endl; }
};

class B : public Abstract {
public:
void func() const { cout << "B" << endl; }
};

int main()
{
Caller c(new A);
c.func();
c.set_x(new B);
c.func();
return 0;
}

Jul 23 '05 #4
He is probably not allowed to modify A and B. If he could, then I don't
see why you would need a Caller class at all.
class Caller {
Abstract *x;
public:
Caller() { x = NULL; }
Caller(Abstract *x) { this->x = x; }
~Caller() { if(x) delete x; }
void func() { if(x) x->func(); }
void set_x(Abstract *x)
{ if(this->x) delete this->x;
this->x = x; }
// u might wanna try overloadin da = operator
// but keep in mind that <Caller obj> = new <Abstract derived> won't
work!
};
Well, i hope you realize you are passing a pointer to an object you have
not created and then you are deleting it in the destructor. That's not
good "OO" either. You need to keep in mind implementation details, too
bad. There are some typical solutions for such problems, like reference
counter, master pointers, or the simple thing of making responsible for
the deletion of the object to its creator.
forayer wrote: Jason Heyes wrote:
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept?

i hope u do realize the fact that only Derived (or now Adapter) is the
_only_ derived class of Abstract. classes A & B r diff classes. ur
template class imposes the restriction on objects that they must have
func() defined. i wouldnt exactly attribute such template restrictions
to OO. instead u might wanna try:

class Abstract {
public:
virtual void func() const = 0;
};

class Caller {
Abstract *x;
public:
Caller() { x = NULL; }
Caller(Abstract *x) { this->x = x; }
~Caller() { if(x) delete x; }
void func() { if(x) x->func(); }
void set_x(Abstract *x)
{ if(this->x) delete this->x;
this->x = x; }
// u might wanna try overloadin da = operator
// but keep in mind that <Caller obj> = new <Abstract derived> won't
work!
};

class A : public Abstract {
public:
void func() const { cout << "A" << endl; }
};

class B : public Abstract {
public:
void func() const { cout << "B" << endl; }
};

int main()
{
Caller c(new A);
c.func();
c.set_x(new B);
c.func();
return 0;
}

Jul 23 '05 #5

"forayer" <th********@hot mail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Jason Heyes wrote:
> I use a template class called Derived to instantiate derived classes of> class Abstract. Is this an OO concept?

i hope u do realize the fact that only Derived (or now Adapter) is the
_only_ derived class of Abstract. classes A & B r diff classes. ur
template class imposes the restriction on objects that they must have
func() defined. i wouldnt exactly attribute such template restrictions
to OO. instead u might wanna try:


Bad design. And thanks for the memory leak. The Abstract class requires a
virtual destructor since you are deleting derived objects through an
Abstract pointer. Look up initialization lists as well.

#include <iostream>
#include <stdexcept>

class Abstract {
virtual ~Abstract() { std::cout << "~Abstract" \n; }
public:
virtual void func() const = 0;
};

class Caller {
Abstract *x;
public:
Caller() { x = NULL; }
Caller() : x(0) { }
Caller(Abstract *x) { this->x = x; }
Caller(Abstract *p) : x(p) { }
~Caller() { if(x) delete x; }
void func() { if(x) x->func(); }
void func() throw(std::exce ption)
{
try
{
if (x == 0)
throw std::exception( "Error: func() called on a null object\n");
x->func();
}
catch( const std::exception& e)
{
std::cout << e.what();
}
}
void set_x(Abstract *x)
{ if(this->x) delete this->x;
this->x = x; }
where is the copy constructor? If you choose to disable it:

private:
Abstract(const Abstract& r_copy_);
// u might wanna try overloadin da = operator
// but keep in mind that <Caller obj> = new <Abstract derived> won't
work!
};

class A : public Abstract {
public:
void func() const { cout << "A" << endl; }
};

class B : public Abstract {
public:
void func() const { cout << "B" << endl; }
};

int main()
{
Caller c(new A);
c.func();
c.set_x(new B);
c.func();
return 0;
}


with an Abstract non-virtual d~tor (or the compiler generated equivalent):

A
~Abstract
B
~Abstract

.... memory leak ... ~A and ~B are never invoked

with Abstract virtual d~tor:

A
~A
~Abstract
B
~B
~Abstract

.... ok ...

Jul 23 '05 #6
david wrote:
He is probably not allowed to modify A and B. If he could, then I don't
see why you would need a Caller class at all.
You're right, if that's the case then it doesnt.
Well, i hope you realize you are passing a pointer to an object you have
not created and then you are deleting it in the destructor.


Where am I doing that??? As far as I can see, I am just passing a base
class pointer, which could very well be pointing to a derived class
object. besides, i do check if it is a null pointer before deleting the
object pointed to.
Could you clarify your statement, please?

forayer

Jul 23 '05 #7

Sorry, i understand my english is bad, i'll make an effort:
Caller(Abstract *x) { this->x = x; }
void set_x(Abstract *x) these are two ways of initializing a Caller object. You are passing as
parameters an Abstract object that someone else created. ~Caller() { if(x) delete x; }

In the above destructor you are deleting the object pointed by x, but of
course you don't know wether it's a shared object, and you are deleting
it, so you are imposing through your particular implementation that just
the object Caller may use the object pointed by x and no one else. This,
for example, wouldn't be valid:

void do_bad_things (Abstract * a) {
Caller caller ( a );
caller.func ();
.... // more things
} <<<< After returning, the object pointed by "a" has been deleted.

This would be better:
~Caller() { }
Jul 23 '05 #8
david wrote:
> Caller(Abstract *x) { this->x = x; }
> void set_x(Abstract *x)

these are two ways of initializing a Caller object. You are passing as
parameters an Abstract object that someone else created.


Well, I am passing only a pointer to the object. Besides, whatever
object the passed pointer points to has to be a derived class object of
class Abstract (since class Abstract is, well, abstract!). I figured
that since:
Abstract *x = new A;
x->func();
x = new B;
x->func();
is valid, I could use the same technique in class Caller. Of course,
this makes sense only if class Caller is made use of as a class
factory.

> ~Caller() { if(x) delete x; }

In the above destructor you are deleting the object pointed by x, but of
course you don't know wether it's a shared object, and you are deleting
it, so you are imposing through your particular implementation that just
the object Caller may use the object pointed by x and no one else.


I agree. But if you take a look at the main() function in the first
post by Jason Heyes, you will see that the Derived<A> object is left
dangling when Derived<B> is created and assigned. So I thought a little
cleanup action might tidy up such things.

rgds,
forayer

Jul 23 '05 #9

"forayer" <th********@hot mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
david wrote:

I agree. But if you take a look at the main() function in the first
post by Jason Heyes, you will see that the Derived<A> object is left
dangling when Derived<B> is created and assigned. So I thought a little
cleanup action might tidy up such things.


And you dangled both since:

~Caller() { if(x) delete x; }

only deletes the Abstract portion of the derived class (x is a base pointer
and Abstract doesn't have a virtual d~tor).

Jul 23 '05 #10

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

Similar topics

10
2339
by: nop90 | last post by:
Proof of concept: Currently I have a web hosting service and it does support php. Can the following be done in php? Explanations or examples would be appreciated. Create 2 applications, app-1 and app-2 app-1 loops continuosly. Every 1 second the application writes the time of day to a file.
1
3883
by: Dick | last post by:
Before going in to details i like to understand more about concept and design of this database. Most books go directly in to details. Please, can a current admistrator gife me information on this? Thanks in advance, Dick vd Spek email: ds@dds.nl
4
3921
by: jabailo | last post by:
These guys have it going on. They actually use design ideas that Microsoft employees can drool upon. Visual Studio 2010 Concept IDE http://www.codeproject.com/csharp/Concept_IDE.asp "Like a concept car strutting its stuff, the Visual Studio 2010 Concept IDE is packed with over-the-top features and weird ideas. Tame as it may look, this UI designed for one thing: jamming out code. Save the bizarre
3
1544
by: Ramza Brown | last post by:
I have this concept I throwing together. It is not professional or anything, just something I thought might be interesting. The concept is business(me) - to - consumer. It is, What did you buy?. You log into this site and describe purchases you might have made over time. It might be simple like: 'Tommy Shirt cost $30' Whatever...you submit that and you get to analyze this stat against other's user's Tommy purchase for example.
0
1160
by: duelearning | last post by:
I do not think that on the current market, or being developed, there is a real concept search tool. The key issue is that nobody has offered a satisfactory answer on what a concept is and its structural formula (Epistemological solution)before any computer programming language is applied. What we have now is just a some kind of "keyword linking" tricks or schemes, similar as what Bill Gates describes, just "a bunch of links" out of a...
7
2949
by: srinivas | last post by:
Hi, I am a asp programmer.I am displaying the db records in the html pages in a web page.I have 500 columns and 1000 rows in that html table.Here i am planning to implement the "MS-Excel Freeze Panes" concept.How to implement this concept in a web page.If any body have the code or good resource please send me. Thanks in Advance
4
1899
by: mohan | last post by:
Hi All, How to implement virtual concept ( dynamic polymorphism ) in c. I guess i should create a void pointer which is pointing to the function. Not clear about this Does anyone have some idea Mohan
1
1752
by: nimrod | last post by:
i have this problem to solve but not sure how to go about it.can anyone help me please. Environment = market Concept one to many each have action buy and action sell When multiple concepts enter the environment and all perform buy and sell actions the demand or supply will drop. This will affect the environment and the environment will alter its state to and change the rates based upon demand and supply. This in turn will result in the...
0
1211
by: ParasakthiGuru | last post by:
hai, i don't know Replication concept if anybody can know that replication concept pls sent to me replication brief tutorials By Guru
2
2725
Subsciber123
by: Subsciber123 | last post by:
I am writing a program to create family trees. It is stable, but I would say that it is still in the pre-alpha stage. Anyway, I would like to be able to export the tree to a concept map. Does anybody know of either a module in python that supports making concept maps or a program that stores its concept maps in an easily duplicable xml format? Any code that anybody has written in python that is relevant in the slightest to creating a concept...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.