I am working a project,how do i access a class through another class,
For example I have a guest class,with vechile inside of it.How do I access vechile through guest.
4 replies to this topic
#1
Posted 28 October 2010 - 12:02 PM
|
|
|
#2
Posted 28 October 2010 - 12:13 PM
class Guest {
public:
class Vechile {
public:
void print() {
cout << "inside Vechile::print()" << endl;
}
};
Vechile vechile;
};
int main() {
Guest g;
g.vechile.print();
return 0;
}
Perhaps something like this?
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#3
Posted 28 October 2010 - 04:41 PM
Its not an nested class,but the vehicle class is located within a header file to itself and an instance of vehicle is defined within guest.How do I access this instance without getting an error about private member functions or variables.
#4
Posted 28 October 2010 - 07:04 PM
You cannot access the private data of a class from outside that class. You have to provide a function that gets the private data, or you have to make it public.
Dave
#5
Posted 30 October 2010 - 08:49 AM
Hi,
Normally, you cannot access private data members of a class - However if you want access private member of a class within another class; then you must declare that class as friend of first class.
Example
For more details, you can find tutorial on friend class
Friend Functions and Friend Classes - Cprogramming.com
I hope this helps
Munir
Normally, you cannot access private data members of a class - However if you want access private member of a class within another class; then you must declare that class as friend of first class.
Example
class SampleFriendClass;
class Target
{
private:
int i;
friend class SampleFriendClass;
};
class SampleFriendClass
{
public:
AccessPrivateMembers (Target &target)
{
cout<<target.i<<endl;
}
};
For more details, you can find tutorial on friend class
Friend Functions and Friend Classes - Cprogramming.com
I hope this helps
Munir
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









