Computer Tech

Full Version: Accessing Data in Classes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This will be short.
There's basically 3 ways to access data from a class.

The most common way is a child of the class.
Code:
#include <iostream>

class Tutorial
{
    public:
    void func()
    {
        std::cout<<"Message from func01";
    }
};

int main()
{
    Tutorial child;
    child.func();
    std::cin.get();
    return 0;
}

You can also use a direct approach with static functions.
Code:
#include <iostream>

class Tutorial
{
    public:
    static void func()
    {
        std::cout<<"Message from func01";
    }
};

int main()
{
    Tutorial::func();
    std::cin.get();
    return 0;
}

The last method I can think of is a pointer to the class.
We'll use the arrow(->) operator for this.
Code:
#include <iostream>

class Tutorial
{
    public:
    void func()
    {
        std::cout<<"Message from func01";
    }
};

int main()
{
    Tutorial *ptr=NULL;
    ptr->func();
    std::cin.get();
    return 0;
}

Realise these two are equal.
Code:
a->b
//is the same as
(*a).b
Finally something I already knew Tongue
I usually just use children though the other ones are good to know too.
Reference URL's