Class
A class in C++ is an encapsulation of data members and functions that manipulate the data. The class can also have some other important members which are architecturally important.
This C++ Tutorial discusses the components of a C++ class. More C++ tutorials will follow.
The data members can be of any legal data type, a class type, a struct type etc., They can also be declared as pointers and accessible normally as like other data members. The Example class given below in this C++ tutorial has two data members x and y of type integer.
class Example_class //Sample Class for the C++ Tutorial
{
private:
int x; //Data member
int y; // Data member
public:
Example_Class() //Constructor for the C++ tutorial
{
x = 0;
y = 0;
}
~Example_Class() //destructor for the C++ Tutorial
{ }
int Add()
{
return x+y;
}
};
-------------------------------------------
Function overloading is the practice of declaring the same function with different signatures. The same function name will be used with different number of parameters and parameters of different type. But overloading of functions with different return types are not allowed.
For example in this C++ Tutorial let us assume an AddAndDisplay function with different types of parameters.
//C++ Tutorial - Sample code for function overloading
void AddAndDisplay(int x, int y)
{
cout<<" C++ Tutorial - Integer result: "<<(x+y);
}
void AddAndDisplay(double x, double y)
{
cout<< " C++ Tutorial - Double result: "<<(x+y);
}
void AddAndDisplay(float x, float y)
{
cout<< " C++ Tutorial - float result: "<<(x+y);
}
Some times when these overloaded functions are called, they might cause ambiguity errors. This is because the compiler may not be able to decide what signature function should be called.
If the data is type cast properly, then these errors will be resolved easily. Typically, function overloading is used wherever a different type of data is to be dealt with. For example this can be used for a function which converts farenheit to celsius and vice versa. One of the functions can deal with the integer data, other can deal float for precision etc.,
-----------------------------------
When a function is declared inline, the function is expanded at the calling block. The function is not treated as a separate unit like other normal functions.
But a compiler is free to decide, if a function qualifies to be an inline function. If the inline function is found to have larger chunk of code, it will not be treated as an inline function, but as like other normal functions.
Inline functions are treated like macro definitions by the C++ compiler. They are declared with the keyword inline as follows.
//Declaration for C++ Tutorial inline sample:
int add(int x,int y);
//Definition for C++ Tutorial inline sample:
inline int add(int x,int y)
{
return x+y;
}
In fact, the keyword inline is not necessary. If the function is defined with its body directly and the function has a smaller block of code, it will be automatically treated as inline by the compiler.
As implied, inline functions are meant to be used if there is a need to repetitively execute a small block of code, which is smaller. When such functions are treated inline, it might result in a significant performance difference.
When a function is declared inline, the function is expanded at the calling block. The function is not treated as a separate unit like other normal functions.
But a compiler is free to decide, if a function qualifies to be an inline function. If the inline function is found to have larger chunk of code, it will not be treated as an inline function, but as like other normal functions.
Inline functions are treated like macro definitions by the C++ compiler. They are declared with the keyword inline as follows.
//Declaration for C++ Tutorial inline sample:
int add(int x,int y);
//Definition for C++ Tutorial inline sample:
inline int add(int x,int y)
{
return x+y;
}
In fact, the keyword inline is not necessary. If the function is defined with its body directly and the function has a smaller block of code, it will be automatically treated as inline by the compiler.
As implied, inline functions are meant to be used if there is a need to repetitively execute a small block of code, which is smaller. When such functions are treated inline, it might result in a significant performance difference.
-----------------------------------------------------------
Static data types can be accessed without instantiation of the class in C++. This is applicable for static functions also.
The differences between a static member function and non-static member functions are as follows.
The static member functions are not used very frequently in programs. But nevertheless, they become useful whenever we need to have functions which are accessible even when the class is not instantiated.
--------------------------------------------------------------------------------------
The this pointer is used as a pointer to the class object instance by the member function. The address of the class instance is passed as an implicit parameter to the member functions. The sample below, in this c++ Tutorial shows how to use it. It is a common knowledge that C++ keeps only one copy of each member function and the data members are allocated memory for all of their instances. This kind of various instances of data are maintained use this pointer. Look at the sample below, in this c++ Tutorial.
Look at the following example to understand how to use the 'this' pointer explained in this C++ Tutorial.
class this_pointer_example // class for explaining C++ tutorial
{
int data1;
public:
//Function using this pointer for C++ Tutorial
int getdata()
{
return this->data1;
}
//Function without using this pointer
void setdata(int newval)
{
data1 = newval;
}
};
Thus, a member function can gain the access of data member by either using this pointer or not.
----------------------------------------------------------------------------------
The this pointer is used as a pointer to the class object instance by the member function. The address of the class instance is passed as an implicit parameter to the member functions. The sample below, in this c++ Tutorial shows how to use it. It is a common knowledge that C++ keeps only one copy of each member function and the data members are allocated memory for all of their instances. This kind of various instances of data are maintained use this pointer. Look at the sample below, in this c++ Tutorial.
Look at the following example to understand how to use the 'this' pointer explained in this C++ Tutorial.
class this_pointer_example // class for explaining C++ tutorial
{
int data1;
public:
//Function using this pointer for C++ Tutorial
int getdata()
{
return this->data1;
}
//Function without using this pointer
void setdata(int newval)
{
data1 = newval;
}
};
Thus, a member function can gain the access of data member by either using this pointer or not.