Solved

class in a class

Posted on 2000-03-09
7
217 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

828 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