Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Encapsulation

Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. 


Encapsulation clearly represents the ability to bundle related data and functionality within a single, autonomous entity called a class. 


Encapsulation helps by breaking a program down into small, self-contained entities.  



Why static methods cant access non static members?

A static variable will be available for the entire lifetime of the program, even before the object of call is being created.  See below code

class CreditCard
{
public:
    static int noOfCards;
    …
};
int CreditCard::noOfCards = 99;
int _tmain(int argc, _TCHAR* argv[])
{
   Console::WriteLine("{0}", CreditCard::noOfCards); 
   // This line will print 99.
   ...
}

Same holds true for the static functions declared in a class, with the limitation that A static member function can only access static class members.  We will see why cant static member function cant access not static member data.

In the following code:

class CreditCard
{
public:
   static int GetnoOfCards()
   {
       return noOfCards;
   }
   ...
private:
   static int noOfCards;
};
int CreditCard::noOfCards = 99;
int _tmain(int argc, _TCHAR* argv[])
{
   Console::WriteLine("{0}", CreditCard::GetnoOfCards());
   // This line will print 99
   ...

}

Its clear that we can access the static member function without creating the object of a class. But for non static members object must be declared or constructor must get called to assign them memory, they needs to be initialized  before they get used. Hence static methods cannot access the non static members.

What Is Object-Oriented Programming?

The Object Oriented Programming can be defined as type of programming where one can define not only the data type of a data structure, but also the types of operations (functions/methods) that can be applied to the data structure. 


The Object Oriented Programming mainly supports  features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance. 

Caching is a technique used to store frequently accessed data in a temporary storage layer to improve system performance and reduce latency....