14.05.2016, 15:41
(
Last edited by iSteve; 13/12/2016 at 05:09 AM.
)
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.
By default a class's access specifier is Private.
A class is terminated by a semi column ";"
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.
Eg
eg:
Expanding functions outside the class
A scope resolution operator is "::".It can be used to expand member function outside the class
Eg:
A friend function is not a member of the class it is declared a friend to.
Eg
NOTE: if class B is a friend to class A, does not mean that class A is a friend to class B.
Eg:
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
}