Using Constructor of base class for constructor in derived?

Hi!

I'm trying to understand how the inheritance works but ran into some issues. I've been able to solve most of them with other forum threads but I have a feeling that I'm missing something important ;).

I have a base class called "Matrix" with a .cpp and .h file.
There, the constructor Matrix(int n, int m) creates a matrix with n rows and m columns.

In addition, I would like to make an "Eye" class for the Identity Matrix with a separate .cpp and .h file.
My idea was to have a constructor Eye(int n) which would look something like this:
1
2
3
4
5
6
Eye::Eye (int n) {
    Matrix temp(n,n);
    for (int i=0; i<n ; i++) {temp.set(i,i,1);} 
    //set(int a, int b, int c) is a method of base class which assigns the value c to the spot (a,b) in the Matrix
    (*this)=temp; // I know this doesn't work, but that's sort of the idea
}

Issues:
1. My values are saved within a "protected" array of numbers (I'm using vector< vector< double>>). It looks like I cannot change a protected value from a member of the base class in a method of the derived class, therefore I need to use this public method "set(...)". Is that correct?
2. The assignment operator doesn't work because it doesn't know how to convert "Matrix" to "Eye".. or vice-versa. That makes sense... but how can I achieve that effect?

Thanks for some help!

Ced

Last edited on
1. My values are saved within a "protected" array of numbers (I'm using vector< vector< double>>). It looks like I cannot change a protected value from a member of the base class in a method of the derived class, therefore I need to use this public method "set(...)". Is that correct?


Yes, if the Eye is not inherited from Matrix. But you may define Eye as a friend class if you want to access its private or protected memebers.

2. The assignment operator doesn't work because it doesn't know how to convert "Matrix" to "Eye".. or vice-versa. That makes sense... but how can I achieve that effect?


You have to define an operator= that tells the compiler how those classes are related:

1
2
3
4
5
Eye& operator=(const Matrix& src)
{
    //for example
    this->Matrix.matrix = src.matrix; //copies the internal vector<vector<double> > called matrix.
}
Last edited on
1. protected allows you to access base members in the derived class (as opposed to private).
2. It does work. An Eye is a Matrix if it derives from Matrix, so static_cast<Matrix&>(*this)=temp; or Matrix::operator=(temp); works.
3. Both approaches aren't the way to go. You can call a base constructor as follows:
Eye::Eye (int n) : Matrix(n,n) {}

4. Inheritance isn't appropriate here. An indentity matrix is a matrix, but it is no longer an identity matrix once you change it (however, the type remains). Instead, create a static Matrix method that returns an identity matrix.
Last edited on
Thanks for the replies!

@The Destroyer:
Eye is inherited from Matrix as class Eye : public Matrix. But when I tried to call temp.mat[n][n] = 1; (mat is the name of my number array in the Matrix class), I got the following error:
1
2
matrix.h:13:34: error: ‘std::vector<std::vector<double> > Matrix::mat’ is protected
eye.cpp:6:10: error: within this context

Accessing "mat" for a member of Eye from within Eye works, but I couldn't access "mat" belonging to "temp", which is a "Matrix", from within "Eye".

@Athar:
to 2: Maybe the error lies somewhere else then. But my compiler was complaining:
1
2
3
4
eye.cpp:8:15: error: no match foroperator=’ in ‘*(Eye*)this = temp’
eye.cpp:8:15: note: candidate is:
eye.h:7:7: note: Eye& Eye::operator=(const Eye&)
eye.h:7:7: note:   no known conversion for argument 1 from ‘Matrix’ to ‘const Eye&’

to 3: Worked great, thanks!
to 4: Seems obvious now that you say it, thanks. I haven't really looked at static methods yet, will do.
Last edited on
Accessing "mat" for a member of Eye from within Eye works, but I couldn't access "mat" belonging to "temp", which is a "Matrix", from within "Eye".

Yeah, you can access your own protected Matrix internals, but not those of other objects.

About 2.:
Yeah, to avoid calling Eye's assignment operator, explictly call the one of the base class.
Topic archived. No new replies allowed.