[C++] Getting one object to access another object's data members

  • Comp Sci
  • Thread starter butterfli
  • Start date
  • Tags
    Data Members
In summary, the conversation discusses accessing data members of one object from another object. The solution involves passing a reference to the first object as a parameter into a method of the second object. It is also mentioned that declaring data members as private promotes encapsulation and that passing in parameters eliminates the need to declare variables in the second object. The conversation ends with a clarification on how to pass in a reference parameter and an example of how to call the method with the correct parameters.
  • #1
butterfli
10
0

Homework Statement


see #3

Homework Equations


N/A

The Attempt at a Solution


I'm working on this assignment and I got stuck at this part in programming here's what I need to do:

I need to find a way to have one object access the data members of another object.

A broken down/simplified version of my code looks like this:
Code:
class ClassOne 
{
public:
	ClassOne();	
	int m_SomeData;
	string getData();
};

ClassOne::ClassOne()  //setting data to 1
{
	m_SomeData = 1; 
}

string ClassOne::getData() 
{
	return m_SomeData;
}

What I want to do is:
Code:
class ClassTwo 
{
public:
	void showData();
};void ClassTwo::showData() 
{
	//pseudocode: cout << m_SomeData from ClassOne; 
	//how do I get this line to work? 
}

I know I have to pass in parameters to showData() and possibly dereference but I don't know what to pass in.
 
Physics news on Phys.org
  • #2
ClassOne is public, and so are its data members. If you create a ClassOne object, any other class has full access to the data and methods on ClassOne. To do what you want to do, I would write showData so that it has a parameter that is a reference to a ClassOne object.

In practice, though what you are trying to do is very seldom done, and is to be avoided. The data members of a class are usually declared using the private or protected access specifier, not public. Making the data private promotes encapsulation via data hiding.
 
  • #3
Mark44 said:
write showData so that it has a parameter that is a reference to a ClassOne object.
Can you show me how that code looks? I know I sound like I'm asking to be spoon-fed but the textbook I have does not have those examples.
 
  • #4
Code:
void classTwo::showData(ClassOne & classOneobj)
{
   // code for method
}

Inside this method you can call getData or evaluate m_someData directly.
 
  • #5
Code:
void classTwo::showData(ClassOne & classOneobj)
{
   cout << m_SomeData;
}

My compiler wants me to declare m_SomeData in classTwo because it's giving me an undeclared identifier error. I thought passing in parameters (ClassOne & classOneobj) was suppose to eliminate the need to do that...?
 
  • #6
m_SomeData is out of scope, so you can't use it that way. The idea of passing in a parameter to a function is to actually use the parameter, which you haven't done.

If you have an instance of a class, how do you use a method or property on that object?
 
  • #7
Oh, duh.

Code:
void ClassTwo::showData(ClassOne & classOneobj)
{
   classOneobj.getData();
}

Thank you.

One last question:
Code:
int main() {
	ClassTwo dummy;
	dummy.showData();
	return 0;
}

dummy.showData(); wants parameters passed into it. What would they be?

Parameters with data types like strings and ints, I know to pass in strings and ints

void someFunction (string someString)
someObject.someFunction("this is a string");

But what would the parameters for a reference be?

Edit: I tried doing: dummy.showData(ClassOne & dummy); but coming back with an "illegal use of expression" error.

Edit 2: Okay, I got it. It should be

Code:
int main() {
	ClassTwo dummy;
	ClassOne dummy1;
	dummy.showData(dummy1);
	return 0;
}

Thank you for everything Mark.
 
Last edited:

What is the difference between public and private data members in C++?

In C++, public data members can be accessed by any object, while private data members can only be accessed by methods within the same class. This allows for better control and encapsulation of data within an object.

How can I access private data members from another object in C++?

In order to access private data members from another object, you can create a public method within the class that allows for retrieval of the private data. This method can then be called by another object to access the data.

Can objects of different classes access each other's data members in C++?

No, objects of different classes cannot directly access each other's data members. However, you can create friend classes which can access private data members of another class.

Is it possible for a derived class to access private data members of its base class in C++?

Yes, a derived class can access private data members of its base class as long as the derived class is declared as a friend class in the base class.

What is the purpose of using accessor and mutator methods in C++?

Accessor methods are used to retrieve the value of a private data member, while mutator methods are used to modify the value of a private data member. This allows for controlled access and modification of data within an object.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
875
  • Engineering and Comp Sci Homework Help
Replies
1
Views
858
  • Programming and Computer Science
Replies
2
Views
641
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top