Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing Data in Classes
Author Message
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #1
Accessing Data in Classes
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
(This post was last modified: 01-16-2011 08:25 PM by Scorch.)
01-16-2011 08:18 PM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner
**********

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #2
RE: Accessing Data in Classes
Finally something I already knew Tongue
01-16-2011 08:40 PM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #3
RE: Accessing Data in Classes
I usually just use children though the other ones are good to know too.
01-16-2011 09:06 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: