Reference to Dynamic Objects in C++ Last Updated : 07 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically. The dynamic objects can be created with the help of pointers. Syntax: ClassName *ptr_obj; // pointer to objectptr_obj = new ClassName // Dynamic object creationdelete ptr_obj; // Delete object dynamically Below is the C++ program to implement the dynamic objects. C++ // C++ program to implement // dynamic objects #include<iostream> using namespace std; // Class definition class Test { // Data members int a, b; public: // Constructor to initialize // data members of class Test() { cout << "Constructor is called" << endl; a = 1; b = 2; }; // Destructor ~Test() { cout << "Destructor is called" << endl; } // Function to print values // of data members void show() { cout << "a = " << a << endl; cout << "b = " << b << endl; } }; // Driver code int main() { // pointer to object Test *ptr; // dynamic object creation ptr = new Test; // Accessing member through // pointer to object ptr->show(); // Destroying object dynamically delete ptr; return 0; } OutputConstructor is called a = 1 b = 2 Destructor is calledReference to Dynamic Objects The address of dynamic objects returned by the new operator can be dereferenced and a reference to them can be created. Syntax: ClassName &RefObj = * (new ClassName); The reference to object RefObj can be used as a normal object. The memory allocated to such objects cannot be released except during the termination of the program. Below is the program of Reference to Dynamic Objects in C++. C++ // C++ program to implement // Reference to dynamic objects #include<iostream> #include<string.h> using namespace std; class student{ private: int roll_no; char name[20]; public: void setdata(int roll_no_in, char name_in[20]) { roll_no = roll_no_in; strcpy(name, name_in); } void outdata() { cout << "Roll No is: " << roll_no << endl; cout << "Name: " << name << endl; } }; // Driver code int main() { // Reference to dynamic object student &s1 = *(new student); s1.setdata(1, "Ajay"); s1.outdata(); // Reference to dynamic object student &s2 = *(new student); s2.setdata(2, "Aman"); s2.outdata(); student &s3 = *(new student); s3.setdata(3, "Akshay"); // Reference to static object student &s4 = s3; s3.outdata(); s4.outdata(); return 0; } Output: Roll No is: 1Name: AjayRoll No is: 2Name: AmanRoll No is: 3Name: AkshayRoll No is: 3Name: Akshay Comment More infoAdvertise with us Next Article Reference to Dynamic Objects in C++ A akshaysobti15 Follow Improve Article Tags : C++ Geeks Premier League Geeks-Premier-League-2022 Practice Tags : CPP Similar Reads Pointers vs References in C++ Prerequisite: Pointers, References C and C++ support pointers, which is different from most other programming languages such as Java, Python, Ruby, Perl and PHP as they only support references. But interestingly, C++, along with pointers, also supports references. On the surface, both references and 5 min read Pointers and References in C++ In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a 5 min read Can References Refer to Invalid Location in C++? Reference Variables: You can create a second name for a variable in C++, which you can use to read or edit the original data contained in that variable. While this may not sound appealing at first, declaring a reference and assigning it a variable allows you to treat the reference as if it were the 2 min read References in C++ In C++, a reference works as an alias for an existing variable, providing an alternative name for it and allowing you to work with the original data directly.Example:C++#include <iostream> using namespace std; int main() { int x = 10; // ref is a reference to x. int& ref = x; // printing v 5 min read Passing Map as Reference in C++ STL Prerequisite: Maps in C++ STL Pass by reference Elements in the map are in form of pairs where the first is key and another value, denoting key-value pairs. Also, all the key values are unique means no two elements can have the same key value. Passing maps by value is a costly task, costly in terms 3 min read Like