473,378 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how do i access data member from other class

when i write two classes as follow .

compiler tell me that i can't access private number from other class

but if types of two classes are the same it don't have error message ? why?

class schema
{

private:
string name;
int age;
public:
schema(string n,int a):name(n),age(a){}

};
class data
{
private:
string str;
int num;
public:
data(int n,string s):num(n),str(s){}
data(){};

//error ! access private member from other class
void gets(const schema &s){str = s.name ; num = s.age ;}

// ok it can work,but why? it's also private member
void getd(const data &s) { str = s.str ; num = s.num;}
void put(){cout<<str<<num;}

};
--
Origin: ¤¤¥¿¤j¾Ç£»GAIS¤ý´Â  gais.twbbs.org 
Author: diadia ±q 61-217-192-12.HINET-IP.hinet.net µoªí
Jul 22 '05 #1
2 2572
diadia wrote:
when i write two classes as follow .

compiler tell me that i can't access private number from other class

but if types of two classes are the same it don't have error message ? why?

class schema
{

private:
string name;
int age;
public:
schema(string n,int a):name(n),age(a){}

};
class data
{
private:
string str;
int num;
public:
data(int n,string s):num(n),str(s){}
data(){};

//error ! access private member from other class
void gets(const schema &s){str = s.name ; num = s.age ;}

// ok it can work,but why? it's also private member
void getd(const data &s) { str = s.str ; num = s.num;}
void put(){cout<<str<<num;}

};


Note that access permissions are based on types not on
individual objects.
HDH,
marc

Jul 22 '05 #2


diadia wrote:
when i write two classes as follow .

compiler tell me that i can't access private number from other class

but if types of two classes are the same it don't have error message ? why?
a class can access it's own private data members from within the class. A class can not access
another class's private membes..thats the very reason you would make them private: so that only that
class that owns the private members can access them. If you want to be able to acess them from
outside that classs you need to create a function that returns those values and then save those
values to variables. If u want to save the values to private members of another class then you need
to also create a function in that "destination" class to set those variables within the class.


class schema
{

private:
string name;
int age;
public:
schema(string n,int a):name(n),age(a){}

};

class data
{
private:
string str;
int num;
public:
data(int n,string s):num(n),str(s){}
data(){};

//error ! access private member from other class
void gets(const schema &s){str = s.name ; num = s.age ;}

// ok it can work,but why? it's also private member
void getd(const data &s) { str = s.str ; num = s.num;}
void put(){cout<<str<<num;}

};


Jul 22 '05 #3

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

Similar topics

2
by: Michael Young | last post by:
I've been trying to compile code similar to the example code below, but I keep getting errors indicating that the function 'foo()' is not accessible. At first, I thought this was a bug in the...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
9
by: Maciej | last post by:
Hi, I tried to build windl project from the book 'The Art of C++" in VS.NET 7 and it fails. I attached all required .lib files. I got the following error messages from the linker: windl error...
3
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
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 )...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
5
by: Peter Olcott | last post by:
I created an object that requires access to another objects data, yet have found no good way to pass this data as a parameter because the member function that requires this data must be a binary...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.