Solved

class in a class

Posted on 2000-03-09
7
218 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 30 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
Industry Leaders: 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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 91
Problem with SqlConnection 4 192
C++ mouse_event mouse look 7 131
MFC COM Server not showing  form 4 25
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

739 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