S
Silver
First of all...merry christmas everyone!
Now, I have to write a program. Among other things, I need to overload the
'*' operator, so that it returns a pointer-to-ponter of type int, which is
intended to contain a 2d array.
Now, I have 2 classes, as follows:
class A
{
protected:
int n; // It's the size of a nxn matrix. Is read from the
keyboard in the constructor
int **p; // memory is dynamically allocated so that p can be
used as an nxn array
public:
A() { } // Default constructor
A(int x); // Constructor with argument
~A(); // Destructor
};
class B: public A // INHERITANCE
{
private:
int k; // size for a kxk array. Is read form the keyboard in the
constructor
int **d; // memory is dynamically allocated so that d can be
used as an kxk array
public:
B(int y);
~B();
};
In the constructor definitions, I allocate memory dynamically, plus I read
the values for the array. In the main program, 2 objects are created :
A a;
B b;
What I am supposed to do, is use the overloaded operator in main(), so that
b.d contains the product of a.p * b.p. ( the funny thing is that a.p*b.p is
nxn array while b.d is an kxk array!! )
I am having some difficulties as to where to define the overloaded operator:
---as a member of class A?
---as a member of class B?
---friend perhaps?
cause whatever I try, I keep getting the error that I cannot access some
class member. The last thing I tried was defining accessors in each class
( get_d, get_p etc) which looked very silly. It compiled, but I got a
run-time error
Could someone advise me on how to implement the overloading ?
PS. Using VS .NET
Thanks in advance!
Now, I have to write a program. Among other things, I need to overload the
'*' operator, so that it returns a pointer-to-ponter of type int, which is
intended to contain a 2d array.
Now, I have 2 classes, as follows:
class A
{
protected:
int n; // It's the size of a nxn matrix. Is read from the
keyboard in the constructor
int **p; // memory is dynamically allocated so that p can be
used as an nxn array
public:
A() { } // Default constructor
A(int x); // Constructor with argument
~A(); // Destructor
};
class B: public A // INHERITANCE
{
private:
int k; // size for a kxk array. Is read form the keyboard in the
constructor
int **d; // memory is dynamically allocated so that d can be
used as an kxk array
public:
B(int y);
~B();
};
In the constructor definitions, I allocate memory dynamically, plus I read
the values for the array. In the main program, 2 objects are created :
A a;
B b;
What I am supposed to do, is use the overloaded operator in main(), so that
b.d contains the product of a.p * b.p. ( the funny thing is that a.p*b.p is
nxn array while b.d is an kxk array!! )
I am having some difficulties as to where to define the overloaded operator:
---as a member of class A?
---as a member of class B?
---friend perhaps?
cause whatever I try, I keep getting the error that I cannot access some
class member. The last thing I tried was defining accessors in each class
( get_d, get_p etc) which looked very silly. It compiled, but I got a
run-time error
Could someone advise me on how to implement the overloading ?
PS. Using VS .NET
Thanks in advance!