Composition question

Hello all,
I am new to this forum and new to programming. In class, we recently went over composition and I am still having trouble getting my program to execute. I am thinking my professor just isn't explaining it to me in a way I can comprehend for practical use. I understand what I should be able to do, but actually implementing it and being able to use it universally is just not happening. Mainly, I am having trouble calling other objects from other classes into another class. I am sure the last thing someone wants to do is read my code, so I will do my best to refrain from that if possible. If anyone can just break it down differently, or possibly give a small example, it would be greatly appreciated. I love programming and refuse to let some small misunderstanding prevent my progress. Thank you in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <iostream>
using std::cout;
using std::endl;

class someClass
{
   public:
   int intInSomeClass;
};


class someOtherClass
{
  public:
  int intInSomeOtherClass;
  someClass c;
};


int main()
{
  someOtherClass instantiatedObject1;  // Create an object of type someOtherClass 
  instantiatedObject1.intInSomeOtherClass=7; // this object contains an int called intInSomeOtherClass
  instantiatedObject1.c.intInSomeClass=12; // .... and also contains an object of the type
                                          //  called someClass, where the instance of it is named 'c'
  
  cout << instantiatedObject1.intInSomeOtherClass << " " << instantiatedObject1.c.intInSomeClass << endl;
  
return 0;
}


Two classes are defined, and one of them contains an instance of the other.

Output is:

7 12


Last edited on
Thanks Moschops. I have seen your posts around the beginner forum and appreciate your help.
Topic archived. No new replies allowed.