C++ Object Pointers & Object Reference in OOP | Reference vs Pointer

C++ Object Pointers in OOP | C++ Object Reference in OOP | Difference Between Reference and Pointer |OOP Tutorial in C++

In this tutorial, we are going to learn about the C++ Object Pointers in OOP , C++ Object Reference in OOP , Difference Between Reference and Pointer ,OOP Tutorial in C++

What is Object Pointers in C++?

The pointers pointing to objects are referred to as Object Pointers.

A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access members of a pointer to a class you use the member access operator -> operator.


Syntax of Object Pointer in OOP :

class-name ∗ object-pointer ;


Let's see an example of Object Pointers in C++ :

class Simple{
    public:
      int a;
};
int main(){
    Simple obj;
    Simple* ptr;   // Pointer of class type
    ptr = &obj;
    cout << obj.a;
    cout << ptr->a;  // Accessing member with pointer
    return 0;
}


C++ this Pointer:

In C++ programming, this is a keyword that refers to the current instance of the class.

There can be 3 main usage of this keyword in C++:

It can be used to pass current object as a parameter to another method.

It can be used to refer current class instance variable.

It can be used to declare indexers.

Let's see the example of this keyword in C++ that refers to the fields of current class.

C++ this Pointer Example:

#include <iostream>  
using namespace std;  
class Employee {  
   public:  
       int id; //data member (also instance variable)      
       string name; //data member(also instance variable)  
       float salary;  
       Employee(int id, string name, float salary)    
        {    

             this->id = id;    

            this->name = name;    

            this->salary = salary;   

        }    

       void display()    

        {    

            cout<<id<<"  "<<name<<"  "<<salary<<endl;    

        }    

};  

int main(void) {  

    Employee e1 =Employee(101, "Sonoo", 890000); 

    Employee e2=Employee(102, "Nakul", 59000);  

    e1.display();    

    e2.display();    

    return 0;  

}


Output

101  Sonoo  890000

102  Nakul  59000

  

What is Object Reference?


A reference is an address that indicates where an object's variables and methods are stored and can be declared as a reference by putting ‘&’ in the declaration.

There is no need to use the * to dereference a reference variable.

Object Reference Example :

#include <iostream>  
using namespace std;

int main(){ 
    int y=10;
    int &r = y;  // r is a reference to int y
    cout << r;
}

Output

10


Difference between Reference and Pointer :

References

Pointers

Reference must be initialized when it is created.

Pointers can be initialized any time.

Once initialized, we cannot reinitialize a reference.

Pointers can be reinitialized any number of time.

You can never have a NULL reference.

Pointers can be NULL.

Reference is automatically dereferenced.

* is used to dereference a pointer.





0 Response to C++ Object Pointers & Object Reference in OOP | Reference vs Pointer

Post a Comment