Thread: Conversions between base classes and derived classes

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    15

    Conversions between base classes and derived classes

    Cosider this little program:

    Code:
    #include <iostream>
    using namespace std;
    
    
    class figura {
    public:
    	int j;
    	virtual void imprime() =0;
    	
    	//constructor
    	figura () : j(3) { }
    };
    
    
    class d_figura2 : public figura {
    public:
    	virtual void imprime() { cout << j << endl; }
    	
    	//method
    	void muda() { j = 9; }
    };
    
    
    class d_figura3 : public figura {
    public:
    	int retorna() { return j; }
    };
    
    
    
    int main () {
        figura *minhafigura;
    	d_figura2 minhafigura2;
    	d_figura3 *minhafigura3;
    	
    	
    	minhafigura2.imprime(); // prints the number 3 (j=3)
    	minhafigura2.muda(); // change j=3 to j=9
    	minhafigura2.imprime(); // now prints the number 9 (j=9)
    	
    	minhafigura = &minhafigura2;
    	
    	minhafigura->imprime(); // prints the number 9 again (j=9)
    	
    	/* 
    	 *	Now I want to use the method retorna() from class d_figura3.
    	 *	But, I need to do that from the object minhafigura2,
    	 *	because of its previous definitions, e.g., variable j.
    	 *	What am I suppose to do?
    	 */
    		 
    	// thought something like that:
    	minhafigura = minhafigura3;
    	minhafigura3->retorna();
    	// of course, it didn't work because *minhafigura3 isn't an object, so it was not initialized
    
        return 0;
    }
    Could anyone help me on that?

    Thanks

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Your base class is a pure virtual class and the second derived class becomes also virtual as you are not defining the virtual function in it. That's why you can't instantiate it's object. Define that virtual function inside the second derived class and you'll be able to create object.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    yes you are right. but I wasn't enough clear about my intentions.
    I dont want to create an object as: ' d_figura3 minhafigura3; '

    I want to acces the method retorna() --- inside the class d_figura3 ---- from the object minhafigura2, of typed name d_figura2.


    Thank you.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    You can't access it from minifgura2 unless you make it a friend class of d_figura2.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    hmmmm, I could do that.
    But, if I do so, i.e., inside of d_figura2 declare: friend d_figura3.

    How should I acces retorna() from the object minhafigura2 ?
    what is the line command?

    Thanks

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    This may sound weird because it isn't exactly technical, but stay with me.

    There is no `int figura3::retorna(void)' to call because there is no `figura3' with which to call it and there is no `figura3' because one can't yet exist.

    Yea, that doesn't sound to bad, but I'm not sure what you are trying to do as the code has a some errors and you seem to be trying to convert from one derived class to a sibling instead of a parent.

    I can't really help until I know what you are trying to do, but other will be along. Until then, if you want to invoke a method, you need an object, an instance, of the type to which that method belongs. That object can be an instance of a class, contained as a member of a class, or as a parent of class. One way or the other, you need that object.

    Soma

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mr.777 View Post
    You can't access it from minifgura2 unless you make it a friend class of d_figura2.
    Erm, retorna is a public function. You don't need friends.
    But it is inside d_figura3, and not d_figura2, which really means that need an instance of d_figura3 to use. How to do or why are questions I cannot answer since I don't understand what you're trying to do.

    There are lots errors involving pointers, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by Elysia View Post
    Erm, retorna is a public function. You don't need friends.
    But it is inside d_figura3, and not d_figura2, which really means that need an instance of d_figura3 to use. How to do or why are questions I cannot answer since I don't understand what you're trying to do.

    There are lots errors involving pointers, too.
    He/she needs to call it from the instance of difigura2. That's why i said him to use friend classes.
    I think that's the only way to access using the object of one class and calling other member function of another class...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Erm, no.
    All you need--anywhere--is an instance of the class whose member functions you want to use.
    Now, if difigura2 wanted to use a private member function in difigura3, then you would need friends.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Okay but from difigura2, how he/she may access public functions?
    May we do like;
    Code:
    difigura2 obj;
    obj.member_Function_Of_Class_Difigura3();
    So?
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, you can do that. You need some sort of instance.
    But if this is right or not, I cannot say from this context.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Okay. So, tharnier, you can just do like this.... as mentioned above.
    You just need to define the virtual function inside the difigura3 class so that it's no more virtual.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "You can do" does not mean it's right.
    What relationships does difigura2 have with difigura3?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    Apparently, call a method retorna() -- inside of the class d_figura3 -- from an object instantiate to class d_figura2 is not the best way to go.

    So, I will explain right exactly what I need, in order get a better solution from you.

    I created an object minhafigura2, from class d_figura2. It object uses the method muda() to set the value 9 to the variable j -- declared public into figura, the base class.

    As we can see in class d_figura3, the method retorna() uses the variable j too, from base class figura. But what value of j will it uses? The answer is Depend.

    1) If I create an object of class d_figura3, then it will use the value 3, because this value was initialized by the constructor figura() -- constructor of the base class;

    2) If I create a pointer object, i.e., a pointer to class d_figura3, then it wont uses nothing at all. Because the pointer was not instantiated, so the variable does not exist.

    3) What do I need?? I need that the method retorna() uses the value 9, set to the object minhafigura2.


    Yes I know, I could change my code in order to class d_figura3 receive the variable j thru argument. But I need to avoid that.

    I thought it would be possible using an object (or pointer) from class figura, because it is the same base class for both d_figura2 and d_figura3 classes.

    So that is my point. Any idea to the 3) item?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Virtual Base Classes
    By skewray in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2006, 06:56 PM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM

Tags for this Thread