📜  引用 C++ 中的动态对象

📅  最后修改于: 2022-05-13 01:54:26.614000             🧑  作者: Mango

引用 C++ 中的动态对象

在 C++ 中,可以在运行时创建对象。 C++ 支持两个运算符newdelete来执行内存分配和取消分配。这些类型的对象称为动态对象。 new运算符用于动态创建对象,delete运算符用于动态删除对象。动态对象可以在指针的帮助下创建。

句法:

下面是实现动态对象的 C++ 程序。

C++
// C++ program to implement 
// dynamic objects
#include
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;
}


C++
// C++ program to implement
// Reference to dynamic objects
#include
#include
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;
}


输出
Constructor is called
a = 1
b = 2
Destructor is called

引用动态对象

可以取消引用 new运算符返回的动态对象的地址,并可以创建对它们的引用。

句法:

对对象 RefObj 的引用可以用作普通对象。分配给此类对象的内存只能在程序终止期间释放。

下面是引用 C++ 中的动态对象的程序。

C++

// C++ program to implement
// Reference to dynamic objects
#include
#include
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;
}

输出: