Solved

What a way is more OOPer ?

Posted on 2002-07-06
4
213 Views
Last Modified: 2010-04-01
I must to implement Quaternion and Complex numbers classes. I began so:

template <class T>
class Quaternion
{
 public:
  ...
  Quaternion(); // few kinds of Ctors
  ...
 private:
  T w,x,y,z; // w+xi+yj+zk;
};

I have 3 ways to continue:

1-st way:

template <class T>
class Complex1
{
  public:
   ...
   Complex1(); // few kinds of Ctors
   ...

  pirvate:
   Quarterion<T> q;  // I use the q as complex number
   // and its y,z fields initialized by zero;
};

2-nd way:
template <class T>
class Complex2:public Quarterion<T>
{
  public:
  ...
  Complex2(T a, T b):Quarterion(a,b,0,0){}
  // yet few needed Ctors
  ...
};

3-rd way:
template <class T>
class Complex3:private Quarterion<T>
{

//  Additional question (accordingly I add points):

  How can I to declare here function from Quarterion class so that they should public (I don't want that user of    Complex3 class should be able to use function from Quarterion beside of function I'll permit for using)?
};




What way from these is prefer ?



Thank you.
0
Comment
Question by:VR_
  • 2
4 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 7134050
This is known as the ellipse-circle dilemma (or the rectangle-square dilemma). The basic answer is that you should do what makes more sense in your context, and not necessarily reflect what mathematicians consider to be the true taxonomy.

For more info see http://ootips.org/ellipse-circle.html
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 100 total points
ID: 7135060
I think the Liskov Substitution Principle (also mentioned in the link yonat gave above), shows that at least public inheritance is probably the wrong answer in this case.

LSP is the principle of substitutability,  it states that for public inheritance you should be able to use (in this case) a Complex number *anywhere* you can use a Quaternion.

This is probably not the case - for a Complex number you require that the y & z fields are presumably always zero.  Therefore if any operation on Quaternion violates this condition, then inheritance is a bad choice.  e.g. if there is a public method on a Quaternion that changes the y or z fields, then public inheritance is the wrong choice.

Private inheritance *might* be the right answer, but only if either i) you need to access protected methods of the base class or ii) you need to override virtual methods of the base class.

As such, the right answer is most likely to be containment/aggregation (your 2nd way), although as yonat suggests, this very much depends on the details of what you are doing.
0
 
LVL 1

Author Comment

by:VR_
ID: 7137290
I implement it through aggregation, although before it I tried private inheritance, where I meet with next problem:
I've adjusted access control to member function which I need in Complex for example:

template <class T>
class Quaternion{
...
public: Quaternion<T> operator+ (const Quaternion<T> q);
...
};

template <class T>
class Complex:private Quaternion<T>{
...
using Quaternion<T>::operator+;
...
};

and in user code:

...
Comlpex c1(1,2),c2(3,4),c3;
c3=c1+c2;
cout << (c1+c2);
...

// of course I have comp. error. So, although I have access to op+ in Quaternion I cannot use it with Complex parameters !
I tried partialy solution as definition
operator Quaternion<T>(), but anyway returned value is Quaternion<T>  :(
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 7137664
> So, although I have access to op+ in Quaternion I
> cannot use it with Complex parameters !
> I tried partialy solution as definition

it just won't work as you have it, this kind of exposure of Quaternions methods is a bad idea... how will you deal with:

Quaternion c1;
Complex    c2, c3;
....
c3 = c1 + c2;

this would be bad.  

This is exactly one of the cases I described above... substitutability fails here.  (and by making Quaternions methods public, you are in effect implementing a kind of selective public inheritance).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now