How to define an object from Class A in Class B

This is a discussion on How to define an object from Class A in Class B within the C++ Programming forums, part of the General Programming Boards category; Hello guys! I'm a beginner in C++ programming and I've a (stupid, I think) doubt about how to pass an ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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,681
    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,305
    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,311
    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%.

  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, 01: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, 04:57 PM
  5. Replies: 3
    Last Post: 02-12-2002, 09:09 PM

Tags for this Thread


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