Inheritance problem

This is a discussion on Inheritance problem within the C++ Programming forums, part of the General Programming Boards category; Hello I am new to the concept of inheritance. So I have a straight forward error I think. I want ...

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 08:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 10:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21