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

  • Context: Comp Sci 
  • Thread starter Thread starter butterfli
  • Start date Start date
  • Tags Tags
    Data Members
Click For Summary

Discussion Overview

The discussion revolves around how to allow one object in C++ to access the data members of another object. It focuses on programming techniques related to object-oriented design, specifically encapsulation and method parameters.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes a need to access data members of one class from another class and provides a simplified code example.
  • Another participant suggests that since ClassOne is public, its data members can be accessed directly, but emphasizes that this practice is generally discouraged in favor of encapsulation.
  • A request for code examples is made to clarify how to pass a reference of ClassOne to the showData method in ClassTwo.
  • A participant provides a code snippet showing how to declare the showData method with a reference parameter to ClassOne.
  • There is confusion regarding the scope of m_SomeData, leading to a clarification that the parameter should be used to access data members instead of trying to access them directly.
  • A participant realizes they can call the getData method on the ClassOne object passed as a parameter to showData.
  • Further questions arise about how to correctly pass parameters to the showData method in the main function, leading to a clarification on how to instantiate and pass the ClassOne object.

Areas of Agreement / Disagreement

Participants generally agree on the need to pass a reference to ClassOne in order to access its data members from ClassTwo. However, there is some disagreement on the appropriateness of making data members public versus private, with some advocating for encapsulation.

Contextual Notes

Participants discuss the implications of data access levels in object-oriented programming, highlighting the importance of encapsulation and the potential pitfalls of public data members.

Who May Find This Useful

Students and programmers learning about object-oriented programming in C++, particularly those interested in class design and data encapsulation techniques.

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 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K