Solved

class in a class

Posted on 2000-03-09
7
213 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
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
Comment Utility
Hello, Zoppo, how did you get it there before me?
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now