Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > OOP question

Reply
Thread Tools

OOP question

 
 
cppforlife
Guest
Posts: n/a
 
      08-27-2005
I have a question about using 'has a' inheritance. I want to know how
to extend class that had been used in another class. What i mean is if
i have AddressBook class and its private member AddressRecord class how
i can extend AdressRecord class without changing member of AdressBook


class AddressRecord;
class AddressBook{
public:
//...
private:
//...
AddressRecord* add;
};

class AddressRecord{
//..
};

if i will inherit from AddressRecord class i should change private
member od AddressBook class but it violates OCP(open closed principle).
How can i do that dynamiclly using OOP and if i use templates is it
good?

template<class T>
class AddressBook{
public:
//...
private:
//...
T* add;
};

Thank you.

//(E-Mail Removed)
//www.cppforlife.tk

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-27-2005
cppforlife wrote:
> I have a question about using 'has a' inheritance. I want to know how
> to extend class that had been used in another class. What i mean is if
> i have AddressBook class and its private member AddressRecord class
> how i can extend AdressRecord class without changing member of
> AdressBook


There are several implications by this existing design. First of all,
if 'AddressRecord' was meant to be extended, then 'AddressBook' must
have already been designed to accommodate that, by, for example, not
keeping the instances of 'AddressRecord' by value but keeping them by
pointers (or references). If that's so, then all you need to do is to
make sure your new class adheres to LSP by implementing the true "is-a"
relationship to 'AddressRecord'.

> class AddressRecord;
> class AddressBook{
> public:
> //...
> private:
> //...
> AddressRecord* add;


What's that? A dynamic array of AddressRecord objects? Then it won't
work. It has to be a dynamic [collection] of 'AddressRecord' pointers
if you want to keep the same implemenation of 'AddressBook' for the
entire 'AddressRecord'-base hierarchy.

> };
>
> class AddressRecord{
> //..
> };
>
> if i will inherit from AddressRecord class i should change private
> member od AddressBook class but it violates OCP(open closed
> principle).


I strongly recommend you re-examine the reasons why you need the changes
of which you're asking. Why do you need to derive something from the
'AddressRecord' class? It seems that the 'AddressBook'-'AddressRecord'
pair of types wasn't meant to be extended...

> How can i do that dynamiclly using OOP and if i use
> templates is it good?
>
> template<class T>
> class AddressBook{
> public:
> //...
> private:
> //...
> T* add;
> };


Here you're re-implementing 'AddressBook'. Yes, that's possible. This
is what is known as "compile-time polymorphism". Again, there has to
be a reason for everything. Why do you need 'AddressBook' a template?
Do you plan using it for yet unknown types like 'AddressRecord'?

V


 
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
OOP Question Elliot Rodriguez ASP .Net 7 02-14-2006 05:47 PM
General Question about java parameters / OOP good design techniques antoine Java 7 03-03-2005 05:01 AM
Simple OOP, polymorphism design question Jacek Dziedzic C++ 6 09-04-2004 01:47 AM
OOP Design Question Brenton Fletcher Java 4 11-20-2003 07:01 AM
OOP question in java Mike Deliberto Java 1 07-01-2003 09:24 PM



Advertisments