Thread: How to define an object from Class A in Class B

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    7

    How to define an object from Class A in Class B

    Hello guys!
    I'm a beginner in C++ programming and I've a (stupid, I think) doubt about how to pass an object's class into another class. Suppose we have these two classes:
    Code:
     
    
    • class A {...}
    • class B {...}
    and I want to use an object from A into B. For example:
    Code:
     
    
    • class B {
    • A ab;
    • [methods prototypes that include the object ab]
    • method_B (A ab); //for example
    • ...}
    The question is, can I do this? Does it make sense, thinking about object-oriented programming?
    Or, I could define an A's object in main() and after that I would call a method from B that would include A's object as argument?
    My question is all about how to use object's from another class into another (functionally independent!) without "violating" the object oriented programming rules.
    Thank you for any help,

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Nothing wrong with one object containing another object.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    I think you didn't understand my question.

    My point is, can I use an A's class object into another class B?
    Can I do that? Does it respect all the philosophy of object-oriented programming?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Of course!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's called "Composition". It's used all the time.

    By the way, Method_B wouldn't have to take ab as a parameter if you simply wanted to access the ab from that insteance of B.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rocksoad23 View Post
    I think you didn't understand my question.

    My point is, can I use an A's class object into another class B?
    Can I do that? Does it respect all the philosophy of object-oriented programming?
    Those questions have already been asked and answered.

    The questions you have asked have justified the answers you have received so far. If people aren't understanding your question, or not giving you some specific information you're seeking, then you need to ask your questions more clearly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Thanks, I got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-31-2011, 02:23 AM
  2. Replies: 10
    Last Post: 05-22-2009, 11:12 PM
  3. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  4. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  5. Replies: 3
    Last Post: 02-12-2002, 10:09 PM

Tags for this Thread