Reference to Dynamic Objects in C++ Last Updated : 07 Mar, 2022 Comments Improve Suggest changes 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++ akshaysobti15 Follow Improve Article Tags : C++ Geeks Premier League Geeks-Premier-League-2022 Practice Tags : CPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read 30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is 15 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Use Case Diagram - Unified Modeling Language (UML) A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within 9 min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read Half Wave Rectifier A Half-wave rectifier is an electronic device that is used to convert Alternating current (AC) to Direct current (DC). A half-wave rectifier allows either a positive or negative half-cycle of AC to pass and blocks the other half-cycle. Half-wave rectifier selectively allows only one half-cycle of th 15 min read Like