Jump to content

Can an object exist within an object?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

Someone was saying that an object can exist within an object. The discussion was about classes. I know that a class can exist within a class but don't get how an object can exist within some other object. Does it make any sense to you? Please let me know. Thank you.

Regards
Jackson
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
An object exists within another object only when a class contains other objects or classes


class X

{

 public:

    int x;

}


int main()

{

   X h; // now h contains an instance of x


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Yes.
class X

{

public:

int a;

};

class Y

{

public:

X x;

};

int main()

{

Y g;  //here object g contains x which is object of X

g.x.a=10;

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users