Solved

class in a class

Posted on 2000-03-09
7
216 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 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
TTimer no longer functioning 7 91
Exception thrown at 0x00007FFD5BC81F28 7 44
operator overload: incompatible type and too many params 5 77
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

778 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