Polymorphism & Inheritence

This is a discussion on Polymorphism & Inheritence within the C++ Programming forums, part of the General Programming Boards category; Hi, im trying to write a simple virtual function that a base object;s derived objects can use. But i seem ...

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Polymorphism & Inheritence

    Hi, im trying to write a simple virtual function that a base object;s derived objects can use. But i seem to keep running into problems, firstly:

    I have an object called LatticeNode which stores the object (cell or its derived type). This class also has a function called getCell which returns a reference to this object. Ill try to explain my code a little before i ask the questions.

    Eg. The LatticeNode class is:

    Code:
    class LatticeNode {
    
    	public:
    		int num;
    		Cell *t;
    
    		void set(int i, int j);
    		void setI(int x) { i = x; }
    		void setJ(int x) { j = x; }
    		void setNum(int i) { num = i; }
    		void inc();
    		void dec();
    
    		void inc_i() { i++; }
    		void inc_j() { j++; }
    
    		LatticeNode *next;
    		LatticeNode *prev;
    	
    		Cell* getCell();
    
    		//default constructor
    		LatticeNode(Cell *c);
    		LatticeNode();
    
    		int getI() { return i; }
    		int getJ() { return j; }
    		int getNum() { return num; }
    
    	private:
    
    		int i;
    		int j;
    };
    the getCell function is defined as this (with the constructor - this may be wrong also):

    Code:
    LatticeNode::LatticeNode(Cell *c)
    { 
        num = NULL; 
        next = NULL; 
        prev = NULL; 
        t = c;
    }
    
    Cell* LatticeNode::getCell()
    {
    	cout << "In get cell" << endl;
    
    	if(typeid( t ).name() == "Class Cell *")
    		return (Cell *)t;
    
    	if(typeid( t ).name() == "Class Juxtacrine *")
    		return (Juxtacrine *)t;
    
    	if(typeid( t ).name() == "Class Morphogen *")
    		return (Morphogen *)t;
    
    	return t;
    }
    Now ignore the typeid if statements, and look at the constructor. Am i getting this assignment correct? the t = c;?

    Does this assign the Cell object (or its derived type) to t? Ive checked at runtime and it seems to assign to the correct type, although im not sure...

    In the getCell function do i just need to do a return t; ? instead of all this typeid stuff? polymorphism would suggest i dont as it should return the correct object type even if its derived?

    Ive been using this code to try and test the getCell function but all i get are Cell * types back.

    Code:
                    Morphogen *a = new Morphogen();
    	Juxtacrine *b = new Juxtacrine();
    
    	LatticeNode *n = new LatticeNode(a);
    	LatticeNode *m = new LatticeNode(b);
    
    	cout << typeid( n->getCell() ).name() << endl;  
    	cout << typeid( m->getCell() ).name() << endl;
    in the cout statements all i get is "Class Cell *"

    Should i be getting "Class Morphogen *" etc? or am i doing something wrong?

    I am also trying to get a virtual function to work with this object hierarcy but if i try to call it i get an exception?? eg

    Code:
    class Cell 
    {
         public:
                 virtual void print() { cout << "im a cell" << endl; }
    };
    
    class M : public Cell
    {
        public:
                  void print() { cout << "M"<< endl; }
    };
    
    class J : public Cell
    {
        public:
                   void print() { cout << "J"<< endl; }
    };
    }
    when i call print() i get an exception if i use the getCell method to call it eg:

    LatticeNode *n; n->getCell()->print();

    Any ideas? as this is causing me loads of problems...!

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    I have been looking at the problem - i think its something to do with the assignment,, but im having trouble with it. basically i think i need an = operator to copy/create a new object?

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    LatticeNode::LatticeNode(Cell *c)
    { 
        num = NULL; //maybe num = 0; ?
        next = NULL; 
        prev = NULL; 
        t = c;
    }
    That code snippet shoudnīt case any problem as long you dont delete the cell- or dereived object outside LatticeNode eg
    Code:
    Morphogen *a = new Morphogen();
    LatticeNode *n = new LatticeNode(a);
    //This could be dangerous!!
    //delete a;
    You could avoid this by doing
    Code:
    LatticeNode *n = new LatticeNode(new Morphogen()); //yuk
    but then you have to remember to free it inside LatticeNode or else you have gotten a memory leak.

    In the getCell function do i just need to do a return t; ? instead of all this typeid stuff? polymorphism would suggest i dont as it should return the correct object type even if its derived?
    Just return t. It makes no sense to typecast it down because the virtual mechanism takes care of that problem. Eg if it points to a (M)orphogen the appropriate function will be invoked for (M)orphogen.



    I am also trying to get a virtual function to work with this object hierarcy but if i try to call it i get an exception?? eg

    Code:
    Code:
    class Cell 
    {
         public:
                 virtual void print() { cout << "im a cell" << endl; }
    };
    
    class M : public Cell
    {
        public:
                  void print() { cout << "M"<< endl; }
    };
    
    class J : public Cell
    {
        public:
                   void print() { cout << "J"<< endl; }
    };
    }
    when i call print() i get an exception if i use the getCell method to call it eg:

    LatticeNode *n; n->getCell()->print();

    Any ideas? as this is causing me loads of problems...!
    That cell-hierarcy works as it should, no errors there. Try it out with
    Code:
    ...
    Cell *pTest = new M;
    pTest->print();
    ...
    and M is printed out. But when you
    Code:
    LatticeNode *n; n->getCell()->print();
    you are using a pointer that doesnīt point to anything! Make it point to something and make sure that LatticeNode::t is initialized as well and then try out the second statement.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    ok thanks ill take these things on board, thanks for all the advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  3. Operator overloading and inheritence
    By ventolin in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 11:35 AM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 09:19 PM
  5. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM

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