Remove Ads

Share on Facebook Share on Twitter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing Data in Classes
#1
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;
}
[/code]

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;
}
[/code]

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;
}
[/code]

Realise these two are equal.
[code]
a->b
//is the same as
(*a).b
[/code]
Reply
#2
Finally something I already knew Tongue
Reply
#3
I usually just use children though the other ones are good to know too.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)