• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

C++: Inheritance and Overloading

Joined
5/14/07
Messages
85
Points
16
Quick question for those of you who are more knowledgeable in C++...

Let's say I have a two classes:

C++:
class Matrix
{
private:
[INDENT]int _rows;
int _columns;
[/INDENT]};

class SuperMatrix : public Matrix
{
public:
[INDENT]SuperMatrix(const SuperMatrix& sm2);
const SuperMatrix operator=(const SuperMatrix& rhs);
~SuperMatrix();
[/INDENT]private:
[INDENT]double* _m;
[/INDENT]}

Because SuperMatrix contains a pointer, it's going to need an explicitly defined copy constructor, destructor, and assignment operator. The copy constructor and destuctor are easy. The problem is the assignment operator.

How do I do this? The lhs needs the new rhs _rows and _columns values, but the operator procedure will have no ability to copy them over because they are private to the base Matrix class. (I cannot -- will not -- change this.) I'm sure there's something obvious I'm overlooking. Help!
 
I'm missing information. Is that your Matrix class? I would like to see the whole interface of the matrix class. Does the Matrix class has a copy constructor? How do you create a Matrix Class? From that interface you can not do anything with the Matrix class since there is no way to access its members.

Why can't you change the type of Matrix class members? I would suggest to change it to protected but you say you can't change it... why?

Those are quick pointers. Again, you will have to show more information in order to give you a better assessment. Actually you should provide the whole interface to the Matrix class. You can create a friend function to the class Matrix (I'm not a fan of friend functions).
 
Ok, I'm confused.

If I understand this, you're trying to do a "deep copy." You say "the copy constructor is easy" but have some trouble with the assignment overload. Actually, the code for the two should be nearly the same. If you're doing a copy constructor, you want to return a brand new copy.

As far as how to get the _row and _column for your copy, if you must do a copy this way and cannot make it protected (which is the standard way for doing this sort of stuff) you can create members 'int getRows()' and 'int getColumns()' to get the information and setRows/setColums to set them up.

Alain is right though, for a proper design to be done we'll need to see the whole thing. I don't know what you're using SuperMatrix for (as opposed to Matrix) but I suspect that the right way to design this is through containment rather than inheritance.

Joe

P.S. After posting this, I realized what exactly you are looking for.

The problem is you're doing a copy constructor/assignment overload in SuperMatrix. If you're going to do that, you had -better- have the same in in Matrix, right??? Now in your SuperMatrix copy constructor/assignment operator, just use the scope resolution operator Matrix:: to do the base class deep copy. There. Now you no longer need visibility to _row/_column; your base class copy constructor takes care of that for you.
 
I am asking this question because of a problem I'm having with my Matrix class, but this question is more of a general C++ issue. I probably shouldn't have used the word "Matrix" anywhere in my question at all, but just made two classes, "Parent" and "Child".

The major issue here is that I don't want any of the child classes changing the value of the base class private variables. They should only be set on creation, by the constructors, and during assignment by the = operator. This is why I don't want to use friend or protected qualifiers, nor do I want to create setRows() or setColumns() mutator functions.

Thanks, Joe, for the suggestion of using a base class overloaded = operator and copy constructor. Here's my next issue, though. What happens if the base class is an abstract type? (That's what they call it when there's a virtual function = 0, right? I'm still getting used to the terminology here.) If that's the case, I can't create a function whose return type is Base Class, such as operator=, because one can't return an abstract type.
 
The problem is you're doing a copy constructor/assignment overload in SuperMatrix. If you're going to do that, you had -better- have the same in in Matrix, right???

This is exactly what I was expecting but I didn't see it in the interface originally posted.
 
Adam, regarding the Matrix& operator= ...
This definitly works on an abstract Matrix class

Matrix& operator=(const Matrix& rhs);

and the cpp contains the implementation for the operator.
Let me know if you need the rest of code
 

Attachments

  • Matrix.doc
    291 bytes · Views: 21
Adam, regarding the Matrix& operator= ...
This definitly works on an abstract Matrix class

Matrix& operator=(const Matrix& rhs);

and the cpp contains the implementation for the operator.
Let me know if you need the rest of code

I'm not a C++ expert but I think this works because of polymorphism. SuperMatrix& operator=(const SuperMatrix& rsh) is an overloaded method. It does not override Matrix& operator=(const Matrix rhs). The two methods are completely different.
 
"If that's the case, I can't create a function whose return type is Base Class, such as operator=, because one can't return an abstract type."

I think the deal with the operator assignment is that it is doing a reference return. It passes back a Matrix&, not a Matrix. True, you can never return an instantiation of an abstract type. But you can return a pointer to any concrete type which inherits from the base class - and SuperMatrix is a concrete type which is also a Matrix. Polymorphism, don't 'cha know.

In other words, in the = overload, you grab the concrete class (like SuperMatrix) that is being assigned to. Your SuperMatrix implementation of &= will in turn call the Matrix &= to copy over the base class members to the assignment operator target. No problem there - you're never creating an abstract object. You're using an object that's already there.

Hope that helps,

Joe
 
Back
Top