Connecting Tech Pros Worldwide Help | Site Map

inheritance or friendship?

Member
 
Join Date: Oct 2009
Posts: 33
#1: Oct 11 '09
hello...
i am looking for a better way of doing Class Matrix.

the current code i have now is Class Vector which is inherited from Class Base.

Class Matrix needs the vector in Class Vector and need some of its private member func like the number of column and protected member that is the vector itself.
the common function between Class Vector and Class Matrix are tostring (convert int to str) and few overload operator functions.


eg for tostring method.the way to implement it is a bit different since vector convert the string via each column and matrix convert its int to string by each row and column.

which method would give me advantages in time.inheritance or friend?in what way?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,156
#2: Oct 11 '09

re: inheritance or friendship?


Matrix should not inherit from Vector because a matrix is not a specialisation of a vector. In fact if anything a vector is a specialisation of a matrix that only has one column however even that might not work as vector and matrix operations tend to be different.

I suggest you read up on Liskov substitution principle and Design by contract.
Member
 
Join Date: Oct 2009
Posts: 33
#3: Oct 12 '09

re: inheritance or friendship?


thanks for the link! :)
Reply