-
objects
i created a simple program to store two different names. how do i acesss the objects thats in int main from a class function?
Code:
#include <iostream>
#include <conio.h>
using namespace std;
class nameishere {
protected:
char *theName;
};
class player_name: public nameishere{
public:
player_name (char *);
};
player_name::player_name (char* name)
{
theName = name;
}
class display_theName {
public:
void display ();
};
void display_theName::display()
{
cout << sakic.theName; // <-- doesnt work
cout << joe.theName; // <-- doesnt work
}
int main()
{
player_name sakic ("sakic");
player_name joe ("joe");
getch();
return 0;
}
-
That wont work cos the function does not know where them are objects are declared or anything. It has no local variables to look at, nor has it got any globals to look at it. You need to pass the address of the classes that you have declared in main to the display_theName function. Or use the local variable which you have used within your class.
Code:
void display_theName::display()
{
cout << theName;
}