473,503 Members | 939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about the private data member

class Number
{
public:
Number(int num=0):private_number(num) {};

void compare(const Number & another_number_class) const
{
if(this->private_number == another_number_class.private_number)
std::cout<<"the two classes have the same numbers !"<<std::endl;
else
std::cout<<"the two classes have different numbers !"<<std::endl;
};

private:
int private_number;
};

In this class Number, the function "compare" can reach the private
data member of both current object and "the parameter object". Is this
because both objects belong to the same class?

stonny
Jun 27 '08 #1
3 1175
stonny wrote:
class Number
{
public:
Number(int num=0):private_number(num) {};

void compare(const Number & another_number_class) const
{
if(this->private_number == another_number_class.private_number)
std::cout<<"the two classes have the same numbers !"<<std::endl;
else
std::cout<<"the two classes have different numbers !"<<std::endl;
};

private:
int private_number;
};

In this class Number, the function "compare" can reach the private
data member of both current object and "the parameter object". Is this
because both objects belong to the same class?
Yes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On May 14, 3:40 pm, stonny <zhangdexin2...@gmail.comwrote:
Is this
because both objects belong to the same class?

stonny
Yes.
You can consider a class to be a friend of itself.
Jun 27 '08 #3
stonny <zh************@gmail.comwrote:

Other's have answered your direct question. I want to go over your code
and point out some issues that might help you understand it better...
class Number
{
public:
Number(int num=0):private_number(num) {};
Calling the variable "another_number_class" is incorrect. It is an
object that is a Number, not an object of some other class. Make sure
you understand the distinction between object and class.
void compare(const Number & another_number_class) const
{
if(this->private_number == another_number_class.private_number)
std::cout<<"the two classes have the same numbers!"<<std::endl;
else
std::cout<<"the two classes have different numbers!"<<std::endl;
};
This might be more comprehensible:

void compare( const Number& that ) const
{
if ( this->private_number == that->private_number )
cout << "the two objects have the same numbers!" << endl;
}

Note, both objects, "this" object and "that" object, are of the same
class.
private:
int private_number;
};

In this class Number, the function "compare" can reach the private
data member of both current object and "the parameter object". Is this
because both objects belong to the same class?
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3981
by: YellowDog | last post by:
I want to use a property without a private data member. This is supposed to be legal, except that I get an Stack Overflow Exception when I implicity use the set method. Here's the code: Option...
3
2053
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is...
12
2648
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
2
1497
by: Tom | last post by:
1. I drag the sqlDataAdapter and sqlConnection from the tool bar to the web form. It creates automatically: protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; protected...
8
1488
by: pauldepstein | last post by:
I am writing a program which looks at nodes which have coordinates which are time-dependent. So I have a class called node which contains the private member declarations int date; int month; ...
7
1270
by: Ryan | last post by:
I'm in the process of learning more about building my ASP.NET website to use my SQL datastore and am a bit confused about how ADO.NET works with ASP.NET. This Microsoft article implies that using...
4
1731
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good...
6
2260
by: zfareed | last post by:
Is it possible to print an array that is declared from a class type that also contains private data members? I have declared an array in the main function and then set the values for the array...
2
2586
by: mickey22 | last post by:
Hi all, I have some data members and member functions that are declared as private in a class A. Now I want to use these data members and functions(which are private) in my new C++ source...
4
3494
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.