Help with ULAM

Hello judyotham,

Thank you for using code tags.

As I understand this
1
2
3
4
5
class UlamProgram
{
    public:
          String name;
};

"name" is defined as a "public" variable. As such the whole program has access to it. As a public variable you could just as easily used a "struct" or "global variable" because this defeats the purpose of a "class".

On the other hand
1
2
3
4
5
6
class ULAM
{
    private:
        UlamProgram programs[10];
        UlamScreen screen;
};

"programs" is a "private" variable to the class and only a member function of the class can access this variable.

The problem is not getting to "name", but needing a "get" function to access "programs" first. After that "name" is "public" and all you have to do is use its name.

Class "ULAM" should look something like:
1
2
3
4
5
6
7
8
9
class ULAM
{
    public:
        std::string GetName();

    private:
        UlamProgram programs[10];
        UlamScreen screen;
};


Then the function would be like:
1
2
3
4
std::string ULAM::GetName(size_t index)
{
	return programs[index].name;
}

I believe this should work, but I have not had a chance to test it yet.

Just a small thing you forgot, or missed, the semi-colon after the closing "}" of the class.

Hope that helps,

Andy
@Handy Andy,
Please read the OP's first paragraph: it is blatant spam. The problem has just been copied verbatim from some random source
https://stackoverflow.com/questions/52128892/c-get-acces-to-class-variable
Handy Andy wrote:
As a public variable you could just as easily used a "struct" or "global variable" because this defeats the purpose of a "class".
Nonsense. It defeats nothing and a global variable is someing entire different.

Did you see the link? It looks very much like spam...
Topic archived. No new replies allowed.