عرض نسخة كاملة من lesson1 c++ درس رقم 1

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.

C++ Tutorial - Class Data Members:

   Very important point about the Data members in this C++ Tutorial! This title is not a keyword or a data type in C++. This is just to explain one of the logical classifications of the types of members that are available in C++. 

   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.

C++ Tutorial - Function members in classes:

   Functions declared inside a class can be any of the following four types. This C++ Tutorial explains each one of them as below.

Ordinary member functions :

   These are ordinary functions defined with a return type and parameters. The return type can also be void. The special trait about member functions is they can access the private/protected data members of their class and manipulate them. No external functions can access the private/protected data members of a class. The sample below this C++ Tutorial uses an ordinary member function Add(), returning an integer value.

Constructors:

   Constructors in C++ are special member functions of a class. They have the same name as the Class Name. There can be any number of overloaded constructors inside a class, provided they have a different set of parameters. There are some important qualities for a constructor to be noted.    In the example class given below in this C++ tutorial has the constructor Example_Class(), with the same name as the class.

Destructors:

   Destructors in C++ also have the same name, except for the fact that they are preceded by a '~' operator. The destructors are called when the object of a class goes out of scope. It is not necessary to declare a constructor or a destructor inside a class. If not declared, the compiler will automatically create a default one for each. If the constructor/destructor is declared as private, then the class cannot be instantiated. Check below for the sample class of the C++ tutorial for an example of destructor.

C++ Tutorial - Access Level:

   The classes in C++ have 3 important access levels. They are Private, Public and Protected. The explanations are as follows.

Private:

   The members are accessible only by the member functions or friend functions.

Protected:

   These members are accessible by the member functions of the class and the classes which are derived from this class.

Public:

   Accessible by any external member. Look at the sample class below.

C++ Tutorial - Example of a class:

   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.

  • A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
  • A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.
  • A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
  • A static member function cannot have access to the 'this' pointer of the class.

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.

C++ Tutorial - important notes on this pointer:

  • this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
  • this pointer is not counted for calculating the size of the object.
  • this pointers are not accessible for static member functions.
  • this pointers are not modifiable. 

   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.

C++ Tutorial - important notes on this pointer:

  • this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
  • this pointer is not counted for calculating the size of the object.
  • this pointers are not accessible for static member functions.
  • this pointers are not modifiable. 

   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.