# Thread: Inheritance problem

1. ## Inheritance problem

Hello

I am new to the concept of inheritance. So I have a straight forward error I think.
I want to use a protected variable from the base class. But I get an error.
Is there somebody who could tell me how to do it right and wy this doesnt work?
Thank you

Code:
```class Vector {
public:
Vector(int size);
void Write(void);
void Set(int index, double value);

private:
int length;

protected:
double *array;
};

class SpaceVector: public Vector{

public:
SpaceVector();
void Set(double x, double y, double z);
SpaceVector Cross(SpaceVector &sv2);
};

SpaceVector Cross(SpaceVector &sv2){

SpaceVector sv3 = SpaceVector();
double x, y, z;

x = sv2.array[1];
return sv3;
}

Vector.h: In function ‘SpaceVector Cross(SpaceVector&)’:
Vector.h:19: error: ‘double* Vector::array’ is protected```

2. It looks like you defined Cross as a free function instead of a member function, so the member function named Cross was left unimplemented.

Why exactly do you want to use public inheritance here? You apparently have no virtual member functions.

But I dont understand what you mean with:
you defined Cross as a free function instead of a member function, so the member function named Cross was left unimplemented.
Could you explain me?

Also we have not covered virtual member functions yet in my class.
I have chosen public because I want to use the function Write from the base class in the main for a spacevector. If this is not the right way to do this, please tell me what is.

4. Basically, this:
Code:
`SpaceVector Cross(SpaceVector &sv2){`
should be:
Code:
`SpaceVector SpaceVector::Cross(SpaceVector &sv2){`
Originally Posted by thescratchy
Also we have not covered virtual member functions yet in my class.
Then you should not be using public inheritance yet.

Originally Posted by thescratchy
I have chosen public because I want to use the function Write from the base class in the main for a spacevector. If this is not the right way to do this, please tell me what is.
It looks like you do not need a SpaceVector class. The member functions for your current SpaceVector class can be free functions that operate on a Vector object.

5. Sorry I was confused by what you call: free and member functions.
I didnt know yet that you call them free functions.

So you are saying it is possible to do it without the SpaceVector class but its a exercise on using classes.
So if I want to use an member function from the class Vector in my main, on an object from SpaceVector, what is the best way to do this? Making the base class public? Or overwrite the function in SpaceVector or ..?

Thank you

6. Originally Posted by thescratchy
So you are saying it is possible to do it without the SpaceVector class but its a exercise on using classes.
You're using a Vector class. In fact, you could rename the Vector class into SpaceVector, and basically just have a single set of member functions that operate on a SpaceVector.

7. Ok I understand what you are saying but they want me to use the classes: Vector and SpaceVector.
And I need to write in the main the coordinates of the SpaceVector. But I have in the class Vector already the function Write to do this.

So what is the best approach?
Making the base class public? Or overwrite the function in SpaceVector or ..?
Thank you

8. Are you supposed to use inheritance? If so, then the best approach given your constraints is to do exactly what you did, even though it is a bad approach. Otherwise, the best approach is to store a Vector member variable in SpaceVector, even though there is a better approach that you are not allowed to use.

9. Yes, I need to use inheritance here but I will present your best approach to my teachers.

Thank you for all your help.

Popular pages Recent additions