Classes gather the records which are related to one kind of object. In classes, there is not only the data of actual world item, but it also trying to get data. One more quality of classes is that they can get data from further classes.
A class has a lot of functions and variables. We can compare a class with a house. Here functions are like the entrance of the house and variables are article within the house. Classes are introduces in order to make the program more privatized. There are codes for various functions which are directed by variables. Class needs some data for its start but there is no need of control by functions or variables.
The format of classes is very easy to understand. First of all, you have to write ‘class’ and after that write the name. After that you have to use unwrap bracket. Now, explain the type of restraint. Restriction has 3 stages. These are public, protected and private. In public, a part of program which is not included in class can check the variables of class. In protected, nothing can contact with variable except the class. In order to make the restriction of your choice you have to write the restriction (public, protected, private). After this, you have to use colon. Now, you can write variables and different functions. At the end, you have to use the ending bracket and then semi-colon.
Constructor and destructor are known as the functions of classes. The format is very easy. Here, the class name shows the constructor as well as “a ~” previous to class name destructor. Constructor is used to start variables and destructor gives end. In this way, memory gets free space. Whenever a class is defined in a program, constructor is also called. Destructor is always used when there is the function related to the class gets ended.
If you want to describe a function which is a associated with the class but not with the real class then you have to write… ‘return type, class name, 2 colons, function name’ through this procedure, compiler will get to know that function is included in class.
using namespace std;
// all the functions underneath this are available to program.
void setspeed ( int p );
// Always remember the semi-colon
//This constructor can not get arguments
processorspeed = 0;
//Arguments can’t be taken by Destructors
void Computer::setspeed ( int p )
// If you have to indicate a function outside, then you have to write the name of the class
// following by the return type and after that two colons, then at the end put the name
// of that function.
processorspeed = p;
// These 2 colons explain the compiler that this function is included
// in the class
// For making a class you have to think it like
compute.setspeed ( 100 );
// If you have to run a function in class, then you have to write the name of the example,
// period, and the name of function.
Now, you have got an introduction of classes. It is good for you to keep on practicing it and if there is any trouble then you can visit our forum.