[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

class in a class

Posted on 2000-03-09
7
Medium Priority
?
222 Views
Last Modified: 2010-04-02
Sir, I have created a program in which I created a class called bowler. now I created a new class called node and included an object of bowler class in it as a member. How can I access the member variable of class bowler through node class in main? is their any solution?
0
Comment
Question by:jamanat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 2600151
Hi jamanat,

there are multiple possibilities, i.e.:

class bowler {
public:
 int m_iNumber;
 int& GetNumber(){ return m_iNumber; } // returns a reference
....
}

class node {
public:
 bowler m_cBowler;
 int& GetBowlerNumber(){ return m_cBowler.GetNumber(); } // returns a reference
....
}

foo ( node& obj )
{
 int number = obj.m_cBowler.m_iNumber; // direct access
 obj.GetBowlerNumber() = 5; // access via wrapper function, mostly the better way
 obj.m_cBowler.GetNumber() = 5; // the same as befor
// for pointers
 node* pNode = &obj;
 number = pNode->m_cBowler.m_iNumber; // direct access
 pNode->GetBowlerNumber() = 5;
 ...
}

hope that helps,

ZOPPO
0
 
LVL 22

Accepted Solution

by:
nietod earned 90 total points
ID: 2600153
It woudl be a very good idea to post your code with a question like this.  But you probalby have something like

class bowler
{
   public:
     int i;
};

class node
{
    public:
        bowler b;
}

Then in main you can have

int main()
{
   node n;

    // set the int called i inside the bowler inside the node to 5.      
    n.b.i = 5;

    rethrn 0;
};
0
 
LVL 22

Expert Comment

by:nietod
ID: 2600165
Now if the data member ("i" in this case) is not public, then you might need to provide "data member access" functions to let you set the data member.  Like

class bowler
{
   int i;
public:
   void set_i(int x) { i = x; };
};

class node
{
   bowler b;
public:
   Set_b_i(int x) { b.set_i(x); };
}

int main()
{
   node n;

   // set the int called i inside the bowler inside the node to 5.  
   n.set_b_i(5);
    return 0
}


Let me know if you have any questions.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Expert Comment

by:nietod
ID: 2608180
Hello, Zoppo, how did you get it there before me?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2611295
Hmmm, do not know ... directly before I posted my comment I reloaded this question and there wasn't anything posted yet...
0
 
LVL 22

Expert Comment

by:nietod
ID: 2611854
I was two minutes later, but I didn't see you there until later.  Or I would have withdrawn my answer.   You probably shoudl have answered, it certainly seems like that qualifies as an answer.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2611976
Seems there're sometimes problems with EE's comment database (I know it coz once they lost one of my comments, Q.10288780) ... but who cares   :)

>You probably should have answered...
I'll try to get used to ...

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question