Thread: Inheritance problem

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thanks for your reply.

    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, this:
    Code:
    SpaceVector Cross(SpaceVector &sv2){
    should be:
    Code:
    SpaceVector SpaceVector::Cross(SpaceVector &sv2){
    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    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. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    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 subscribe to a feed

Similar Threads

  1. Inheritance problem
    By logicwonder in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2006, 10:14 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 09:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM