We will focus on the concept of classes because they are widely used in programming languages that support object oriented objects. When you continue in the field of software, you will see many modern programming languages that use the class.
Classes
A class and a fixed component of a structure that contains within it a number of variables, functions, and links between them, performing a single function or common to other classes.
1.Classes component
And because we are talking about the C++ language, we will follow the syntax of this language in defining the components, and among the most important components of the classes:
- Private identifiers: the private area in the component that the class can see from the inside in all its functions, but the editor cannot see it outside the bounds of the object.
class test { private: };
- Public identifiers: the public area inside the object or class where the editor can deal with it outside and affect its variables significantly.
class test { public: };
- Variables: Each object must contain at least one variable of any type: int, double, char and other C++ identifiers or even 3d party library identifiers.
class test { private: int count = 0; //variables public: };
- Functions: Also, there must be at least one function to maintain the shape and purpose of the object or the classes, and the function is an association in which arithmetic operations are performed.
class test { private: int count = 0; public: int getcount() { return count; } };
- The constructor: It is a function that takes the name of the object and its properties and is generated at the moment the object is born. The object is born when a variable of its type is defined in the main function, and contains values that are changed either from outside anywhere when called, or from inside without the editor noticing that change
class test { private: int count = 0; public: test() { // this is constructor count++; } };
- Destructor: It organizes memory management so that the heap of values and code waste does not accumulate when the object is destroyed, and it is often used in dynamic objects and is denoted by the ~ sign.
class test { private: int count = 0; public: test() { //this is constructor count++; } ~test() { //this is destructor count = 0; } };
2. Classes Benefits
Classes or objects are useful in large projects that tolerate errors and repetition in labels. For example, when many variables are repeatedly used in the void main() page in C, the sequence of names becomes difficult and it is impossible to find new words for each variable.
The other point of the importance of organisms in the work of the so-called capsule code. Imagine the medicine capsule that contains the ingredients of the drug with small granules. Each pellet is wrapped in a tape. The matter will become impossible to encapsulate those granules, which led to placing them in a small capsule with all its contents.
And here came the benefit of object vectors, where each code is encapsulated in a page or a remote area that is called in only two lines or less, and this is called Encapsulation in programming languages.
3. Calling object
As we said earlier, placing object files is either in a repository of code in remote pages or on the same page, usually above the void main() , since we are dealing with C++ language, there is a main function for all program elements, all variables, objects and functions are called inside and this is an area Running code, which is the most important area in calling a program and arranging its contents.
int main() { return 0; }
Without using this function or a function of type void main(), your code will not work, and it is fixed in this way, only returns zero. To understand retro and void functions, visit the C++ course for beginners.
When calling an object in the void main or int main function, we define the name of the object with the same name to access all functions and operations available within it.
4.Example
Please, to understand the classes well, copy the following code into your code editor.
/* static variabe with Class */ #include<iostream> using namespace std; class test{ private : int count=0; public: //static count is off bcs output will be 1 At The all obects in main test(){ count++; } int getcount(){ return count; } }; int main(){ test s1,s2,s3; cout<<"Count Is : "<<s1.getcount()<<endl; cout<<"Count Is : "<<s2.getcount()<<endl; cout<<"Count Is : "<<s3.getcount()<<endl; return 0; }