SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 14/05/2016, 03:41 PM   #1
iSteve
Big Clucker
 
Join Date: Apr 2014
Posts: 88
Reputation: 15
Default [C++GDK] Classes and Objects

Author:iSteve/ Srinabh Jha
Difficulty:Novic
Title:Classes and Objects
Previous:[Tutorial] [C++GDK] C++ basics.
Next:Coming soon
Made for SA-MP forums


Alright, for those who don't know, you can write your gamemodes ( or codes ) for samp in C/C++ using Zeex's GDK.
In this tutorial i would be teaching you basics of C++, which you can implement in your gamemodes.

Class.
A class is a user defined data type, a class can hold data members and member functions, with their access specifier.
By default a class's access specifier is Private.
A class is terminated by a semi column ";"

PHP Code:
eg:
class 
test
{
    class 
members and member functiins;
}; 
Access Specifiers.
These declare the visiblity of the members of a user defined data type.
Private members can not be accessed by anything other than a friend( will come back to this later) or a member of that class.
Protected members has the same visiblity as that of a private one, but the difference lies in inheritance ( will be discussed later).
Public members can be accessed by other members of the class.

Object
An object is an instance of a class, or a variable of a class.
Eg
PHP Code:
class TEST
{
    
int a;
};
int main()
{
    
TEST ob;

Accessing member functions or member data from an object.
To access a function in a class using an object, we can use the object with a dot "." and then the function name.

eg:
PHP Code:
class TEST
{
    public:
int a;
    
void hello()
    {
        
cout<<"Hello world\n";
    }
};
int main()
{
    
TEST obj;
    
obj.a=1;//initializes the value of a as 1
    
obj.hello()//prints Hello world on the terminal.

Scope resolution operator"::"

Expanding functions outside the class
A scope resolution operator is "::".It can be used to expand member function outside the class
Eg:
PHP Code:
class A
{
    public:
void print();
};
void A::print()
{
    
cout<<"Hello world";


Friend Functions

A friend function is an external function which can be declared friend to a class.The friend function can access private, protected as well as the public members of the class it is a friend to.
A friend function is not a member of the class it is declared a friend to.
Eg
PHP Code:
class A
{
    
int a=10;
    public:
friend int frnd(A);//
};

int frnd(A x)
{
    return 
A.a;
}
int main()
{
    
A ob1;
    
int x;
    
x=ob1.frnd();//x = 10

Friend Classes
A class can be declared as a friend to another class, this class can thus access the member functions and member data of the class declared friend to.
NOTE: if class B is a friend to class A, does not mean that class A is a friend to class B.
Eg:
PHP Code:
class A
{
    public:
    
void out()
    {
        
cout<<"Hello\n";
    }
    
friend class B;
};
class 
B
{
    public:
void output()
        {
            
out();//accessing the out() function of the class A in class B 
        
}
};

int main()
{
    
B ob1;
    
ob1.output();//prints Hello


Last edited by iSteve; 13/12/2016 at 05:09 AM.
iSteve is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with classes icko202 Scripting Help 8 14/04/2013 12:07 PM
Need help With Classes. iOmar Scripting Help 11 16/08/2012 04:00 PM
Locking some classes locks ALL classes thefatshizms Scripting Help 0 11/08/2012 10:00 PM
help with classes Corporalsw Scripting Help 5 24/04/2012 04:13 PM


All times are GMT. The time now is 02:37 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.