By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,418 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

how do i access data member from other class

P: n/a
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: jGAIS  gais.twbbs.org 
Author: diadia q 61-217-192-12.HINET-IP.hinet.net o
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.