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

  • Thread starter Thread starter butterfli
  • Start date Start date
  • Tags Tags
    Data Members
AI Thread Summary
To enable one object to access another object's data members in C++, a reference to the first object must be passed as a parameter to a method in the second object. In the provided code, ClassTwo's showData method should accept a reference to an instance of ClassOne, allowing access to its public methods and data members. The correct implementation involves calling ClassOne's getData method within showData to retrieve the desired data. Encapsulation principles suggest that data members should typically be private, promoting better data management and security. The final main function demonstrates the correct instantiation and parameter passing to showData.
butterfli
Messages
10
Reaction score
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
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.
 
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.
 
Code:
void classTwo::showData(ClassOne & classOneobj)
{
   // code for method
}

Inside this method you can call getData or evaluate m_someData directly.
 
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...?
 
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?
 
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:

Similar threads

Replies
12
Views
2K
Replies
2
Views
2K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
15
Views
2K
Replies
9
Views
3K
Back
Top