Pointers

 Pointers

In C++, pointers are variables that store the memory addresses of other variables.

Address in C++

If we have a variable var in our program, &var will give us its address in the memory. For example,

Example 1: Printing Variable Addresses in C++

#include <iostream>

using namespace std;

int main()

{

    // declare variables

    int var1 = 3;

    int var2 = 24;

    int var3 = 17;

    // print address of var1

    cout << "Address of var1: "<< &var1 << endl;

    // print address of var2

    cout << "Address of var2: " << &var2 << endl;

    // print address of var3

    cout << "Address of var3: " << &var3 << endl;

}

Pointers are used to store addresses rather than values.

Here is how we can declare pointers.

int *pointVar;

Here, we have declared a pointer pointVar of the int type.

We can also declare pointers in the following way.

int* pointVar; // preferred syntax

Let's take another example of declaring pointers.

int* pointVar, p;

Here, we have declared a pointer pointVar and a normal variable p.

Assigning Addresses to Pointers

Here is how we can assign addresses to pointers:

int* pointVar, var;

var = 5;

// assign address of var to pointVar pointer

pointVar = &var;

Here, 5 is assigned to the variable var. And, the address of var is assigned to the pointVar pointer with the code pointVar = &var.

Get the Value from the Address Using Pointers

To get the value pointed by a pointer, we use the * operator. For example:


int* pointVar, var;

var = 5;

// assign address of var to pointVar

pointVar = &var;

// access value pointed by pointVar

cout << *pointVar << endl;   // Output: 5

In the above code, the address of var is assigned to pointVar. We have used the *pointVar to get the value stored in that address.

When * is used with pointers, it's called the dereference operator. It operates on a pointer and gives the value pointed by the address stored in the pointer. That is, *pointVar = var.

Note: In C++, pointVar and *pointVar is completely different. We cannot do something like *pointVar = &var;

Changing Value Pointed by Pointers

If pointVar points to the address of var, we can change the value of var by using *pointVar.

Example,

int var = 5;

int* pointVar;

// assign address of var

pointVar = &var;

// change value at address pointVar

*pointVar = 1;

cout << var << endl; // Output: 1

Here, pointVar and &var have the same address, the value of var will also be changed when *pointVar is changed.

Ref: https://www.programiz.com/cpp-programming/pointers

Abstraction

Data Abstraction in C++

  • Data Abstraction is a process of providing only the essential details to the outside world and hiding the internal details, i.e., representing only the essential details in the program.
  • Data Abstraction is a programming technique that depends on the seperation of the interface and implementation details of the program.
  • Let's take a real life example of AC, which can be turned ON or OFF, change the temperature, change the mode, and other external components such as fan, swing. But, we don't know the internal details of the AC, i.e., how it works internally. Thus, we can say that AC seperates the implementation details from the external interface.
  • C++ provides a great level of abstraction. For example, pow() function is used to calculate the power of a number without knowing the algorithm the function follows.

In C++ program if we implement class with private and public members then it is an example of data abstraction.

Data Abstraction can be achieved in two ways:

  • Abstraction using classes
  • Abstraction in header files.

Data Abstraction in C++

Abstraction using classes: An abstraction can be achieved using classes. A class is used to group all the data members and member functions into a single unit by using the access specifiers. A class has the responsibility to determine which data member is to be visible outside and which is not.

Abstraction in header files: An another type of abstraction is header file. For example, pow() function available is used to calculate the power of a number without actually knowing which algorithm function uses to calculate the power. Thus, we can say that header files hides all the implementation details from the user.

Access Specifiers Implement Abstraction:

  • Public specifier: When the members are declared as public, members can be accessed anywhere from the program.
  • Private specifier: When the members are declared as private, members can only be accessed only by the member functions of the class.

Let's see a simple example of abstraction in header files.

// program to calculate the power of a number.

  1. #include <iostream>  
  2. #include<math.h>  
  3. using namespace std;  
  4. int main()  
  5. {    
  6.  int n = 4;  
  7.    int power = 3;  
  8.    int result = pow(n,power);         // pow(n,power) is the  power function  
  9.    std::cout << "Cube of n is : " <<result<< std::endl;  
  10.    return 0;  
  11. }  

Output:

Cube of n is : 64

In the above example, pow() function is used to calculate 4 raised to the power 3. The pow() function is present in the math.h header file in which all the implementation details of the pow() function is hidden.

Let's see a simple example of data abstraction using classes.

  1. #include <iostream>    
  2. using namespace std;    
  3.  class Sum    
  4. {    
  5. privateint x, y, z; // private variables  
  6. public:    
  7. void add()    
  8. {    
  9. cout<<"Enter two numbers: ";    
  10. cin>>x>>y;    
  11. z= x+y;    
  12. cout<<"Sum of two number is: "<<z<<endl;    
  13. }    
  14. };    
  15. int main()    
  16. {    
  17. Sum sm;    
  18. sm.add();    
  19. return 0;    
  20. }    

Output:

Enter two numbers:
3
6
Sum of two number is: 9

In the above example, abstraction is achieved using classes. A class 'Sum' contains the private members x, y and z are only accessible by the member functions of the class.

Advantages Of Abstraction:

  • Implementation details of the class are protected from the inadvertent user level errors.
  • A programmer does not need to write the low level code.
  • Data Abstraction avoids the code duplication, i.e., programmer does not have to undergo the same tasks every time to perform the similar operation.
  • The main aim of the data abstraction is to reuse the code and the proper partitioning of the code across the classes.
  • Internal implementation can be changed without affecting the user level code.

Ref: https://www.javatpoint.com/data-abstraction-in-cpp